From b9b1bddea8a73654a26ffd7f50082b63e61f312f Mon Sep 17 00:00:00 2001 From: Ivan Socolsky Date: Tue, 12 Jan 2016 11:45:46 -0300 Subject: [PATCH] REST endpoint + added to shell command --- config.js | 2 +- fiatrateservice/fiatrateservice.js | 16 ++++++++++++ lib/expressapp.js | 26 ++++++++++--------- lib/fiatrateservice.js | 6 ++--- start.sh | 1 + stop.sh | 1 + test/integration/fiatrateservice.js | 40 ++++++++++++++++++++--------- 7 files changed, 64 insertions(+), 28 deletions(-) create mode 100644 fiatrateservice/fiatrateservice.js diff --git a/config.js b/config.js index 25c502f..4ba24b7 100644 --- a/config.js +++ b/config.js @@ -54,7 +54,7 @@ var config = { }, fiatRateServiceOpts: { defaultProvider: 'BitPay', - fetchInterval: 15, // in minutes + fetchInterval: 10, // in minutes }, // To use email notifications uncomment this: // emailOpts: { diff --git a/fiatrateservice/fiatrateservice.js b/fiatrateservice/fiatrateservice.js new file mode 100644 index 0000000..6cc5a99 --- /dev/null +++ b/fiatrateservice/fiatrateservice.js @@ -0,0 +1,16 @@ +#!/usr/bin/env node + +'use strict'; + +var config = require('../config'); +var FiatRateService = require('../lib/fiatrateservice'); + +var service = new FiatRateService(); +service.init(config, function(err) { + if (err) throw err; + service.startCron(config, function(err) { + if (err) throw err; + + console.log('Fiat rate service started'); + }); +}); diff --git a/lib/expressapp.js b/lib/expressapp.js index 30899f8..c197374 100644 --- a/lib/expressapp.js +++ b/lib/expressapp.js @@ -113,7 +113,7 @@ ExpressApp.prototype.start = function(opts, cb) { }; }; - function getServer(req, res, cb) { + function getServer(req, res) { var opts = { clientVersion: req.header('x-client-version'), }; @@ -501,18 +501,20 @@ 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, - } - server.getFiatRate(opts, function(err, rates) { - if (err) returnError({ - code: 500, - message: err, + getServerWithAuth(req, res, function(server) { + var opts = { + code: req.params['code'], + source: req.query.source, + ts: +req.query.ts, + }; + server.getFiatRate(opts, function(err, rates) { + if (err) returnError({ + code: 500, + message: err, + }); + res.json(rates); + res.end(); }); - res.json(rates); - res.end(); }); }); diff --git a/lib/fiatrateservice.js b/lib/fiatrateservice.js index 5b9f4ae..9a3306a 100644 --- a/lib/fiatrateservice.js +++ b/lib/fiatrateservice.js @@ -108,7 +108,7 @@ FiatRateService.prototype._retrieve = function(provider, cb) { }; -FiatRateService.prototype.getRate = function(code, opts, cb) { +FiatRateService.prototype.getRate = function(opts, cb) { var self = this; $.shouldBeFunction(cb); @@ -116,10 +116,10 @@ FiatRateService.prototype.getRate = function(code, opts, cb) { opts = opts || {}; var provider = opts.provider || self.defaultProvider; - var ts = opts.ts || Date.now(); + var ts = _.isNumber(opts.ts) ? opts.ts : Date.now(); async.map([].concat(ts), function(ts, cb) { - self.storage.fetchFiatRate(provider, code, ts, function(err, rate) { + self.storage.fetchFiatRate(provider, opts.code, ts, function(err, rate) { if (err) return cb(err); return cb(null, { ts: +ts, diff --git a/start.sh b/start.sh index bfe1e55..3cd1e27 100755 --- a/start.sh +++ b/start.sh @@ -34,5 +34,6 @@ run_program messagebroker/messagebroker.js pids/messagebroker.pid logs/messagebr run_program bcmonitor/bcmonitor.js pids/bcmonitor.pid logs/bcmonitor.log run_program emailservice/emailservice.js pids/emailservice.pid logs/emailservice.log run_program pushnotificationsservice/pushnotificationsservice.js pids/pushnotificationsservice.pid logs/pushnotificationsservice.log +run_program fiatrateservice/fiatrateservice.js pids/fiatrateservice.pid logs/fiatrateservice.log run_program bws.js pids/bws.pid logs/bws.log diff --git a/stop.sh b/stop.sh index b15de9d..7ad5071 100755 --- a/stop.sh +++ b/stop.sh @@ -11,6 +11,7 @@ stop_program () } stop_program pids/bws.pid +stop_program pids/fiatrateservice.pid stop_program pids/emailservice.pid stop_program pids/bcmonitor.pid stop_program pids/pushnotificationsservice.pid diff --git a/test/integration/fiatrateservice.js b/test/integration/fiatrateservice.js index 169c7cf..58d4d06 100644 --- a/test/integration/fiatrateservice.js +++ b/test/integration/fiatrateservice.js @@ -44,7 +44,9 @@ describe('Fiat rate service', function() { value: 123.45, }], function(err) { should.not.exist(err); - service.getRate('USD', {}, function(err, res) { + service.getRate({ + code: 'USD' + }, function(err, res) { should.not.exist(err); res.rate.should.equal(123.45); done(); @@ -62,7 +64,9 @@ describe('Fiat rate service', function() { value: 345.67, }], function(err) { should.not.exist(err); - service.getRate('EUR', {}, function(err, res) { + service.getRate({ + code: 'EUR' + }, function(err, res) { should.not.exist(err); res.rate.should.equal(345.67); done(); @@ -82,11 +86,14 @@ describe('Fiat rate service', function() { value: 200.00, }], function(err) { should.not.exist(err); - service.getRate('USD', {}, function(err, res) { + service.getRate({ + code: 'USD' + }, function(err, res) { should.not.exist(err); res.rate.should.equal(100.00, 'Should use default provider'); - service.getRate('USD', { - provider: 'Bitstamp' + service.getRate({ + code: 'USD', + provider: 'Bitstamp', }, function(err, res) { should.not.exist(err); res.rate.should.equal(200.00); @@ -111,7 +118,8 @@ describe('Fiat rate service', function() { value: 345.67, }], function(err) { should.not.exist(err); - service.getRate('USD', { + service.getRate({ + code: 'USD', ts: 50, }, function(err, res) { should.not.exist(err); @@ -138,7 +146,8 @@ describe('Fiat rate service', function() { }], next); }, function(err) { should.not.exist(err); - service.getRate('USD', { + service.getRate({ + code: 'USD', ts: [50, 100, 199, 500], }, function(err, res) { should.not.exist(err); @@ -191,17 +200,22 @@ describe('Fiat rate service', function() { service._fetch(function(err) { should.not.exist(err); - service.getRate('USD', {}, function(err, res) { + service.getRate({ + code: 'USD' + }, function(err, res) { should.not.exist(err); res.fetchedOn.should.equal(100); res.rate.should.equal(123.45); - service.getRate('USD', { - provider: 'Bitstamp' + service.getRate({ + code: 'USD', + provider: 'Bitstamp', }, function(err, res) { should.not.exist(err); res.fetchedOn.should.equal(100); res.rate.should.equal(120.00); - service.getRate('EUR', {}, function(err, res) { + service.getRate({ + code: 'EUR' + }, function(err, res) { should.not.exist(err); res.fetchedOn.should.equal(100); res.rate.should.equal(234.56); @@ -229,7 +243,9 @@ describe('Fiat rate service', function() { service._fetch(function(err) { should.not.exist(err); - service.getRate('USD', {}, function(err, res) { + service.getRate({ + code: 'USD' + }, function(err, res) { should.not.exist(err); res.ts.should.equal(100); should.not.exist(res.rate)