Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
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