From 10ac3a4d65ff006000564f480322cea39715679c Mon Sep 17 00:00:00 2001 From: Ivan Socolsky Date: Mon, 11 Jan 2016 17:46:36 -0300 Subject: [PATCH] init fiat rate service from within bws --- config.js | 4 ++++ lib/expressapp.js | 11 ++++------- lib/server.js | 29 +++++++++++++++++++++++++++-- test/integration/fiatrateservice.js | 2 +- 4 files changed, 36 insertions(+), 10 deletions(-) diff --git a/config.js b/config.js index c3887e4..25c502f 100644 --- a/config.js +++ b/config.js @@ -52,6 +52,10 @@ var config = { subjectPrefix: '', pushServerUrl: 'http://localhost:8000/send', }, + fiatRateServiceOpts: { + defaultProvider: 'BitPay', + fetchInterval: 15, // in minutes + }, // To use email notifications uncomment this: // emailOpts: { // host: 'localhost', diff --git a/lib/expressapp.js b/lib/expressapp.js index 599230a..30899f8 100644 --- a/lib/expressapp.js +++ b/lib/expressapp.js @@ -502,13 +502,10 @@ ExpressApp.prototype.start = function(opts, cb) { router.get('/v1/fiatrates/:code/', function(req, res) { var opts = { - code: req.params['code'], - source: req.query.source, - ts: req.query.ts, - } - // if (_.isString(ts) && ts.indexOf(',') !== -1) { - // ts = ts.split(','); - // } + code: req.params['code'], + source: req.query.source, + ts: req.query.ts, + } server.getFiatRate(opts, function(err, rates) { if (err) returnError({ code: 500, diff --git a/lib/server.js b/lib/server.js index b0133c5..68f0578 100644 --- a/lib/server.js +++ b/lib/server.js @@ -22,6 +22,7 @@ var Lock = require('./lock'); var Storage = require('./storage'); var MessageBroker = require('./messagebroker'); var BlockchainExplorer = require('./blockchainexplorer'); +var FiatRateService = require('./fiatrateservice'); var Model = require('./model'); var Wallet = Model.Wallet; @@ -33,6 +34,7 @@ var storage; var blockchainExplorer; var blockchainExplorerOpts; var messageBroker; +var fiatRateService; var serviceVersion; var HISTORY_LIMIT = 10; @@ -50,6 +52,7 @@ function WalletService() { this.blockchainExplorer = blockchainExplorer; this.blockchainExplorerOpts = blockchainExplorerOpts; this.messageBroker = messageBroker; + this.fiatRateService = fiatRateService; this.notifyTicker = 0; }; @@ -101,6 +104,22 @@ WalletService.initialize = function(opts, cb) { return cb(); }; + function initFiatRateService(cb) { + if (opts.fiatRateService) { + fiatRateService = opts.fiatRateService; + return cb(); + } else { + var newFiatRateService = new FiatRateService(); + var opts2 = opts.fiatRateServiceOpts || {}; + opts2.storage = storage; + newFiatRateService.init(opts2, function(err) { + if (err) return cb(err); + fiatRateService = newFiatRateService; + return cb(); + }); + } + }; + async.series([ function(next) { @@ -109,6 +128,9 @@ WalletService.initialize = function(opts, cb) { function(next) { initMessageBroker(next); }, + function(next) { + initFiatRateService(next); + }, ], function(err) { if (err) { log.error('Could not initialize', err); @@ -2373,13 +2395,16 @@ WalletService.prototype.startScan = function(opts, cb) { * @param {String} [opts.provider] - A provider of exchange rates (default 'BitPay'). * @returns {Object} rates - The exchange rate. */ -WalletService.prototype.getRate = function(opts, cb) { +WalletService.prototype.getFiatRate = function(opts, cb) { var self = this; if (!Utils.checkRequired(opts, ['code'])) return cb(new ClientError('Required argument missing')); - + self.fiatRateService.getRate(opts, function(err, rate) { + if (err) return cb(err); + return cb(null, rate); + }); }; diff --git a/test/integration/fiatrateservice.js b/test/integration/fiatrateservice.js index 5fbd1db..169c7cf 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.only('Fiat rate service', function() { +describe('Fiat rate service', function() { var service, request; before(function(done) {