Skip to content

Instantly share code, notes, and snippets.

View crtr0's full-sized avatar

Carter Rabasa crtr0

View GitHub Profile
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"] }]
}
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);
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,
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);
}
});
}
voteCounts = exports.voteCounts = function(event, callback) {
db.view('event', 'all', {startkey: [event._id], endkey: [event._id, {}, {}], group_level: 2}, function(err, body) {
if (err) {
callback(err);
}
else {
// populate count for voteoptions
event.voteoptions.forEach(function(vo, i){
var found = _und.find(body.rows, function(x) {return x.key[1] == vo.id});
vo['votes'] = (found? found.value : 0);
@crtr0
crtr0 / gist:4506628
Created January 10, 2013 23:15
Nano error on bulk update
/Users/crabasa/Code/tmp/votr-part3/node_modules/nano/node_modules/errs/lib/errs.js:167
merged.stacktrace = err.stack.split("\n");
^
TypeError: Object fabric_doc_update:handle_message/3,rexi_utils:process_mailbox/6,rexi_utils:recv/6,fabric_doc_update:go/3,fabric:update_docs/3,chttpd_db:db_req/2,chttpd:handle_request/1,mochiweb_http:headers/5 has no method 'split'
at Object.exports.merge (/Users/crabasa/Code/tmp/votr-part3/node_modules/nano/node_modules/errs/lib/errs.js:167:33)
at Request._callback (/Users/crabasa/Code/tmp/votr-part3/node_modules/nano/nano.js:311:28)
at Request.init.self.callback (/Users/crabasa/Code/tmp/votr-part3/node_modules/nano/node_modules/request/main.js:122:22)
at Request.EventEmitter.emit (events.js:91:17)
at Request.<anonymous> (/Users/crabasa/Code/tmp/votr-part3/node_modules/nano/node_modules/request/main.js:661:16)
at Request.EventEmitter.emit (events.js:115:20)
~/Code/scripts
ಠ_ಠ node -e 'require("http").createServer(function(q,s){console.error(q.headers);s.end("ok");this.close()}).listen(1337)' &
[1] 50947
~/Code/scripts
ಠ_ಠ cat foo.py #!/usr/bin/env python
import urllib
response = urllib.urlopen("http://localhost:1337")
print response