Skip to content

Instantly share code, notes, and snippets.

View crtr0's full-sized avatar

Carter Rabasa crtr0

View GitHub Profile
### Keybase proof
I hereby claim:
* I am crabasa on github.
* I am carterrabasa (https://keybase.io/carterrabasa) on keybase.
* I have a public key whose fingerprint is BBF3 AD23 FD40 EC24 1E16 BF9F 4495 F1D3 412F B86B
To claim this, I am signing this object:
@crtr0
crtr0 / domains.js
Last active December 19, 2015 21:59
var d = domain.create();
d.on("error", function(error) {
// handle it
});
d.run(function() {
// deeply nested and possibly async code
});
@crtr0
crtr0 / sample.mm
Last active December 19, 2015 06:39 — forked from kwhinnery/sample.mm
#pragma mark * UIView methods
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
// Synchronous networking on the main thread is seriously bad - only doing this for
// demo purposes
NSURL* url = [NSURL URLWithString:@"https://your-domain.azurewebsites.net/capability"];
NSURLResponse* response = nil;
require 'twilio-ruby'
require './creds'
# initialize Twilio client
client = Twilio::REST::Client.new(Creds.ACCOUNT_SID, Creds.AUTH_TOKEN)
# groups all phone numbers based on number of correct answers to trivia questions
pool = [
[3, ["a", "b", "c"]],
[2, ["d", "e", "f"]],
{ "_id": "event:demo",
"type": "event",
"shortname": "demo",
"name": "My Cool Demo Event",
"phonenumber": "+15555551212",
"state": "on",
"voteoptions": [{ "id": 1, "name": "foo"}, { "id": 2, "name": "bar"}, { "id": 3, "name": "baz"}]
}
{ "_id": "vote:event:demo:+15555551111",
@crtr0
crtr0 / event.json
Last active December 11, 2015 03:58
{ "_id": "123",
"shortname": "demo",
"name": "My Cool Demo Event",
"phonenumber": "+15555551212",
"state": "on",
"voteoptions": [
{ "id": 1, "name": "foo", "votes": 2, "numbers": ["+15555551111", "+15555552222"] },
{ "id": 2, "name": "bar", "votes": 0, "numbers": [] },
{ "id": 3, "name": "baz", "votes": 1, "numbers": ["+1555555333"] }]
}
flushVotes = function() {
var votesToSave = _und.values(votesCache);
votesCache = {};
if (votesToSave.length > 0) {
db.bulk({docs: votesToSave}, function(err, body) {
if (err) {
console.log("Failed to save votes, popping them back on the cache");
votesToSave.forEach(function(v) {
votesCache[v._id] = v;
saveVote = exports.saveVote = function(event, vote, from) {
// The _id of our vote document will be a composite of our event_id and the
// person's phone number. This will guarantee one vote per event
var voteDoc = {
_id: 'vote:' + event._id + ':' + from,
type: 'vote',
event_id: event._id,
event_phonenumber: event.phonenumber,
vote: vote,
findBy = exports.findBy = function(view, params, callback) {
var event;
if (event = eventsCache[view+JSON.stringify(params)]) {
callback(null, event);
}
else {
db.view('event', view, params, function(err, body) {
if (err) {
console.log(err);
findByPhonenumber = exports.findByPhonenumber = function(phonenumber, callback) {
findBy('byPhonenumber', {key: phonenumber}, function(err, event) {
if (err) {
callback(err, null);
}
else {
findBy('all', {key: [event._id], reduce: false}, callback);
}
});
}