Skip to content

Instantly share code, notes, and snippets.

View crtr0's full-sized avatar

Carter Rabasa crtr0

View GitHub Profile
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
@crtr0
crtr0 / js_forloop_wtf.js
Created December 27, 2012 18:24
Can anyone explain why the if statement below never succeeds? I am running it as part of a Node.js v0.8.7 app.
var arr = ['a', 'b', 'c'];
for (i in arr) {
if (i === 0) {
console.log('do something special')
}
}
// sadly, that console.log never happens
@crtr0
crtr0 / gist:4197996
Created December 3, 2012 21:02
Listen for a "vote" event from socket.io and update the chart data appopriately
socket.on('vote', function(data) {
vote = parseInt(data);
index = vote - 1;
votes = chart.series[0].data[index].y;
chart.series[0].data[index].update(votes+1);
});
@crtr0
crtr0 / gist:4197978
Created December 3, 2012 20:59
Connects to the socket.io server and emit a message
var socket = io.connect();
socket.on('connect', function() {
console.log("Connected, lets sign-up for updates about votes for this event");
socket.emit('event', '{{ shortname }}');
});
@crtr0
crtr0 / gist:4197975
Created December 3, 2012 20:58
Initialize socket.io, attach it to our server and handle some events
socketio = require('socket.io')
// Attach socket.io to our web server
io = socketio.listen(server);
io.configure('development', function(){
io.set('log level', 1);
});
io.sockets.on('connection', function(socket) {
@crtr0
crtr0 / gist:4197970
Created December 3, 2012 20:57
Initialize chart data and labels arrays for Highcharts
var chartdata = [],
labels = [];
voting.forEach(function(vo, i) {
// the number of votes
chartdata.push(vo.votes);
// the label for this data point
labels.push(vo.name+' - '+(i+1));
});
@crtr0
crtr0 / gist:4197962
Created December 3, 2012 20:56
Monkey patch for unescaping HTML in a String
String.prototype.unescapeHtml = function () {
var temp = document.createElement("div");
temp.innerHTML = this;
var result = temp.childNodes[0].nodeValue;
temp.removeChild(temp.firstChild);
return result;
}
@crtr0
crtr0 / gist:4197945
Created December 3, 2012 20:54
Decoding some JSON data in our Mustache template
var data = "{{ voteoptions }}";
var voting_string = data.unescapeHtml();
var voting = JSON.parse(voting_string);