Skip to content

Instantly share code, notes, and snippets.

View gartenfeld's full-sized avatar

David Rosson gartenfeld

View GitHub Profile
@gartenfeld
gartenfeld / blockspring.js
Created August 28, 2015 01:55
Serverless Slack Bots
// https://www.blockspring.com/blog/serverless-slack-bots
blockspring = require('blockspring');
var webhook = function(team_domain, service_id, token, user_name, team_id, user_id, channel_id, timestamp, channel_name, text, trigger_word, raw_text) {
// Basic bot will just echo back the message
response = ["*", user_name, "* said _", text, "_"].join('')
return {
@gartenfeld
gartenfeld / bluebird_promise.js
Created August 20, 2015 23:53
Promises with Bluebird.
var Promise = require('bluebird');
var async = function (input) {
return new Promise(function (resolve, reject) {
var data = input + ' Whatever.';
// call the async process here
setTimeout(function(){
// send the result when it arrives
resolve(data);
}, 100);
@gartenfeld
gartenfeld / q_promise.js
Last active August 29, 2015 14:27
Promises using Q.
var Q = require('q');
var async = function (prefix) {
// create a promise wrapper
var hold = Q.defer();
var success = function () {
var data = prefix + "SOLVED!";
// by calling 'resolve', the next steps in the
// 'then' handler (e.g. 'console.log' below)
// will be executed with 'data' passed in
@gartenfeld
gartenfeld / code_tag.css
Created August 6, 2015 23:53
Inline code styling.
code {
padding: 2px 3px;
color: #d14;
background-color: #f7f7f9;
border: 1px solid #e1e1e8;
white-space: nowrap;
}
@gartenfeld
gartenfeld / backbone-collection-cycle.coffee
Last active August 29, 2015 14:26 — forked from rjz/backbone-collection-cycle.coffee
Find next model in a Backbone Collection
# Methods for cycling through the models in a Backbone Collection
# Usage:
#
# c = new MyCollection([...])
# nextModel = c.modelAfter(myModel)
# myModel == c.modelBefore(nextModel)
# # true
#
class MyCollection extends Backbone.Collection
@gartenfeld
gartenfeld / passport_session.js
Last active August 29, 2015 14:26
Passport authentication using basic credentials.
passport.use(new LocalStrategy( // a new Local instance
function(username, password, done) { // passing in a function
User.findOne({ username: username }, function (err, user) { // Mongo interface
if (err) { return done(err); } // generic error
if (!user) { return done(null, false); } // user not found
if (!user.verifyPassword(password)) { return done(null, false); } // wrong password
return done(null, user); // authenticated, returns 'user'
});
}
));
@gartenfeld
gartenfeld / powerset.js
Created July 23, 2015 17:10
Generating a powerset
function powerset(alphabet) {
var p = [[]];
for (var i=0; i < alphabet.length; i++) {
// here the value of len is set at the beginning
for (var j = 0, l = p.length; j < l; j++) {
p.push(p[j].concat(alphabet[i]));
// inspector
console.log(p);
}
}
@gartenfeld
gartenfeld / node_post.js
Created July 22, 2015 00:04
Extracting POST data in Node as the pieces arrive in a buffer stream.
var data = '';
request.on('data', function (partial) {
data += partial;
});
request.on('end', function () {
data = JSON.parse(data);
});
@gartenfeld
gartenfeld / perm.js
Created July 9, 2015 23:50
Generating permutations and logging each step.
function permutator(inputArr) {
// the output matrix of all permutations
var results = [];
// takes the array of character set
function permute(columnOptions, path) {
var place, newPath, path = path || [];
// for a given row
// go through the options for "which column"
@gartenfeld
gartenfeld / color_strip.js
Created July 9, 2015 23:49
Generating a hue spectrum.
var hue, cell,
grades = 100,
query = '';
for (var i = 0; i < grades; i++) {
hue = i * 360 / grades
query = '<div style="float:left;' +
'width:' + (100/grades) + '%;' +
'height:20px;' +
'background-color: hsl(' + hue + ', 100%, 75%);"></div>'