Move to new proto + add magic of left-pad
This commit is contained in:
parent
98d9c87e72
commit
b5b6d255ba
3 changed files with 24 additions and 20 deletions
|
@ -18,6 +18,7 @@
|
||||||
|
|
||||||
const path = require('path');
|
const path = require('path');
|
||||||
const grpc = require('grpc');
|
const grpc = require('grpc');
|
||||||
|
const leftPad = require('left-pad');
|
||||||
|
|
||||||
const PROTO_PATH = path.join(__dirname, './proto/demo.proto');
|
const PROTO_PATH = path.join(__dirname, './proto/demo.proto');
|
||||||
const PORT = 31337;
|
const PORT = 31337;
|
||||||
|
@ -28,15 +29,15 @@ const client = new shopProto.CurrencyService(`localhost:${PORT}`,
|
||||||
|
|
||||||
const request = {
|
const request = {
|
||||||
from: {
|
from: {
|
||||||
currency_code: 'USD',
|
currency_code: 'CHF',
|
||||||
units: 300,
|
units: 300,
|
||||||
nanos: 500000000
|
nanos: 0
|
||||||
},
|
},
|
||||||
to_code: 'CHF'
|
to_code: 'EUR'
|
||||||
};
|
};
|
||||||
|
|
||||||
function _moneyToString (m) {
|
function _moneyToString (m) {
|
||||||
return `${m.amount.decimal}.${m.amount.fractional} ${m.currency_code}`;
|
return `${m.units}.${leftPad(m.nanos, 9, '0')} ${m.currency_code}`;
|
||||||
}
|
}
|
||||||
|
|
||||||
client.getSupportedCurrencies({}, (err, response) => {
|
client.getSupportedCurrencies({}, (err, response) => {
|
||||||
|
|
|
@ -8,6 +8,7 @@
|
||||||
"async": "^1.5.2",
|
"async": "^1.5.2",
|
||||||
"google-protobuf": "^3.0.0",
|
"google-protobuf": "^3.0.0",
|
||||||
"grpc": "^1.0.0",
|
"grpc": "^1.0.0",
|
||||||
|
"left-pad": "^1.3.0",
|
||||||
"request": "^2.87.0",
|
"request": "^2.87.0",
|
||||||
"xml2js": "^0.4.19"
|
"xml2js": "^0.4.19"
|
||||||
}
|
}
|
||||||
|
|
|
@ -31,7 +31,7 @@ const shopProto = grpc.load(PROTO_PATH).hipstershop;
|
||||||
let _data;
|
let _data;
|
||||||
function _getCurrencyData (callback) {
|
function _getCurrencyData (callback) {
|
||||||
if (!_data) {
|
if (!_data) {
|
||||||
console.log('Fetching currency data...')
|
console.log('Fetching currency data...');
|
||||||
request(DATA_URL, (err, res) => {
|
request(DATA_URL, (err, res) => {
|
||||||
if (err) {
|
if (err) {
|
||||||
throw new Error(`Error getting data: ${err}`);
|
throw new Error(`Error getting data: ${err}`);
|
||||||
|
@ -53,7 +53,7 @@ function _getCurrencyData (callback) {
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
} else {
|
} else {
|
||||||
console.log('Using cached currency data...')
|
console.log('Using cached currency data...');
|
||||||
callback(_data);
|
callback(_data);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -62,9 +62,10 @@ function _getCurrencyData (callback) {
|
||||||
* Helper function that handles decimal/fractional carrying
|
* Helper function that handles decimal/fractional carrying
|
||||||
*/
|
*/
|
||||||
function _carry (amount) {
|
function _carry (amount) {
|
||||||
amount.fractional += (amount.decimal % 1) * 100;
|
const fractionSize = Math.pow(10, 9);
|
||||||
amount.decimal = Math.floor(amount.decimal) + Math.floor(amount.fractional / 100);
|
amount.nanos += (amount.units % 1) * fractionSize;
|
||||||
amount.fractional = amount.fractional % 100;
|
amount.units = Math.floor(amount.units) + Math.floor(amount.nanos / fractionSize);
|
||||||
|
amount.nanos = amount.nanos % fractionSize;
|
||||||
return amount;
|
return amount;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -72,7 +73,7 @@ function _carry (amount) {
|
||||||
* Lists the supported currencies
|
* Lists the supported currencies
|
||||||
*/
|
*/
|
||||||
function getSupportedCurrencies (call, callback) {
|
function getSupportedCurrencies (call, callback) {
|
||||||
console.log('Getting supported currencies...')
|
console.log('Getting supported currencies...');
|
||||||
_getCurrencyData((data) => {
|
_getCurrencyData((data) => {
|
||||||
callback(null, {currency_codes: Object.keys(data)});
|
callback(null, {currency_codes: Object.keys(data)});
|
||||||
});
|
});
|
||||||
|
@ -82,7 +83,7 @@ function getSupportedCurrencies (call, callback) {
|
||||||
* Converts between currencies
|
* Converts between currencies
|
||||||
*/
|
*/
|
||||||
function convert (call, callback) {
|
function convert (call, callback) {
|
||||||
console.log('Starting conversion request...')
|
console.log('Starting conversion request...');
|
||||||
try {
|
try {
|
||||||
_getCurrencyData((data) => {
|
_getCurrencyData((data) => {
|
||||||
const request = call.request;
|
const request = call.request;
|
||||||
|
@ -90,22 +91,23 @@ function convert (call, callback) {
|
||||||
// Convert: from_currency --> EUR
|
// Convert: from_currency --> EUR
|
||||||
const from = request.from;
|
const from = request.from;
|
||||||
const euros = _carry({
|
const euros = _carry({
|
||||||
decimal: from.amount.decimal / data[from.currency_code],
|
units: from.units / data[from.currency_code],
|
||||||
fractional: from.amount.fractional / data[from.currency_code]
|
nanos: from.nanos / data[from.currency_code]
|
||||||
});
|
});
|
||||||
|
|
||||||
// Convert: EUR --> to_currency
|
// Convert: EUR --> to_currency
|
||||||
const target = _carry({
|
const result = _carry({
|
||||||
decimal: euros.decimal * data[request.to_code],
|
units: euros.units * data[request.to_code],
|
||||||
fractional: euros.fractional * data[request.to_code]
|
nanos: euros.nanos * data[request.to_code]
|
||||||
});
|
});
|
||||||
target.fractional = Math.round(target.fractional);
|
result.nanos = Math.round(result.nanos);
|
||||||
|
result.currency_code = request.to_code;
|
||||||
|
|
||||||
console.log('Conversion request successful.')
|
console.log('Conversion request successful.');
|
||||||
callback(null, {currency_code: request.to_code, amount: target});
|
callback(null, result);
|
||||||
});
|
});
|
||||||
} catch (err) {
|
} catch (err) {
|
||||||
console.error('Conversion request failed.')
|
console.error('Conversion request failed.');
|
||||||
callback(err.message);
|
callback(err.message);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue