Skip to content

Instantly share code, notes, and snippets.

@Marak
Forked from andrewtlove/lifeMeter.js
Created October 10, 2015 22:27
Show Gist options
  • Select an option

  • Save Marak/44c786b87bb8d0c453a8 to your computer and use it in GitHub Desktop.

Select an option

Save Marak/44c786b87bb8d0c453a8 to your computer and use it in GitHub Desktop.

Revisions

  1. @andrewtlove andrewtlove revised this gist Oct 10, 2015. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion lifeMeter.js
    Original file line number Diff line number Diff line change
    @@ -23,7 +23,7 @@ module['exports'] = function (hook) {

    .on('complete', function(data) {

    lifeExpectancyDays = (data.total_life_expectancy * 364.25);
    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);

  2. @andrewtlove andrewtlove created this gist Oct 10, 2015.
    45 changes: 45 additions & 0 deletions lifeMeter.js
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,45 @@
    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 * 364.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();
    });


    })


    };