diff --git a/lib/fiatrateservice.js b/lib/fiatrateservice.js index 7cc6e52..5b9f4ae 100644 --- a/lib/fiatrateservice.js +++ b/lib/fiatrateservice.js @@ -18,16 +18,11 @@ var FETCH_INTERVAL = 15; // In minutes function FiatRateService() {}; -FiatRateService.prototype.start = function(opts, cb) { +FiatRateService.prototype.init = function(opts, cb) { var self = this; opts = opts || {}; - if (_.isArray(opts.providers)) { - self.providers = opts.providers; - } else { - self.providers = _.values(require('./fiatrateproviders')); - } self.request = opts.request || request; self.defaultProvider = opts.defaultProvider || DEFAULT_PROVIDER; @@ -45,21 +40,29 @@ FiatRateService.prototype.start = function(opts, cb) { ], function(err) { if (err) { log.error(err); - return cb(err); } - - var interval = opts.fetchInterval || FETCH_INTERVAL; - if (interval) { - self._fetch(); - setInterval(function() { - self._fetch(); - }, interval * 60 * 1000); - } - - return cb(); + return cb(err); }); }; +FiatRateService.prototype.startCron = function(opts, cb) { + var self = this; + + opts = opts || {}; + + self.providers = _.values(require('./fiatrateproviders')); + + var interval = opts.fetchInterval || FETCH_INTERVAL; + if (interval) { + self._fetch(); + setInterval(function() { + self._fetch(); + }, interval * 60 * 1000); + } + + return cb(); +}; + FiatRateService.prototype._fetch = function(cb) { var self = this; @@ -112,7 +115,7 @@ FiatRateService.prototype.getRate = function(code, opts, cb) { opts = opts || {}; - var provider = opts.provider || DEFAULT_PROVIDER; + var provider = opts.provider || self.defaultProvider; var ts = opts.ts || Date.now(); async.map([].concat(ts), function(ts, cb) { diff --git a/lib/server.js b/lib/server.js index 0ab90cf..b0133c5 100644 --- a/lib/server.js +++ b/lib/server.js @@ -2365,6 +2365,23 @@ WalletService.prototype.startScan = function(opts, cb) { }); }; +/** + * Returns exchange rate for the specified currency & timestamp. + * @param {Object} opts + * @param {string} opts.code - Currency ISO code. + * @param {Date} [opts.ts] - A timestamp to base the rate on (default Date.now()). + * @param {String} [opts.provider] - A provider of exchange rates (default 'BitPay'). + * @returns {Object} rates - The exchange rate. + */ +WalletService.prototype.getRate = function(opts, cb) { + var self = this; + + if (!Utils.checkRequired(opts, ['code'])) + return cb(new ClientError('Required argument missing')); + + +}; + module.exports = WalletService; module.exports.ClientError = ClientError; diff --git a/test/integration/fiatrateservice.js b/test/integration/fiatrateservice.js index 98420f0..5fbd1db 100644 --- a/test/integration/fiatrateservice.js +++ b/test/integration/fiatrateservice.js @@ -14,7 +14,7 @@ var helpers = require('./helpers'); var FiatRateService = require('../../lib/fiatrateservice'); -describe('Fiat rate service', function() { +describe.only('Fiat rate service', function() { var service, request; before(function(done) { @@ -28,10 +28,13 @@ describe('Fiat rate service', function() { service = new FiatRateService(); request = sinon.stub(); request.get = sinon.stub(); - service.start({ + service.init({ storage: helpers.getStorage(), request: request, - }, done); + }, function(err) { + should.not.exist(err); + service.startCron({}, done); + }); }); }); describe('#getRate', function() {