-
Notifications
You must be signed in to change notification settings - Fork 0
/
cbar-currency-converter.js
74 lines (62 loc) · 2.58 KB
/
cbar-currency-converter.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
const https = require("https");
const convert = require('xml-js');
const moment = require('moment');
module.exports = function(converOptions) {
let currencyDate
if (typeof converOptions == "undefined") {
currencyDate = moment().format('DD.MM.YYYY');
} else if (("currencyDate" in converOptions)) {
currencyDate = moment(converOptions["converOptions"]).format('DD.MM.YYYY')
} else {
currencyDate = moment().format('DD.MM.YYYY');
}
const options = {
host: 'www.cbar.az',
port: 443,
path: '/currencies/' + currencyDate + '.xml',
method: 'GET'
};
return new Promise((resolve, reject) => {
var req = https.request(options, (res) => {
res.setEncoding('utf8');
let data = '';
res.on('data', (chunk) => {
data += chunk;
});
res.on('end', () => {
let jsObject = convert.xml2js(data);
let ValType = jsObject["elements"][0]["attributes"];
let CbarData = {}
CbarData["Date"] = ValType["Date"];
CbarData["Name"] = ValType["Name"];
CbarData["Description"] = ValType["Description"];
jsObject["elements"].forEach((Elements) => {
Elements["elements"].forEach((Valutes) => {
Valutes["elements"].forEach((valute) => {
CbarData[valute["attributes"]["Code"]] = {
"Nominal": valute["elements"][0]["elements"][0]["text"],
"Name": valute["elements"][1]["elements"][0]["text"],
"Value": valute["elements"][2]["elements"][0]["text"]
}
})
});
})
let result;
if (typeof converOptions != "undefined") {
if (("from" in converOptions) && ("amount" in converOptions)) {
result = parseFloat(converOptions["amount"]) / parseFloat(CbarData[converOptions["from"]]["Value"])
} else if (("to" in converOptions) && ("amount" in converOptions)) {
result = parseFloat(converOptions["amount"]) * parseFloat(CbarData[converOptions["to"]]["Value"])
}
} else {
result = CbarData
}
resolve(result)
});
});
req.on('error', (e) => {
reject(e.message);
});
req.end();
});
}