bitcoin-converter-cli
Version:
A CLI to convert Bitcoin to any currency provided
52 lines (41 loc) • 2.15 kB
JavaScript
;
var chalk = require('chalk');
var request = require('request-promise-native');
var ora = require('ora');
var spinner = ora({
text: 'Retrieving Bitcoin data...',
color: 'yellow'
});
var PRICE_TYPES = {
low: 'low',
high: 'high',
sale: 'ask',
buy: 'bid'
};
module.exports = function () {
var c = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : 'USD';
var amount = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : 1;
var baseUrl = 'https://economia.awesomeapi.com.br/json/all';
var currency = c.toUpperCase();
spinner.start();
var handleSuccess = function handleSuccess(body) {
var coins = JSON.parse(body);
var currentCoin = coins[currency];
var toCurrency = function toCurrency(value) {
return parseFloat(value).toFixed(3);
};
var getPrice = function getPrice() {
var type = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : PRICE_TYPES.high;
return currency === 'BRL' ? "".concat(toCurrency(coins.BTC[type] * amount)) : "".concat(toCurrency(coins.BTC[type] / currentCoin[type] * amount));
};
console.info("".concat(chalk.hex('#B84BFF').bold('Bitcoin Converter')).concat(chalk.hex('#D697FF')(" | ".concat(amount, " BTC to ").concat(chalk.underline.bold(currency), ":")), "\n\n").concat(chalk.cyan.bold('LOW:'), " ").concat(chalk.yellow("".concat(getPrice(PRICE_TYPES.low))), "\n\n").concat(chalk.cyan.bold('HIGH:'), " ").concat(chalk.yellow("".concat(getPrice())), "\n\n").concat(chalk.cyan.bold('VARIANT:'), " ").concat(chalk.yellow("".concat(coins.BTC.pctChange, "%")), "\n\n").concat(chalk.cyan.bold('SALE:'), " ").concat(chalk.yellow("".concat(getPrice(PRICE_TYPES.sale))), "\n\n").concat(chalk.cyan.bold('BUY:'), " ").concat(chalk.yellow("".concat(getPrice(PRICE_TYPES.buy))), "\n"));
};
var handleError = function handleError(err) {
spinner.stop();
console.info(chalk.red('Something went wrong in the API. Try in a few minutes.'));
return err;
};
return request(baseUrl).then(function (body) {
return spinner.stop() && body;
}).then(handleSuccess)["catch"](handleError);
};