separate initialization from cron job
This commit is contained in:
parent
8d6d545139
commit
aac0b70df4
|
@ -18,16 +18,11 @@ var FETCH_INTERVAL = 15; // In minutes
|
||||||
|
|
||||||
function FiatRateService() {};
|
function FiatRateService() {};
|
||||||
|
|
||||||
FiatRateService.prototype.start = function(opts, cb) {
|
FiatRateService.prototype.init = function(opts, cb) {
|
||||||
var self = this;
|
var self = this;
|
||||||
|
|
||||||
opts = opts || {};
|
opts = opts || {};
|
||||||
|
|
||||||
if (_.isArray(opts.providers)) {
|
|
||||||
self.providers = opts.providers;
|
|
||||||
} else {
|
|
||||||
self.providers = _.values(require('./fiatrateproviders'));
|
|
||||||
}
|
|
||||||
self.request = opts.request || request;
|
self.request = opts.request || request;
|
||||||
self.defaultProvider = opts.defaultProvider || DEFAULT_PROVIDER;
|
self.defaultProvider = opts.defaultProvider || DEFAULT_PROVIDER;
|
||||||
|
|
||||||
|
@ -45,21 +40,29 @@ FiatRateService.prototype.start = function(opts, cb) {
|
||||||
], function(err) {
|
], function(err) {
|
||||||
if (err) {
|
if (err) {
|
||||||
log.error(err);
|
log.error(err);
|
||||||
return cb(err);
|
|
||||||
}
|
}
|
||||||
|
return cb(err);
|
||||||
var interval = opts.fetchInterval || FETCH_INTERVAL;
|
|
||||||
if (interval) {
|
|
||||||
self._fetch();
|
|
||||||
setInterval(function() {
|
|
||||||
self._fetch();
|
|
||||||
}, interval * 60 * 1000);
|
|
||||||
}
|
|
||||||
|
|
||||||
return cb();
|
|
||||||
});
|
});
|
||||||
};
|
};
|
||||||
|
|
||||||
|
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) {
|
FiatRateService.prototype._fetch = function(cb) {
|
||||||
var self = this;
|
var self = this;
|
||||||
|
|
||||||
|
@ -112,7 +115,7 @@ FiatRateService.prototype.getRate = function(code, opts, cb) {
|
||||||
|
|
||||||
opts = opts || {};
|
opts = opts || {};
|
||||||
|
|
||||||
var provider = opts.provider || DEFAULT_PROVIDER;
|
var provider = opts.provider || self.defaultProvider;
|
||||||
var ts = opts.ts || Date.now();
|
var ts = opts.ts || Date.now();
|
||||||
|
|
||||||
async.map([].concat(ts), function(ts, cb) {
|
async.map([].concat(ts), function(ts, cb) {
|
||||||
|
|
|
@ -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 = WalletService;
|
||||||
module.exports.ClientError = ClientError;
|
module.exports.ClientError = ClientError;
|
||||||
|
|
|
@ -14,7 +14,7 @@ var helpers = require('./helpers');
|
||||||
|
|
||||||
var FiatRateService = require('../../lib/fiatrateservice');
|
var FiatRateService = require('../../lib/fiatrateservice');
|
||||||
|
|
||||||
describe('Fiat rate service', function() {
|
describe.only('Fiat rate service', function() {
|
||||||
var service, request;
|
var service, request;
|
||||||
|
|
||||||
before(function(done) {
|
before(function(done) {
|
||||||
|
@ -28,10 +28,13 @@ describe('Fiat rate service', function() {
|
||||||
service = new FiatRateService();
|
service = new FiatRateService();
|
||||||
request = sinon.stub();
|
request = sinon.stub();
|
||||||
request.get = sinon.stub();
|
request.get = sinon.stub();
|
||||||
service.start({
|
service.init({
|
||||||
storage: helpers.getStorage(),
|
storage: helpers.getStorage(),
|
||||||
request: request,
|
request: request,
|
||||||
}, done);
|
}, function(err) {
|
||||||
|
should.not.exist(err);
|
||||||
|
service.startCron({}, done);
|
||||||
|
});
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
describe('#getRate', function() {
|
describe('#getRate', function() {
|
||||||
|
|
Loading…
Reference in New Issue