Skip to content

Commit

Permalink
Closes #20: add cryptocoincharts as market module
Browse files Browse the repository at this point in the history
  • Loading branch information
selaux committed Jun 10, 2014
1 parent d0f3e4c commit 76b0f97
Show file tree
Hide file tree
Showing 5 changed files with 170 additions and 21 deletions.
49 changes: 49 additions & 0 deletions lib/modules/markets/cryptocoinchartsInfo.js
@@ -0,0 +1,49 @@
'use strict';

var request = require('../../utils/request'),
Module = require('../../Module'),
setWithHistoricalData = require('../../utils/setWithHistoricalData');

module.exports = Module.extend({
defaults: {
interval: 60 * 60 * 1e3,
tradingPair: 'btc_usd',
chartTimespan: 14 * 24 * 60 * 60 * 1000,
chartPrecision: 60 * 60 * 1000
},

viewId: 'market',

initialize: function () {
var self = this;

self.title = self.config.tradingPair + ' Market @ Cryptocoincharts.info';

self.interval = setInterval(function () {
self.updateMarketStats();
}, self.config.interval);
self.updateMarketStats();
},

updateMarketStats: function () {
var self = this;

request('http://www.cryptocoincharts.info/v2/api/tradingPair/' + self.config.tradingPair).then(function (response) {
/*jshint sub:true*/

self.app.logger.debug('%s - fetched markets from cryptocoincharts.info', self.id, JSON.stringify(response));


self.set({
close: parseFloat(response.price),
currency: response.id.split('/')[1].toUpperCase(),
bestMarket: response['best_market']
});
}).catch(function (err) {
self.app.logger.info('%s - error fetching markets from cryptocoincharts.info', self.id, err.toString());
});
},

set: setWithHistoricalData([ 'close' ], Module.prototype.set)

});
8 changes: 6 additions & 2 deletions lib/views/mixins/renderHistoricalDataGraph.js
Expand Up @@ -16,7 +16,11 @@ module.exports = function (attributes, element, graphOptions, hoverOptions) {
getSeries: function () {
var self = this;

return _.map(attributes, function (attr) {
return _(attributes).filter(function (attr) {
return _.any(self.module.get('historicalData'), function (measurement) {
return measurement[attr.attr] !== undefined;
});
}).map(function (attr) {
return _.extend({
color: '#cae2f7',
data: self.module.get('historicalData').map(function (measurement) {
Expand All @@ -26,7 +30,7 @@ module.exports = function (attributes, element, graphOptions, hoverOptions) {
};
})
}, attr);
});
}).value();
},

initializeGraph: function () {
Expand Down
11 changes: 9 additions & 2 deletions templates/market.hbs
Expand Up @@ -8,8 +8,15 @@
</div>
<div class="col-xs-12 col-md-3">
<table class="table table-bordered">
<tr><td>Best Bid</td><td>{{number bid}} {{currency}}</td></tr>
<tr><td>Best Ask</td><td>{{number ask}} {{currency}}</td></tr>
{{#if bid}}
<tr><td>Best Bid</td><td>{{number bid}} {{currency}}</td></tr>
{{/if}}
{{#if ask}}
<tr><td>Best Ask</td><td>{{number ask}} {{currency}}</td></tr>
{{/if}}
{{#if bestMarket}}
<tr><td>Best Market</td><td>{{bestMarket}}</td></tr>
{{/if}}
</table>
</div>
</div>
Expand Down
17 changes: 0 additions & 17 deletions test/specs/lib/modules/market/bitcoinchartsSpec.js
Expand Up @@ -81,23 +81,6 @@ describe('modules/market/bitcoincharts', function () {
}, 10);
});

it('it should handle non 200 status codes', function (done) {
var Bitcoincharts = SandboxedModule.require('../../../../../lib/modules/markets/bitcoincharts', {
requires: {
'request': function (options, callback) {
setTimeout(function () {
callback(null, { statusCode: 500 });
}, 20);
}
}
}),
bitcoincharts = new Bitcoincharts(app, {});

setTimeout(function () {
expect(bitcoincharts.toJSON()).to.be.empty;
done();
}, 50);
});

it('should set the title correctly', function () {
var config = {
Expand Down
106 changes: 106 additions & 0 deletions test/specs/lib/modules/market/cryptocoinchartsInfoSpec.js
@@ -0,0 +1,106 @@
'use strict';

var _ = require('lodash'),
SandboxedModule = require('sandboxed-module'),
Bluebird = require('bluebird'),

cryptocoinchartsAnswer = {
'id': 'btc/eur',
'price': '484.64000000',
'price_before_24h': '485.82000000',
'volume_first': '1096.24353432283',
'volume_second': '522494.62242794',
'volume_btc': '1096.24',
'best_market': 'hitbtc',
'latest_trade': '2014-06-09 15:49:12'
};

describe('modules/market/cryptocoinchartsInfo', function () {
var app,
Cryptocoincharts,
requestStub;

beforeEach(function () {
requestStub = sinon.stub();
requestStub.withArgs('http://www.cryptocoincharts.info/v2/api/tradingPair/btc_usd').returns(Bluebird.resolve(cryptocoinchartsAnswer));
Cryptocoincharts = SandboxedModule.require('../../../../../lib/modules/markets/cryptocoinchartsInfo', {
requires: {
'../../utils/request': requestStub
}
});
app = { logger: { debug: sinon.stub(), info: sinon.stub() } };
});

it('should get data from cryptocoincharts correctly', function (done) {
var config = {
tradingPair: 'btc_eur'
},
cryptocoincharts;

requestStub.withArgs('http://www.cryptocoincharts.info/v2/api/tradingPair/btc_eur').returns(Bluebird.resolve(cryptocoinchartsAnswer));

cryptocoincharts = new Cryptocoincharts(app, config);
cryptocoincharts.on('change', function () {
setImmediate(function () {
expect(app.logger.debug).to.have.been.calledOnce;
expect(app.logger.debug).to.have.been.calledWith(
'%s - fetched markets from cryptocoincharts.info',
cryptocoincharts.id,
JSON.stringify(cryptocoinchartsAnswer)
);

expect(_.omit(cryptocoincharts.toJSON(), 'historicalData')).to.deep.equal({
currency: 'EUR',
close: 484.64,
bestMarket: 'hitbtc'
});
done();
});
});
});

it('it should handle errors that occur during the request', function (done) {
var err = new Error('Test Error'),
cryptocoincharts;

requestStub.withArgs('http://www.cryptocoincharts.info/v2/api/tradingPair/btc_usd').returns(Bluebird.reject(err));

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

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

it('should set the title correctly', function () {
var config = {
tradingPair: 'ltc_btc'
},
cryptocoincharts;

requestStub.withArgs('http://www.cryptocoincharts.info/v2/api/tradingPair/ltc_btc').returns(Bluebird.resolve(cryptocoinchartsAnswer));

cryptocoincharts = new Cryptocoincharts(app, config);
expect(cryptocoincharts.title).to.equal('ltc_btc Market @ Cryptocoincharts.info');
});

describe('set', function () {
it('should add the bid, ask and close values to historicalData', function () {
var cryptocoincharts = new Cryptocoincharts(app, {}),
now = new Date().getTime();

cryptocoincharts.set({ close: 3 });
expect(cryptocoincharts.get('historicalData')).to.have.length(1);
expect(cryptocoincharts.get('historicalData')[0].close).to.equal(3);
expect(cryptocoincharts.get('historicalData')[0].timestamp).to.be.within(now-1, now+1);
});
});

});

0 comments on commit 76b0f97

Please sign in to comment.