-
Notifications
You must be signed in to change notification settings - Fork 13
/
Copy pathcurrencyConverter.js
144 lines (113 loc) · 4.05 KB
/
currencyConverter.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
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
var xml2js = require('xml2js');
var request = require('request');
var _ = require('underscore');
var fs = require('fs');
var path = require('path');
module.exports = {
settings: {
url: "http://www.ecb.int/stats/eurofxref/eurofxref-daily.xml"
},
baseCurrency: "EUR",
currenciesMap: [],
currenciesMetadata: [],
executeCallback: null,
readJson: function() {
var data = fs.readFileSync(path.resolve(__dirname, 'Currencies.json'), 'utf8');
return JSON.parse(data);
},
removeNamespaces: function(xml){
var fixedXML = xml.replace(/(<\/?)(\w+:)/g,'$1');
return (fixedXML.replace(/xmlns(:\w+)?="[^"]*"/g,'')).trim();
},
parseXML: function(xml) {
var self = this;
var cleanXML = this.removeNamespaces(xml);
var parser = new xml2js.Parser();
parser.parseString(cleanXML, function(err,result){
var currencies = result.Envelope.Cube[0].Cube[0].Cube;
self.createCurrenciesMap(currencies);
});
},
createCurrenciesMap: function(currencies) {
var self = this;
_.each(currencies, function(item) {
var currency = eval('item.$').currency;
var rate = eval('item.$').rate;
self.currenciesMap.push({ currency: currency, rate: rate });
});
self.currenciesMap.push({ currency: 'EUR', rate: 1 });
self.executeCallback();
},
getExchangeRates: function() {
var self = this;
request(self.settings.url, function(error, response, body) {
if (!error && response.statusCode == 200) {
self.parseXML(body);
}
});
},
roundValues: function (value, places) {
var multiplier = Math.pow(10, places);
return (Math.round(value * multiplier) / multiplier);
},
fetchRates: function(settings) {
var self = this;
var getCurrency = function(currency) {
return _.find(self.currenciesMap, function(item) {
return item.currency === currency
});
};
var rates = {};
rates.fromCurrency = getCurrency(settings.fromCurrency);
rates.toCurrency = getCurrency(settings.toCurrency);
rates.exchangeRate = (1 / rates.fromCurrency.rate) * rates.toCurrency.rate;
return rates;
},
getAllCurrencies: function(callback) {
this.getExchangeRates();
this.executeCallback = function() {
callback(this.currenciesMap);
};
},
getBaseCurrency: function(callback) {
this.executeCallback = function() {
callback({currency:"EUR"});
}();
},
convert: function(settings, callback) {
this.getExchangeRates();
this.executeCallback = function() {
var exchangedValue = {};
var rates = this.fetchRates(settings);
exchangedValue.currency = rates.toCurrency.currency;
exchangedValue.exchangeRate = this.roundValues(rates.exchangeRate, settings.accuracy | 4);
exchangedValue.amount = this.roundValues(settings.amount * rates.exchangeRate, settings.accuracy | 4);
callback(exchangedValue);
};
},
getExchangeRate: function(settings, callback) {
this.getExchangeRates();
this.executeCallback = function() {
var exchangedValue = {};
var rates = this.fetchRates(settings);
exchangedValue.toCurrency = rates.toCurrency.currency;
exchangedValue.fromCurrency = rates.fromCurrency.currency;
exchangedValue.exchangeRate = this.roundValues(rates.exchangeRate, settings.accuracy | 4);
callback(exchangedValue);
};
},
getCurrenciesMetadata: function(callback) {
this.currenciesMetadata = this.readJson();
callback(this.currenciesMetadata);
},
getCurrencyMetadata: function(settings, callback) {
this.currenciesMetadata = this.readJson();
var self = this;
var getCurrency = function(currency) {
return _.find(self.currenciesMetadata, function(item) {
return item.Code === currency
});
};
callback(getCurrency(settings.currency));
}
};