Last active
October 10, 2015 22:27
-
-
Save andrewtlove/7f35e05ac39db2530470 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
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