Added currency controller and endpoint.

This commit is contained in:
Braydon Fuller 2015-10-02 12:01:06 -04:00
parent df574bfffd
commit f96d8f3f8b
4 changed files with 169 additions and 20 deletions

45
lib/currency.js Normal file
View File

@ -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;

View File

@ -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() {

View File

@ -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"
}

110
test/currency.js Normal file
View File

@ -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);
});
});