Browse Source

REST endpoint + added to shell command

activeAddress
Ivan Socolsky 9 years ago
parent
commit
b9b1bddea8
  1. 2
      config.js
  2. 16
      fiatrateservice/fiatrateservice.js
  3. 26
      lib/expressapp.js
  4. 6
      lib/fiatrateservice.js
  5. 1
      start.sh
  6. 1
      stop.sh
  7. 40
      test/integration/fiatrateservice.js

2
config.js

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

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

26
lib/expressapp.js

@ -113,7 +113,7 @@ ExpressApp.prototype.start = function(opts, cb) {
}; };
}; };
function getServer(req, res, cb) { function getServer(req, res) {
var opts = { var opts = {
clientVersion: req.header('x-client-version'), clientVersion: req.header('x-client-version'),
}; };
@ -501,18 +501,20 @@ ExpressApp.prototype.start = function(opts, cb) {
}); });
router.get('/v1/fiatrates/:code/', function(req, res) { router.get('/v1/fiatrates/:code/', function(req, res) {
var opts = { getServerWithAuth(req, res, function(server) {
code: req.params['code'], var opts = {
source: req.query.source, code: req.params['code'],
ts: req.query.ts, source: req.query.source,
} ts: +req.query.ts,
server.getFiatRate(opts, function(err, rates) { };
if (err) returnError({ server.getFiatRate(opts, function(err, rates) {
code: 500, if (err) returnError({
message: err, code: 500,
message: err,
});
res.json(rates);
res.end();
}); });
res.json(rates);
res.end();
}); });
}); });

6
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; var self = this;
$.shouldBeFunction(cb); $.shouldBeFunction(cb);
@ -116,10 +116,10 @@ FiatRateService.prototype.getRate = function(code, opts, cb) {
opts = opts || {}; opts = opts || {};
var provider = opts.provider || self.defaultProvider; 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) { 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); if (err) return cb(err);
return cb(null, { return cb(null, {
ts: +ts, ts: +ts,

1
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 bcmonitor/bcmonitor.js pids/bcmonitor.pid logs/bcmonitor.log
run_program emailservice/emailservice.js pids/emailservice.pid logs/emailservice.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 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 run_program bws.js pids/bws.pid logs/bws.log

1
stop.sh

@ -11,6 +11,7 @@ stop_program ()
} }
stop_program pids/bws.pid stop_program pids/bws.pid
stop_program pids/fiatrateservice.pid
stop_program pids/emailservice.pid stop_program pids/emailservice.pid
stop_program pids/bcmonitor.pid stop_program pids/bcmonitor.pid
stop_program pids/pushnotificationsservice.pid stop_program pids/pushnotificationsservice.pid

40
test/integration/fiatrateservice.js

@ -44,7 +44,9 @@ describe('Fiat rate service', function() {
value: 123.45, value: 123.45,
}], function(err) { }], function(err) {
should.not.exist(err); should.not.exist(err);
service.getRate('USD', {}, function(err, res) { service.getRate({
code: 'USD'
}, function(err, res) {
should.not.exist(err); should.not.exist(err);
res.rate.should.equal(123.45); res.rate.should.equal(123.45);
done(); done();
@ -62,7 +64,9 @@ describe('Fiat rate service', function() {
value: 345.67, value: 345.67,
}], function(err) { }], function(err) {
should.not.exist(err); should.not.exist(err);
service.getRate('EUR', {}, function(err, res) { service.getRate({
code: 'EUR'
}, function(err, res) {
should.not.exist(err); should.not.exist(err);
res.rate.should.equal(345.67); res.rate.should.equal(345.67);
done(); done();
@ -82,11 +86,14 @@ describe('Fiat rate service', function() {
value: 200.00, value: 200.00,
}], function(err) { }], function(err) {
should.not.exist(err); should.not.exist(err);
service.getRate('USD', {}, function(err, res) { service.getRate({
code: 'USD'
}, function(err, res) {
should.not.exist(err); should.not.exist(err);
res.rate.should.equal(100.00, 'Should use default provider'); res.rate.should.equal(100.00, 'Should use default provider');
service.getRate('USD', { service.getRate({
provider: 'Bitstamp' code: 'USD',
provider: 'Bitstamp',
}, function(err, res) { }, function(err, res) {
should.not.exist(err); should.not.exist(err);
res.rate.should.equal(200.00); res.rate.should.equal(200.00);
@ -111,7 +118,8 @@ describe('Fiat rate service', function() {
value: 345.67, value: 345.67,
}], function(err) { }], function(err) {
should.not.exist(err); should.not.exist(err);
service.getRate('USD', { service.getRate({
code: 'USD',
ts: 50, ts: 50,
}, function(err, res) { }, function(err, res) {
should.not.exist(err); should.not.exist(err);
@ -138,7 +146,8 @@ describe('Fiat rate service', function() {
}], next); }], next);
}, function(err) { }, function(err) {
should.not.exist(err); should.not.exist(err);
service.getRate('USD', { service.getRate({
code: 'USD',
ts: [50, 100, 199, 500], ts: [50, 100, 199, 500],
}, function(err, res) { }, function(err, res) {
should.not.exist(err); should.not.exist(err);
@ -191,17 +200,22 @@ describe('Fiat rate service', function() {
service._fetch(function(err) { service._fetch(function(err) {
should.not.exist(err); should.not.exist(err);
service.getRate('USD', {}, function(err, res) { service.getRate({
code: 'USD'
}, function(err, res) {
should.not.exist(err); should.not.exist(err);
res.fetchedOn.should.equal(100); res.fetchedOn.should.equal(100);
res.rate.should.equal(123.45); res.rate.should.equal(123.45);
service.getRate('USD', { service.getRate({
provider: 'Bitstamp' code: 'USD',
provider: 'Bitstamp',
}, function(err, res) { }, function(err, res) {
should.not.exist(err); should.not.exist(err);
res.fetchedOn.should.equal(100); res.fetchedOn.should.equal(100);
res.rate.should.equal(120.00); res.rate.should.equal(120.00);
service.getRate('EUR', {}, function(err, res) { service.getRate({
code: 'EUR'
}, function(err, res) {
should.not.exist(err); should.not.exist(err);
res.fetchedOn.should.equal(100); res.fetchedOn.should.equal(100);
res.rate.should.equal(234.56); res.rate.should.equal(234.56);
@ -229,7 +243,9 @@ describe('Fiat rate service', function() {
service._fetch(function(err) { service._fetch(function(err) {
should.not.exist(err); should.not.exist(err);
service.getRate('USD', {}, function(err, res) { service.getRate({
code: 'USD'
}, function(err, res) {
should.not.exist(err); should.not.exist(err);
res.ts.should.equal(100); res.ts.should.equal(100);
should.not.exist(res.rate) should.not.exist(res.rate)

Loading…
Cancel
Save