-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.js
37 lines (33 loc) · 995 Bytes
/
app.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
const request = require("request");
const fs = require("fs");
const validCodes = ["usd", "gbp", "eur", "chf"];
const code = process.argv[2];
const isValid = validCodes.find(currency => currency === code) ? true : false;
const url = `http://api.nbp.pl/api/exchangerates/rates/a/${code}/?format=json`;
if (isValid) {
request(url, {
json: true
}, (err, res, body) => {
if (res.statusCode !== 200) {
console.log("Błąd. Sprawdź url");
};
if (err) {
return console.log("Błąd :", err);
};
setCorrectGrammar(body);
console.log(`Kurs ${body.currency} w dniu ${body.rates[0].effectiveDate} wynosi ${body.rates[0].mid}`);
});
}
const setCorrectGrammar = body => {
switch (process.argv[2]) {
case "usd" || "USD":
body.currency = "dolara amerykańskiego";
break;
case "gbp" || "GBP":
body.currency = "funta szterlinga";
break;
case "chf" || "CHF":
body.currency = "franka szwajcarskiego";
break;
};
};