Skip to content

Instantly share code, notes, and snippets.

@JairoDuarte
Last active November 18, 2017 14:34
Show Gist options
  • Save JairoDuarte/476957f451bc261383a16db5e7c63ebe to your computer and use it in GitHub Desktop.
Save JairoDuarte/476957f451bc261383a16db5e7c63ebe to your computer and use it in GitHub Desktop.
const app = require('./lib/index'),
colors = require('colors/safe'),
{scanf} = require('nodejs-scanf');
console.log(colors.green('Please input value in format: from to money'));
console.log(colors.italic.red('Ex: USD EUR 100'));
console.log(colors.bold(':help -> if you need help'));
// input currency change and value maney
var print = function (params, params1) {
console.log(colors.blue(' %s')+colors.magenta(' is %s'),params,params1);
}
scanf('%s %s %f', function(from, to,value) {
if (from.toUpperCase()==':HELP') {
app().help();
}
else{
app().result(from,to,value,print);
}
});
module.exports = function () {
let API = require('currency-conversion');
// api key for https://currencylayer.com/
let api = new API({
access_key: ['1d36e5db73566dda4cf57cb2af474e19'],
});
// query for get live currency value
var liveQuery = {
source: 'USD',
currencies: []
};
var currency = {
help: function () {
api.list(liveQuery, function (err, result) {
if (err) {
console.log(err);
console.log('Live (Error): ' + JSON.stringify(err));
}else{
console.log();
console.log('Currencies: ');
console.log(result.currencies);
}
});
},
conversion: function (value,rate) {
return value*rate;
},
result: function (from, to, value,collback) {
var _this = this;
api.live(liveQuery, function (err, result) {
if (err) {
console.log(err);
console.log('Live (Error): ' + JSON.stringify(err));
}else{
var string = JSON.stringify(result.quotes);
var quotes = JSON.parse(string);
var _from = quotes['USD'+from], _to = quotes['USD'+to];
collback(value+' '+from,from == 'USD' ? _this.conversion(value,_to)+' '+to : _this.conversion(value,_to/_from)+ ' '+to);
}
});
}
};
return currency;
};
{
"name": "currencyconversion",
"version": "1.0.0",
"description": "App for currency conversion in the terminal",
"main": "app.js",
"bin": {
"CurrencyConversion": "app.js"
},
"preferGlobal": "true",
"scripts": {
"test": "test.js"
},
"keywords": [
"currency",
"money",
"conversão",
"divisas"
],
"author": "Jairo Duarte",
"license": "MIT",
"devDependencies": {
"mocha": "^3.5.3",
"mockery": "^2.1.0",
"sinon": "^3.3.0"
},
"dependencies": {
"nodejs-scanf": "1.1.0",
"currency-conversion": "0.2.4",
"colors": "^1.1.2"
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment