REST endpoint + added to shell command

This commit is contained in:
Ivan Socolsky 2016-01-12 11:45:46 -03:00
parent 10ac3a4d65
commit b9b1bddea8
7 changed files with 64 additions and 28 deletions

View File

@ -54,7 +54,7 @@ var config = {
},
fiatRateServiceOpts: {
defaultProvider: 'BitPay',
fetchInterval: 15, // in minutes
fetchInterval: 10, // in minutes
},
// To use email notifications uncomment this:
// emailOpts: {

View File

@ -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');
});
});

View File

@ -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();
});
});

View File

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

View File

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

View File

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

View File

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