Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Closes #20: add cryptocoincharts as market module
- Loading branch information
Showing
5 changed files
with
170 additions
and
21 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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) | ||
|
||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
106 changes: 106 additions & 0 deletions
106
test/specs/lib/modules/market/cryptocoinchartsInfoSpec.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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); | ||
}); | ||
}); | ||
|
||
}); |