Skip to content

Instantly share code, notes, and snippets.

@FreakTheMighty
Last active January 20, 2017 17:42
Show Gist options
  • Save FreakTheMighty/e4d191a12f7ae83647cfbe77a56b845f to your computer and use it in GitHub Desktop.
Save FreakTheMighty/e4d191a12f7ae83647cfbe77a56b845f to your computer and use it in GitHub Desktop.
Congress API
var request = require('request');
var moment = require('moment');
var parseString = require('xml2js').parseString;
var CONGRESS_XML = 'http://www.senate.gov/general/contact_information/senators_cfm.xml';
var CACHE_KEY = 'contacts';
module['exports'] = function helloWorld(hook) {
store = hook.datastore;
store.get(CACHE_KEY, function(err, cachedContacts) {
var duration;
if (cachedContacts) {
var cachedOn = moment(cachedContacts.cachedOn);
duration = moment.duration(cachedOn.diff(moment()));
}
if (duration && duration.asHours() < 1) {
hook.res.json(cachedContacts);
} else {
request(CONGRESS_XML, function (error, response, body) {
if (!error && response.statusCode == 200) {
parseString(body, {explicitArray: false}, function (err, senateContacts) {
senateContacts.cachedOn = new Date();
store.set(CACHE_KEY, senateContacts, function (err) {
hook.res.json(senateContacts);
});
});
}
});
}
});
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment