Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- 'use strict';
- const joi = require('joi');
- const api = require('./api');
- const Exchange = require('./exchange');
- const xmlParser = require('./parse-xml');
- const schema = joi
- .object({
- source: joi.string().required().min(3).max(3).example('EUR'),
- target: joi.string().required().min(3).max(3).example('GBP')
- })
- .unknown()
- .required();
- const defaults = {
- timeout: 1000 // 1 sec
- };
- const exchange = async (pair, options = {}) => {
- options = Object.assign({}, defaults, options);
- const {source, target} = joi.attempt(pair, schema);
- const {requestApi = api, parser = xmlParser} = options;
- const exchange = new Exchange(requestApi, parser, options);
- const rate = await exchange.convert({source, target});
- return {source, target, rate};
- };
- module.exports = exchange;
- 'use strict';
- const URL = 'https://www.ecb.europa.eu/stats/eurofxref/eurofxref-daily.xml';
- class Exchange {
- constructor(api, parser, options = {}) {
- this.api = api;
- this.options = options;
- this.parser = parser;
- }
- async convert({source, target}) {
- if (!this.xml) {
- await this.fetch();
- this.euroToAll = this.parser(this.xml);
- }
- const euroToSource = this.euroToAll[source];
- const euroToTarget = this.euroToAll[target];
- return exchange(euroToSource, euroToTarget);
- }
- async fetch() {
- const response = await this.api.fetch(URL, this.options);
- this.xml = response.body || '';
- }
- }
- function exchange(from, to) {
- return round(parseFloat(to) / parseFloat(from));
- }
- function round(result, digits = 4) {
- return Math.round(result * (10 ** digits)) / (10 ** digits);
- }
- module.exports = Exchange;
- 'use strict';
- const xmldoc = require('xmldoc');
- const debug = require('debug')('exchange-rate:parse');
- const currencies = require('./currencies');
- const parse = xml => {
- const doc = new xmldoc.XmlDocument(xml);
- const cube = doc.childNamed('Cube').childNamed('Cube');
- const rates = currencies.reduce(
- (accumulator, currency) => {
- const exchange = cube.childWithAttribute('currency', currency);
- if (exchange) {
- const {rate} = exchange.attr;
- accumulator[currency] = rate;
- } else {
- debug(`Node not found for currency: ${currency}`);
- }
- return accumulator;
- },
- {}
- );
- // Add EUR rate to make it consistent
- rates.EUR = '1.0';
- return rates;
- };
- module.exports = parse;
- 'use strict';
- const got = require('got');
- module.exports = {
- async fetch(url, options = {}) {
- return got(url, options);
- }
- };
Add Comment
Please, Sign In to add comment