separate initialization from cron job

This commit is contained in:
Ivan Socolsky 2016-01-11 17:10:24 -03:00
parent 8d6d545139
commit aac0b70df4
3 changed files with 44 additions and 21 deletions

View File

@ -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) {

View File

@ -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;

View File

@ -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() {