diff --git a/lib/modules/markets/cryptocoinchartsInfo.js b/lib/modules/markets/cryptocoinchartsInfo.js new file mode 100644 index 0000000..5fa9885 --- /dev/null +++ b/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) + +}); \ No newline at end of file diff --git a/lib/views/mixins/renderHistoricalDataGraph.js b/lib/views/mixins/renderHistoricalDataGraph.js index 1ee2a53..8d96ab4 100644 --- a/lib/views/mixins/renderHistoricalDataGraph.js +++ b/lib/views/mixins/renderHistoricalDataGraph.js @@ -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) { @@ -26,7 +30,7 @@ module.exports = function (attributes, element, graphOptions, hoverOptions) { }; }) }, attr); - }); + }).value(); }, initializeGraph: function () { diff --git a/templates/market.hbs b/templates/market.hbs index a80021d..118e00a 100644 --- a/templates/market.hbs +++ b/templates/market.hbs @@ -8,8 +8,15 @@
- - + {{#if bid}} + + {{/if}} + {{#if ask}} + + {{/if}} + {{#if bestMarket}} + + {{/if}}
Best Bid{{number bid}} {{currency}}
Best Ask{{number ask}} {{currency}}
Best Bid{{number bid}} {{currency}}
Best Ask{{number ask}} {{currency}}
Best Market{{bestMarket}}
diff --git a/test/specs/lib/modules/market/bitcoinchartsSpec.js b/test/specs/lib/modules/market/bitcoinchartsSpec.js index 6e27c6a..f7f500f 100644 --- a/test/specs/lib/modules/market/bitcoinchartsSpec.js +++ b/test/specs/lib/modules/market/bitcoinchartsSpec.js @@ -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 = { diff --git a/test/specs/lib/modules/market/cryptocoinchartsInfoSpec.js b/test/specs/lib/modules/market/cryptocoinchartsInfoSpec.js new file mode 100644 index 0000000..9f007f5 --- /dev/null +++ b/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); + }); + }); + +}); \ No newline at end of file