From f96d8f3f8b7e0a1d0f2ade0148e7e5d91e3a3d22 Mon Sep 17 00:00:00 2001 From: Braydon Fuller Date: Fri, 2 Oct 2015 12:01:06 -0400 Subject: [PATCH] Added currency controller and endpoint. --- lib/currency.js | 45 +++++++++++++++++++ lib/index.js | 26 ++++------- package.json | 8 ++-- test/currency.js | 110 +++++++++++++++++++++++++++++++++++++++++++++++ 4 files changed, 169 insertions(+), 20 deletions(-) create mode 100644 lib/currency.js create mode 100644 test/currency.js diff --git a/lib/currency.js b/lib/currency.js new file mode 100644 index 0000000..d01c3cb --- /dev/null +++ b/lib/currency.js @@ -0,0 +1,45 @@ +'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 = + new Date(); +} + +CurrencyController.DEFAULT_CURRENCY_DELAY = 10; + +CurrencyController.prototype.index = function(req, res) { + var self = this; + var currentTime = + new Date(); + if (self.bitstampRate === 0 || currentTime >= (self.timestamp + self.currencyDelay)) { + self.timestamp = currentTime; + request('https://www.bitstamp.net/api/ticker/', function(err, response, body) { + if (err) { + self.node.log.error(err); + } + if (!err && response.statusCode === 200) { + self.bitstampRate = parseFloat(JSON.parse(body).last); + } + res.jsonp({ + status: 200, + data: { + bitstamp: self.bitstampRate + } + }); + }); + } else { + res.jsonp({ + status: 200, + data: { + bitstamp: self.bitstampRate + } + }); + } + +}; + +module.exports = CurrencyController; diff --git a/lib/index.js b/lib/index.js index 424d78a..fa3852a 100644 --- a/lib/index.js +++ b/lib/index.js @@ -8,6 +8,7 @@ var AddressController = require('./addresses'); var StatusController = require('./status'); var MessagesController = require('./messages'); var UtilsController = require('./utils'); +var CurrencyController = require('./currency'); var bitcore = require('bitcore'); var $ = bitcore.util.preconditions; var Transaction = bitcore.Transaction; @@ -16,6 +17,9 @@ var EventEmitter = require('events').EventEmitter; var InsightAPI = function(options) { BaseService.call(this, options); + // in minutes + this.currencyRefresh = options.currencyRefresh || CurrencyController.DEFAULT_CURRENCY_DELAY; + this.subscriptions = { inv: [] }; @@ -89,24 +93,12 @@ InsightAPI.prototype.setupRoutes = function(app) { app.get('/utils/estimatefee', utils.estimateFee.bind(utils)); // Currency - /*var currency = require('../app/controllers/currency'); - app.get(apiPrefix + '/currency', currency.index); + var currency = new CurrencyController({ + node: this.node, + currencyRefresh: this.currencyRefresh + }); + app.get('/currency', currency.index.bind(currency)); - // Email store plugin - if (config.enableEmailstore) { - var emailPlugin = require('../plugins/emailstore'); - app.get(apiPrefix + '/email/retrieve', emailPlugin.retrieve); - } - - // Currency rates plugin - if (config.enableCurrencyRates) { - var currencyRatesPlugin = require('../plugins/currencyrates'); - app.get(apiPrefix + '/rates/:code', currencyRatesPlugin.getRate); - } - - //Home route - var index = require('../app/controllers/index'); - app.get('*', index.render);*/ }; InsightAPI.prototype.getPublishEvents = function() { diff --git a/package.json b/package.json index 8dd9212..239078e 100644 --- a/package.json +++ b/package.json @@ -61,13 +61,15 @@ "bitcoreNode": "lib", "dependencies": { "async": "*", - "lodash": "^2.4.1", "bitcore": "^0.13.3", - "bitcore-message": "^0.12.0" + "bitcore-message": "^0.12.0", + "lodash": "^2.4.1", + "request": "^2.64.0" }, "devDependencies": { - "mocha": "~1.16.2", "chai": "*", + "mocha": "~1.16.2", + "proxyquire": "^1.7.2", "should": "^2.1.1", "sinon": "^1.10.3" } diff --git a/test/currency.js b/test/currency.js new file mode 100644 index 0000000..318ac34 --- /dev/null +++ b/test/currency.js @@ -0,0 +1,110 @@ +'use strict'; + +var should = require('should'); +var sinon = require('sinon'); +var proxyquire = require('proxyquire'); +var CurrencyController = require('../lib/currency'); + +describe('Currency', function() { + + var bitstampData = { + high: 239.44, + last: 237.90, + timestamp: 1443798711, + bid: 237.61, + vwap: 237.88, + volume: 21463.27736401, + low: 235.00, + ask: 237.90 + }; + + it.skip('will make live request to bitstamp', function(done) { + var currency = new CurrencyController({}); + var req = {}; + var res = { + jsonp: function(response) { + response.status.should.equal(200); + should.exist(response.data.bitstamp); + (typeof response.data.bitstamp).should.equal('number'); + done(); + } + }; + currency.index(req, res); + }); + + it('will retrieve a fresh value', function(done) { + var TestCurrencyController = proxyquire('../lib/currency', { + request: sinon.stub().callsArgWith(1, null, {statusCode: 200}, JSON.stringify(bitstampData)) + }); + var node = { + log: { + error: sinon.stub() + } + }; + var currency = new TestCurrencyController({node: node}); + currency.bitstampRate = 220.20; + currency.timestamp = new Date() - 61000 * CurrencyController.DEFAULT_CURRENCY_DELAY; + var req = {}; + var res = { + jsonp: function(response) { + response.status.should.equal(200); + should.exist(response.data.bitstamp); + response.data.bitstamp.should.equal(237.90); + done(); + } + }; + currency.index(req, res); + }); + + it('will log an error from request', function(done) { + var TestCurrencyController = proxyquire('../lib/currency', { + request: sinon.stub().callsArgWith(1, new Error('test')) + }); + var node = { + log: { + error: sinon.stub() + } + }; + var currency = new TestCurrencyController({node: node}); + currency.bitstampRate = 237.90; + currency.timestamp = new Date() - 65000 * CurrencyController.DEFAULT_CURRENCY_DELAY; + var req = {}; + var res = { + jsonp: function(response) { + response.status.should.equal(200); + should.exist(response.data.bitstamp); + response.data.bitstamp.should.equal(237.90); + node.log.error.callCount.should.equal(1); + done(); + } + }; + currency.index(req, res); + }); + + it('will retrieve a cached value', function(done) { + var request = sinon.stub(); + var TestCurrencyController = proxyquire('../lib/currency', { + request: request + }); + var node = { + log: { + error: sinon.stub() + } + }; + var currency = new TestCurrencyController({node: node}); + currency.bitstampRate = 237.90; + currency.timestamp = + new Date(); + var req = {}; + var res = { + jsonp: function(response) { + response.status.should.equal(200); + should.exist(response.data.bitstamp); + response.data.bitstamp.should.equal(237.90); + request.callCount.should.equal(0); + done(); + } + }; + currency.index(req, res); + }); + +});