From 8fe5b97004189845ee98a02ab197ca080ff0a002 Mon Sep 17 00:00:00 2001 From: Adam Soltys Date: Sat, 30 Aug 2014 12:13:43 -0700 Subject: Reimplement fetch rates function --- app.js | 28 ++++++++++++---------------- 1 file changed, 12 insertions(+), 16 deletions(-) (limited to 'app.js') diff --git a/app.js b/app.js index b568e65a..9e5c3fed 100644 --- a/app.js +++ b/app.js @@ -88,24 +88,20 @@ return next(); }; - fetchRates = function() { - var file; - file = 'public/js/rates.json'; - fs.truncate(file, 0, function() { - var r, stream; - stream = fs.createWriteStream(file); - r = request("https://api.bitcoinaverage.com/exchanges/all"); - r.on('data', function(chunk) { - return stream.write(chunk); - }); - return r.on('error', function(error) { - return console.log(error); - }); + (fetchRates = function() { + request("https://api.bitcoinaverage.com/exchanges/all", function(error, response, body) { + var file, stream; + try { + require('util').isDate(JSON.parse(body).timestamp); + file = 'public/js/rates.json'; + stream = fs.createWriteStream(file); + return fs.truncate(file, 0, function() { + return stream.write(body); + }); + } catch (_error) {} }); return setTimeout(fetchRates, 120000); - }; - - fetchRates(); + })(); app.get('/', cache, sessions["new"]); -- cgit v1.2.3