Skip to content

Commit

Permalink
Fix last update to display date of last Indaba fetch (#38)
Browse files Browse the repository at this point in the history
  • Loading branch information
dumyan authored and pwalsh committed Sep 2, 2016
1 parent 72e4222 commit 8854fad
Show file tree
Hide file tree
Showing 2 changed files with 5 additions and 30 deletions.
5 changes: 3 additions & 2 deletions api.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,14 +19,15 @@ function api_call (endpoint, callback) {
}
if (should_get_from_cache) {
var data = fs.readFileSync(cache_file);
callback(JSON.parse(data));
var date = new Date(stat.mtime);
callback(JSON.parse(data), date);
} else {

Indaba.getTrackerJSON().then( function (res) {
if (should_update_cache) {
fs.writeFileSync(cache_file, JSON.stringify(res));
}
callback(res);
callback(res, new Date());
});
}
}
Expand Down
30 changes: 2 additions & 28 deletions routes/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -296,20 +296,7 @@ router.get('/locale/:locale/embed', function (req, res) {
});

router.get('/', function (req, res) {
api.call('countries', function (countries) {
// If today is less than the 22nd of the month the data is from
// the last date of two months ago else it's from last date of one
// month ago
var last_update = new Date();
if (last_update.getDate() < 22) {
last_update = new Date(last_update.getFullYear(),
last_update.getMonth()-1, 0);
}
else {
last_update = new Date(last_update.getFullYear(),
last_update.getMonth(), 0);
}

api.call('countries', function (countries, last_update) {
//Override countries (e.g. discontinued countries)
countries = _.map(countries, function(obj) {
if (obj.country in country_override) {
Expand All @@ -332,20 +319,7 @@ router.get('/', function (req, res) {
});

router.get('/embed', function (req, res) {
api.call('countries', function (countries) {
// If today is less than the 22nd of the month the data is from
// the last date of two months ago else it's from last date of one
// month ago
var last_update = new Date();
if (last_update.getDate() < 22) {
last_update = new Date(last_update.getFullYear(),
last_update.getMonth()-1, 0);
}
else {
last_update = new Date(last_update.getFullYear(),
last_update.getMonth(), 0);
}

api.call('countries', function (countries, last_update) {
//Override countries (e.g. discontinued countries)
countries = _.map(countries, function(obj) {
if (obj.country in country_override) {
Expand Down

0 comments on commit 8854fad

Please sign in to comment.