Add caching
This commit is contained in:
parent
7063092393
commit
296841756d
1 changed files with 21 additions and 15 deletions
|
@ -28,26 +28,32 @@ const shopProto = grpc.load(PROTO_PATH).hipstershop;
|
||||||
* Helper function that gets currency data from an XML webpage
|
* Helper function that gets currency data from an XML webpage
|
||||||
* Uses public data from European Central Bank
|
* Uses public data from European Central Bank
|
||||||
*/
|
*/
|
||||||
|
let _data;
|
||||||
function _getCurrencyData (callback) {
|
function _getCurrencyData (callback) {
|
||||||
request(DATA_URL, (err, res) => {
|
if (!_data) {
|
||||||
if (err) {
|
request(DATA_URL, (err, res) => {
|
||||||
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) {
|
if (err) {
|
||||||
throw new Error(`Error parsing HTML: ${err}`);
|
throw new Error(`Error getting data: ${err}`);
|
||||||
}
|
}
|
||||||
|
|
||||||
const array = resJs['Cube']['Cube'].map(x => x['$']);
|
const body = res.body.split('\n').slice(7, -2).join('\n');
|
||||||
const results = array.reduce((acc, x) => {
|
xml2js.parseString(body, (err, resJs) => {
|
||||||
acc[x['currency']] = x['rate'];
|
if (err) {
|
||||||
return acc;
|
throw new Error(`Error parsing HTML: ${err}`);
|
||||||
}, { 'EUR': '1.0' });
|
}
|
||||||
callback(results);
|
|
||||||
|
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);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|
Loading…
Reference in a new issue