Skip to content

Commit b9b1bdd

Browse files
committed
REST endpoint + added to shell command
1 parent 10ac3a4 commit b9b1bdd

File tree

7 files changed

+64
-28
lines changed

7 files changed

+64
-28
lines changed

config.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ var config = {
5454
},
5555
fiatRateServiceOpts: {
5656
defaultProvider: 'BitPay',
57-
fetchInterval: 15, // in minutes
57+
fetchInterval: 10, // in minutes
5858
},
5959
// To use email notifications uncomment this:
6060
// emailOpts: {

fiatrateservice/fiatrateservice.js

+16
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
#!/usr/bin/env node
2+
3+
'use strict';
4+
5+
var config = require('../config');
6+
var FiatRateService = require('../lib/fiatrateservice');
7+
8+
var service = new FiatRateService();
9+
service.init(config, function(err) {
10+
if (err) throw err;
11+
service.startCron(config, function(err) {
12+
if (err) throw err;
13+
14+
console.log('Fiat rate service started');
15+
});
16+
});

lib/expressapp.js

+14-12
Original file line numberDiff line numberDiff line change
@@ -113,7 +113,7 @@ ExpressApp.prototype.start = function(opts, cb) {
113113
};
114114
};
115115

116-
function getServer(req, res, cb) {
116+
function getServer(req, res) {
117117
var opts = {
118118
clientVersion: req.header('x-client-version'),
119119
};
@@ -501,18 +501,20 @@ ExpressApp.prototype.start = function(opts, cb) {
501501
});
502502

503503
router.get('/v1/fiatrates/:code/', function(req, res) {
504-
var opts = {
505-
code: req.params['code'],
506-
source: req.query.source,
507-
ts: req.query.ts,
508-
}
509-
server.getFiatRate(opts, function(err, rates) {
510-
if (err) returnError({
511-
code: 500,
512-
message: err,
504+
getServerWithAuth(req, res, function(server) {
505+
var opts = {
506+
code: req.params['code'],
507+
source: req.query.source,
508+
ts: +req.query.ts,
509+
};
510+
server.getFiatRate(opts, function(err, rates) {
511+
if (err) returnError({
512+
code: 500,
513+
message: err,
514+
});
515+
res.json(rates);
516+
res.end();
513517
});
514-
res.json(rates);
515-
res.end();
516518
});
517519
});
518520

lib/fiatrateservice.js

+3-3
Original file line numberDiff line numberDiff line change
@@ -108,18 +108,18 @@ FiatRateService.prototype._retrieve = function(provider, cb) {
108108
};
109109

110110

111-
FiatRateService.prototype.getRate = function(code, opts, cb) {
111+
FiatRateService.prototype.getRate = function(opts, cb) {
112112
var self = this;
113113

114114
$.shouldBeFunction(cb);
115115

116116
opts = opts || {};
117117

118118
var provider = opts.provider || self.defaultProvider;
119-
var ts = opts.ts || Date.now();
119+
var ts = _.isNumber(opts.ts) ? opts.ts : Date.now();
120120

121121
async.map([].concat(ts), function(ts, cb) {
122-
self.storage.fetchFiatRate(provider, code, ts, function(err, rate) {
122+
self.storage.fetchFiatRate(provider, opts.code, ts, function(err, rate) {
123123
if (err) return cb(err);
124124
return cb(null, {
125125
ts: +ts,

start.sh

+1
Original file line numberDiff line numberDiff line change
@@ -34,5 +34,6 @@ run_program messagebroker/messagebroker.js pids/messagebroker.pid logs/messagebr
3434
run_program bcmonitor/bcmonitor.js pids/bcmonitor.pid logs/bcmonitor.log
3535
run_program emailservice/emailservice.js pids/emailservice.pid logs/emailservice.log
3636
run_program pushnotificationsservice/pushnotificationsservice.js pids/pushnotificationsservice.pid logs/pushnotificationsservice.log
37+
run_program fiatrateservice/fiatrateservice.js pids/fiatrateservice.pid logs/fiatrateservice.log
3738
run_program bws.js pids/bws.pid logs/bws.log
3839

stop.sh

+1
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ stop_program ()
1111
}
1212

1313
stop_program pids/bws.pid
14+
stop_program pids/fiatrateservice.pid
1415
stop_program pids/emailservice.pid
1516
stop_program pids/bcmonitor.pid
1617
stop_program pids/pushnotificationsservice.pid

test/integration/fiatrateservice.js

+28-12
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,9 @@ describe('Fiat rate service', function() {
4444
value: 123.45,
4545
}], function(err) {
4646
should.not.exist(err);
47-
service.getRate('USD', {}, function(err, res) {
47+
service.getRate({
48+
code: 'USD'
49+
}, function(err, res) {
4850
should.not.exist(err);
4951
res.rate.should.equal(123.45);
5052
done();
@@ -62,7 +64,9 @@ describe('Fiat rate service', function() {
6264
value: 345.67,
6365
}], function(err) {
6466
should.not.exist(err);
65-
service.getRate('EUR', {}, function(err, res) {
67+
service.getRate({
68+
code: 'EUR'
69+
}, function(err, res) {
6670
should.not.exist(err);
6771
res.rate.should.equal(345.67);
6872
done();
@@ -82,11 +86,14 @@ describe('Fiat rate service', function() {
8286
value: 200.00,
8387
}], function(err) {
8488
should.not.exist(err);
85-
service.getRate('USD', {}, function(err, res) {
89+
service.getRate({
90+
code: 'USD'
91+
}, function(err, res) {
8692
should.not.exist(err);
8793
res.rate.should.equal(100.00, 'Should use default provider');
88-
service.getRate('USD', {
89-
provider: 'Bitstamp'
94+
service.getRate({
95+
code: 'USD',
96+
provider: 'Bitstamp',
9097
}, function(err, res) {
9198
should.not.exist(err);
9299
res.rate.should.equal(200.00);
@@ -111,7 +118,8 @@ describe('Fiat rate service', function() {
111118
value: 345.67,
112119
}], function(err) {
113120
should.not.exist(err);
114-
service.getRate('USD', {
121+
service.getRate({
122+
code: 'USD',
115123
ts: 50,
116124
}, function(err, res) {
117125
should.not.exist(err);
@@ -138,7 +146,8 @@ describe('Fiat rate service', function() {
138146
}], next);
139147
}, function(err) {
140148
should.not.exist(err);
141-
service.getRate('USD', {
149+
service.getRate({
150+
code: 'USD',
142151
ts: [50, 100, 199, 500],
143152
}, function(err, res) {
144153
should.not.exist(err);
@@ -191,17 +200,22 @@ describe('Fiat rate service', function() {
191200

192201
service._fetch(function(err) {
193202
should.not.exist(err);
194-
service.getRate('USD', {}, function(err, res) {
203+
service.getRate({
204+
code: 'USD'
205+
}, function(err, res) {
195206
should.not.exist(err);
196207
res.fetchedOn.should.equal(100);
197208
res.rate.should.equal(123.45);
198-
service.getRate('USD', {
199-
provider: 'Bitstamp'
209+
service.getRate({
210+
code: 'USD',
211+
provider: 'Bitstamp',
200212
}, function(err, res) {
201213
should.not.exist(err);
202214
res.fetchedOn.should.equal(100);
203215
res.rate.should.equal(120.00);
204-
service.getRate('EUR', {}, function(err, res) {
216+
service.getRate({
217+
code: 'EUR'
218+
}, function(err, res) {
205219
should.not.exist(err);
206220
res.fetchedOn.should.equal(100);
207221
res.rate.should.equal(234.56);
@@ -229,7 +243,9 @@ describe('Fiat rate service', function() {
229243

230244
service._fetch(function(err) {
231245
should.not.exist(err);
232-
service.getRate('USD', {}, function(err, res) {
246+
service.getRate({
247+
code: 'USD'
248+
}, function(err, res) {
233249
should.not.exist(err);
234250
res.ts.should.equal(100);
235251
should.not.exist(res.rate)

0 commit comments

Comments
 (0)