Last active
December 22, 2015 18:09
-
-
Save pspeter3/6511394 to your computer and use it in GitHub Desktop.
Status Board Foursquare Server written in pure node.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* Status Board Foursquare Server | |
*/ | |
var http = require('http'); | |
var https = require('https'); | |
var querystring = require('querystring'); | |
var util = require('util'); | |
/** | |
* Configuration for the server | |
*/ | |
var PORT = process.env.PORT; | |
var TOKEN = process.env.FOURSQUARE_TOKEN; | |
var IDS = process.env.FOURSQUARE_IDS.split(',').reduce(function(a, e) { | |
a[e] = true; | |
return a; | |
}, {}); | |
var HEADER = [process.env.HEADER]; | |
/** | |
* Time Constants | |
*/ | |
var MINUTE = 1000 * 60; | |
var HOUR = 60 * MINUTE; | |
var DAY = 24 * HOUR; | |
var WEEK = 7 * DAY; | |
var YEAR = 52 * WEEK; | |
/** | |
* Foursquare reqeust params | |
* | |
* @constant | |
* @type {Object} | |
*/ | |
var FOURSQUARE = { | |
hostname: 'api.foursquare.com', | |
path: '/v2/checkins/recent?' + querystring.stringify({ | |
oauth_token: TOKEN | |
}) | |
}; | |
/** | |
* Gets the checkins from foursquare | |
* | |
* @param {Function} callback Called with the response directly | |
*/ | |
var getCheckins = function(callback) { | |
https.get(FOURSQUARE, function(res) { | |
var buffer = ''; | |
res.on('data', function(chunk) { | |
buffer += chunk; | |
}); | |
res.on('end', function() { | |
try { | |
var data = JSON.parse(buffer); | |
callback(null, data); | |
} catch (err) { | |
callback(err); | |
} | |
}); | |
}).on('error', function(err) { | |
callback(err); | |
}); | |
}; | |
/** | |
* Checks if a checkin is from a friend | |
* | |
* @param {Checkin} checkin The checkin | |
* @return {Boolean} Whether or not it is from a friend | |
*/ | |
var isFriend = function(checkin) { | |
return IDS[checkin.user.id]; | |
}; | |
/** | |
* Returns the initial of a name | |
* | |
* @param {String} name The name | |
* @return {String} The first letter capitalized | |
*/ | |
var initialOf = function(name) { | |
return name[0].toUpperCase(); | |
}; | |
/** | |
* Converts a delta into a delta string | |
* | |
* @param {Number} delta The time delta | |
* @param {Number} constant The time constant | |
* @param {String} suffix The time suffix | |
* @return {String} The delta time string | |
*/ | |
var toDeltaString = function(delta, constant, suffix) { | |
return '' + Math.floor(delta / constant) + suffix; | |
}; | |
/** | |
* Gets the detla time string | |
* | |
* @param {Number} time The timestampe from foursquare | |
* @return {String} The string representation | |
*/ | |
var getDelta = function(time) { | |
var delta = Date.now() - (time * 1000); | |
if (delta > YEAR) { | |
return toDeltaString(delta, YEAR, 'y'); | |
} | |
if (delta > WEEK) { | |
return toDeltaString(delta, WEEK, 'w'); | |
} | |
if (delta > DAY) { | |
return toDeltaString(delta, DAY, 'd'); | |
} | |
if (delta > HOUR) { | |
return toDeltaString(delta, HOUR, 'h'); | |
} | |
if (delta > MINUTE) { | |
return toDeltaString(delta, MINUTE, 'm'); | |
} | |
return 'now'; | |
}; | |
/** | |
* Converts a checkin into a row | |
* | |
* @param {Checkin} checkin The checkin | |
* @return {String} The string representation | |
*/ | |
var toRow = function(checkin) { | |
var user = checkin.user; | |
var name = initialOf(user.firstName) + initialOf(user.lastName); | |
var venue = checkin.venue.name; | |
var time = getDelta(checkin.createdAt); | |
return util.format('"%s","%s (%s)"', name, venue, time); | |
}; | |
/** | |
* Handles requests to the server | |
* | |
* @param {Request} req | |
* @param {Response} res | |
*/ | |
var handleRequest = function(req, res) { | |
getCheckins(function(err, checkins) { | |
if (err) { | |
console.error(err); | |
res.status = 500; | |
return res.end(); | |
} | |
var rows = checkins.response.recent.filter(isFriend).map(toRow); | |
var data = HEADER.concat(rows).join('\n'); | |
res.status = 200; | |
res.setHeader('Content-Type', 'text/plain'); | |
res.end(data); | |
}); | |
}; | |
var server = http.createServer(handleRequest); | |
server.listen(PORT); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment