Skip to content

Instantly share code, notes, and snippets.

@andrewtlove
Last active October 10, 2015 22:27
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save andrewtlove/7f35e05ac39db2530470 to your computer and use it in GitHub Desktop.
Save andrewtlove/7f35e05ac39db2530470 to your computer and use it in GitHub Desktop.
module['exports'] = function (hook) {
var rest = require('restler'),
moment = require('moment');
var myGender = 'male',
myCountry = 'United States',
myDOB = '1986-05-01';
var currentDate = moment();
var numerousAPI = '',
numerousAPI = 'Basic ' + hook.env.numerousAPIKey;
rest.get(
'http://api.population.io:80/1.0/life-expectancy/total/'
+ myGender + '/'
+ escape(myCountry) + '/'
+ myDOB + '/')
.on('complete', function(data) {
lifeExpectancyDays = (data.total_life_expectancy * 365.25);
lifeLivedDays = Math.round( moment.duration( currentDate.diff( myDOB ) ).as('days'));
percentLifeLeft = 100 - (Math.round((lifeLivedDays / lifeExpectancyDays) * 10000) / 100);
rest.post(
'https://api.numerousapp.com/v2/metrics/2955241375194482323/events',
{
headers: {'Authorization': numerousAPI},
data: '{"value": ' + percentLifeLeft + ', "onlyIfChanged":' + true + '}'
})
.on('complete', function(data, response){
console.log(response.statusCode + ": " + response.statusMessage);
hook.res.end();
});
})
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment