Switch currencyservice to static JSON file (#173)

This commit is contained in:
Ace Nassri 2019-03-06 16:42:08 -08:00 committed by Ahmet Alp Balkan
parent d5db024763
commit f7580958cc
2 changed files with 38 additions and 30 deletions

View file

@ -0,0 +1,35 @@
{
"EUR": "1.0",
"USD": "1.1305",
"JPY": "126.40",
"BGN": "1.9558",
"CZK": "25.592",
"DKK": "7.4609",
"GBP": "0.85970",
"HUF": "315.51",
"PLN": "4.2996",
"RON": "4.7463",
"SEK": "10.5375",
"CHF": "1.1360",
"ISK": "136.80",
"NOK": "9.8040",
"HRK": "7.4210",
"RUB": "74.4208",
"TRY": "6.1247",
"AUD": "1.6072",
"BRL": "4.2682",
"CAD": "1.5128",
"CNY": "7.5857",
"HKD": "8.8743",
"IDR": "15999.40",
"ILS": "4.0875",
"INR": "79.4320",
"KRW": "1275.05",
"MXN": "21.7999",
"MYR": "4.6289",
"NZD": "1.6679",
"PHP": "59.083",
"SGD": "1.5349",
"THB": "36.012",
"ZAR": "16.0583"
}

View file

@ -30,8 +30,6 @@ require('@google-cloud/debug-agent').start({
const path = require('path');
const grpc = require('grpc');
const request = require('request');
const xml2js = require('xml2js');
const pino = require('pino');
const protoLoader = require('@grpc/proto-loader');
@ -39,7 +37,6 @@ const MAIN_PROTO_PATH = path.join(__dirname, './proto/demo.proto');
const HEALTH_PROTO_PATH = path.join(__dirname, './proto/grpc/health/v1/health.proto');
const PORT = 7000;
const DATA_URL = 'http://www.ecb.europa.eu/stats/eurofxref/eurofxref-daily.xml';
const shopProto = _loadProto(MAIN_PROTO_PATH).hipstershop;
const healthProto = _loadProto(HEALTH_PROTO_PATH).grpc.health.v1;
@ -69,36 +66,12 @@ function _loadProto (path) {
}
/**
* Helper function that gets currency data from an XML webpage
* Helper function that gets currency data from a stored JSON file
* Uses public data from European Central Bank
*/
let _data;
function _getCurrencyData (callback) {
if (!_data) {
logger.info('Fetching currency data...');
request(DATA_URL, (err, res) => {
if (err) {
throw new Error(`Error getting data: ${err}`);
}
const body = res.body.split('\n').slice(7, -2).join('\n');
xml2js.parseString(body, (err, resJs) => {
if (err) {
throw new Error(`Error parsing HTML: ${err}`);
}
const array = resJs['Cube']['Cube'].map(x => x['$']);
const results = array.reduce((acc, x) => {
acc[x['currency']] = x['rate'];
return acc;
}, { 'EUR': '1.0' });
_data = results;
callback(_data);
});
});
} else {
callback(_data);
}
const data = require('./data/currency_conversion.json');
callback(data);
}
/**