flosight-api/lib/currency.js
2018-05-18 15:22:16 -07:00

46 lines
1.1 KiB
JavaScript

'use strict';
var request = require('request');
function CurrencyController(options) {
this.node = options.node;
var refresh = options.currencyRefresh || CurrencyController.DEFAULT_CURRENCY_DELAY;
this.currencyDelay = refresh * 60000;
this.bitstampRate = 0;
this.timestamp = Date.now();
}
CurrencyController.DEFAULT_CURRENCY_DELAY = 10;
CurrencyController.prototype.index = function(req, res) {
var self = this;
var currentTime = Date.now();
if (self.bitstampRate === 0 || currentTime >= (self.timestamp + self.currencyDelay)) {
self.timestamp = currentTime;
request('https://api.coinmarketcap.com/v1/ticker/florincoin/', function(err, response, body) {
if (err) {
self.node.log.error(err);
}
if (!err && response.statusCode === 200) {
self.bitstampRate = parseFloat(JSON.parse(body)[0].price_usd);
}
res.jsonp({
status: 200,
data: {
bitstamp: self.bitstampRate
}
});
});
} else {
res.jsonp({
status: 200,
data: {
bitstamp: self.bitstampRate
}
});
}
};
module.exports = CurrencyController;