This repository has been archived by the owner on Jan 19, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
rate.js
63 lines (56 loc) · 1.6 KB
/
rate.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
/* eslint-disable camelcase */
const alfy = require("alfy");
const numeral = require("numeral");
const moment = require("moment");
const getCurrency = require("./get-currency");
const reportFetchRateError = require("./report-fetch-rate-error");
const fixRurToRub = require("./fix-rur-to-rub");
const {
requestURL,
buySymbol,
saleSymbol,
dividerSymbol,
} = require("./config");
numeral.defaultFormat("0.00");
(async () => {
const response = fixRurToRub(await alfy.fetch(requestURL));
const state = {
rates: null,
cached: null,
cachedTime: null,
};
if (response.length >= 4) {
state.rates = response;
state.cached = false;
alfy.cache.set("rates", { rates: response, cachedTime: moment() });
} else if (alfy.cache.get("rates")) {
const { rates, cachedTime } = alfy.cache.get("rates");
state.rates = rates;
state.cached = true;
state.cachedTime = cachedTime;
} else {
reportFetchRateError();
return;
}
const items = state.rates.map(({ ccy, base_ccy, buy, sale }) => {
const title = `${ccy} ・ ${
getCurrency({ name: base_ccy.toLowerCase() }).symbol
} ${numeral(buy).format()} ${buySymbol} ${dividerSymbol} ${
getCurrency({ name: base_ccy.toLowerCase() }).symbol
} ${numeral(sale).format()} ${saleSymbol}`;
const arg = title;
const subtitle = state.cached
? `From cache ${moment(state.cachedTime).fromNow()}`
: "Updated just now";
const icon = {
path: getCurrency({ name: ccy.toLowerCase() }).icon,
};
return {
title,
subtitle,
arg,
icon,
};
});
alfy.output(items);
})();