Skip to content

Commit

Permalink
Use promised request for bitcoincharts module
Browse files Browse the repository at this point in the history
  • Loading branch information
selaux committed May 13, 2014
1 parent b1e8f58 commit 2db5777
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 60 deletions.
22 changes: 7 additions & 15 deletions lib/modules/markets/bitcoincharts.js
@@ -1,6 +1,6 @@
'use strict';

var request = require('request'),
var request = require('../../utils/request'),
_ = require('lodash'),

Module = require('../../Module'),
Expand Down Expand Up @@ -38,26 +38,18 @@ module.exports = Module.extend({
updateMarketStats: function () {
var self = this;

request({
uri: 'http://api.bitcoincharts.com/v1/markets.json',
json: true
}, function (err, res) {
if (err) {
self.app.logger.info('%s - error fetching markets from bitcoincharts.com', self.id, err);
return;
}
self.app.logger.debug('%s - fetched markets from bitcoincharts.com', self.id, JSON.stringify(res.statusCode), JSON.stringify(res.body));
if (res.statusCode !== 200) {
return;
}
request('http://api.bitcoincharts.com/v1/markets.json').then(function (response) {
self.app.logger.debug('%s - fetched markets from bitcoincharts.com', self.id, JSON.stringify(response));

var market = _.filter(res.body, function (market) {
var market = _.find(response, function (market) {
return market.symbol === self.config.symbol;
})[0];
});

if (market) {
self.set(_.clone(market));
}
}).catch(function (err) {
self.app.logger.info('%s - error fetching markets from bitcoincharts.com', self.id, err.toString());
});
},

Expand Down
1 change: 0 additions & 1 deletion package.json
Expand Up @@ -45,7 +45,6 @@
"nodemailer": "0.6.3",
"nomnom": "1.6.2",
"numeral": "1.5.3",
"request": "2.34.0",
"rickshaw": "1.4.6",
"serve-favicon": "2.0.0",
"socket.io": "0.9.16",
Expand Down
77 changes: 33 additions & 44 deletions test/specs/lib/modules/market/bitcoinchartsSpec.js
Expand Up @@ -6,6 +6,7 @@ var chai = require('chai'),
expect = chai.expect,
_ = require('lodash'),
SandboxedModule = require('sandboxed-module'),
Bluebird = require('bluebird'),

bitcoinChartsAnswer = [
{
Expand All @@ -20,30 +21,23 @@ var chai = require('chai'),
bid: 13,
close: 12
}
],
Bitcoincharts = SandboxedModule.require('../../../../../lib/modules/markets/bitcoincharts', {
requires: {
'request': function (options, callback) {
expect(options).to.deep.equal({
uri: 'http://api.bitcoincharts.com/v1/markets.json',
json: true
});
setTimeout(function () {
callback(null, {
statusCode: 200,
body: bitcoinChartsAnswer
});
}, 20);
}
}
});
];

chai.use(sinonChai);

describe('modules/market/bitcoincharts', function () {
var app;
var app,
Bitcoincharts,
requestStub;

beforeEach(function () {
requestStub = sinon.stub();
requestStub.withArgs('http://api.bitcoincharts.com/v1/markets.json').returns(Bluebird.resolve(bitcoinChartsAnswer));
Bitcoincharts = SandboxedModule.require('../../../../../lib/modules/markets/bitcoincharts', {
requires: {
'../../utils/request': requestStub
}
});
app = { logger: { debug: sinon.stub(), info: sinon.stub() } };
});

Expand All @@ -54,48 +48,43 @@ describe('modules/market/bitcoincharts', function () {
bitcoincharts = new Bitcoincharts(app, config);

bitcoincharts.on('change', function () {
expect(app.logger.debug).to.have.been.calledOnce;
expect(app.logger.debug).to.have.been.calledWith(
'%s - fetched markets from bitcoincharts.com',
bitcoincharts.id,
'200',
JSON.stringify(bitcoinChartsAnswer)
);

expect(_.omit(bitcoincharts.toJSON(), 'historicalData')).to.deep.equal({
symbol: 'wantedSymbol',
ask: 14,
bid: 13,
close: 12
setImmediate(function () {
expect(app.logger.debug).to.have.been.calledOnce;
expect(app.logger.debug).to.have.been.calledWith(
'%s - fetched markets from bitcoincharts.com',
bitcoincharts.id,
JSON.stringify(bitcoinChartsAnswer)
);

expect(_.omit(bitcoincharts.toJSON(), 'historicalData')).to.deep.equal({
symbol: 'wantedSymbol',
ask: 14,
bid: 13,
close: 12
});
done();
});
done();
});
});

it('it should handle errors that occur during the request', function (done) {
var err = new Error('Test Error'),
Bitcoincharts = SandboxedModule.require('../../../../../lib/modules/markets/bitcoincharts', {
requires: {
'request': function (options, callback) {
setTimeout(function () {
callback(err);
}, 20);
}
}
}),
bitcoincharts = new Bitcoincharts(app, {});
bitcoincharts;

requestStub.withArgs('http://api.bitcoincharts.com/v1/markets.json').returns(Bluebird.reject(err));

bitcoincharts = new Bitcoincharts(app, {});
setTimeout(function () {
expect(app.logger.info).to.have.been.calledOnce;
expect(app.logger.info).to.have.been.calledWith(
'%s - error fetching markets from bitcoincharts.com',
bitcoincharts.id,
err
err.toString()
);

expect(bitcoincharts.toJSON()).to.be.empty;
done();
}, 50);
}, 10);
});

it('it should handle non 200 status codes', function (done) {
Expand Down

0 comments on commit 2db5777

Please sign in to comment.