Skip to content

Instantly share code, notes, and snippets.

View crtr0's full-sized avatar

Carter Rabasa crtr0

View GitHub Profile
var Twilio = require('./twilio-js');
Twilio.AccountSid = "AC57534a35a8284eb49d22b122fb5be0e6";
Twilio.AuthToken = "bc80e083030a3c14638923c335c82854";
Twilio.UsageRecord.all(function(err, res) {
res.usageRecords.forEach(function(record) {
// have at it!
});
});
@crtr0
crtr0 / gist:4197917
Created December 3, 2012 20:51
Initializing highcharts.js
var chart;
// Attach a handler to the window load event.
$(document).ready(function() {
chart = new Highcharts.Chart({
chart: {
renderTo: 'chart',
type: 'bar'
},
title: {
@crtr0
crtr0 / gist:4197923
Created December 3, 2012 20:52
Loads an event from CouchDB based on the "shortname" parameter in the URL
var event = function(req, res){
events.findBy('shortname', req.params.shortname, function(err, event) {
if (event) {
// remove sensitive data
event.voteoptions.forEach(function(vo){
delete vo.numbers;
});
res.render('event', {
name: event.name, shortname: event.shortname, state: event.state,
@crtr0
crtr0 / gist:4197929
Created December 3, 2012 20:53
JS array of vote option objects
[
{
"id": 1,
"name": "foo",
"votes": 0
},
{
"id": 2,
"name": "bar",
"votes": 0
@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);
@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: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: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: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: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);
});