forked from divyanshu-rawat/checkout-your-cart
-
Notifications
You must be signed in to change notification settings - Fork 0
/
fx.js
48 lines (43 loc) · 1.04 KB
/
fx.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
var superagent = require('superagent');
var _ = require('underscore');
module.exports = function(Config) {
var url = 'http://openexchangerates.org/api/latest.json?app_id=' +
Config.openExchangeRatesKey;
var rates = {
USD: 1,
EUR: 1.1,
GBP: 1.5
};
var ping = function(callback) {
superagent.get(url, function(error, res) {
// If error happens, ignore it because we'll try again in an hour
if (error) {
if (callback) {
callback(error);
}
return;
}
var results;
try {
var results = JSON.parse(res.text);
_.each(results.rates || {}, function(value, key) {
rates[key] = value;
});
if (callback) {
callback(null, rates);
}
} catch (e) {
if (callback) {
callback(e);
}
}
});
};
setInterval(ping, 60 * 60 * 1000); // Repeat every hour
// Return the current state of the exchange rates
var ret = function() {
return rates;
};
ret.ping = ping;
return ret;
};