Skip to content

Commit

Permalink
Add chalk colors
Browse files Browse the repository at this point in the history
  • Loading branch information
AlexandreKavalerski committed Dec 28, 2017
1 parent f603f98 commit 6ccd4e9
Show file tree
Hide file tree
Showing 6 changed files with 26 additions and 10 deletions.
16 changes: 15 additions & 1 deletion bin/ConvertBTC.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,24 @@
'use strict';

var request = require('request');
var chalk = require('chalk');

/* eslint consistent-return: 0 */
function convertBTC() {
var currency = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : 'USD';
var amount = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : 1;

return amount + ' BTC to ' + currency + ' = 2000.00';
var url = 'https://apiv2.bitcoinaverage.com/convert/global?from=BTC&to=' + currency + '&amount=' + amount;
request(url, function (error, response, body) {
var apiResponse = void 0;
try {
apiResponse = JSON.parse(body);
} catch (parseError) {
console.log(chalk.red('Something went wrong with the API. Try in a few minutes.'));
return parseError;
}
console.log(chalk.magenta(amount) + ' BTC to ' + chalk.cyan(currency) + ' = ' + chalk.blue(apiResponse.price));
});
}

module.exports = convertBTC;
2 changes: 1 addition & 1 deletion bin/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,4 @@ var convertBTC = require('./ConvertBTC');

program.version(pkg.version).description(pkg.description).option('-C, --currency <currency>', 'Currency to be converted. (Default: USD)').option('-A, --amount <amount>', 'Value in Bitcoin to convert. (Default: 1)').parse(process.argv);

console.log(convertBTC(program.currency, program.amount));
convertBTC(program.currency, program.amount);
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@
"sinon-chai": "^2.14.0"
},
"dependencies": {
"chalk": "^2.3.0",
"commander": "^2.12.2",
"request": "^2.83.0"
}
Expand Down
5 changes: 3 additions & 2 deletions src/ConvertBTC.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
const request = require('request');
const chalk = require('chalk');

/* eslint consistent-return: 0 */
function convertBTC(currency = 'USD', amount = 1) {
Expand All @@ -8,10 +9,10 @@ function convertBTC(currency = 'USD', amount = 1) {
try {
apiResponse = JSON.parse(body);
} catch (parseError) {
console.log('Something went wrong with the API. Try in a few minutes.');
console.log(chalk.red('Something went wrong with the API. Try in a few minutes.'));
return parseError;
}
console.log(`${amount} BTC to ${currency} = ${apiResponse.price}`);
console.log(`${chalk.magenta(amount)} BTC to ${chalk.cyan(currency)} = ${chalk.blue(apiResponse.price)}`);
});
}

Expand Down
2 changes: 1 addition & 1 deletion src/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,4 +11,4 @@ program
.option('-A, --amount <amount>', 'Value in Bitcoin to convert. (Default: 1)')
.parse(process.argv);

console.log(convertBTC(program.currency, program.amount));
convertBTC(program.currency, program.amount);
10 changes: 5 additions & 5 deletions tests/ConvertBTC.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ const nock = require('nock');
const chai = require('chai');
const sinon = require('sinon');
const sinonChai = require('sinon-chai');
const chalk = require('chalk');

const expect = chai.expect;

Expand All @@ -26,15 +27,14 @@ describe('ConvertBTC', () => {
console.log.restore();
});

// https://apiv2.bitcoinaverage.com/convert/global?from=BTC&to=USD&amount=1
it('should use USD as currency and 1 as amount default', (done) => {
nock('https://apiv2.bitcoinaverage.com')
.get('/convert/global')
.query({ from: 'BTC', to: 'USD', amount: 1 })
.reply(200, responseMock);
convertBTC();
setTimeout(() => {
expect(consoleStub).to.be.calledWith('1 BTC to USD = 14325.57');
expect(consoleStub).to.be.calledWith(`${chalk.magenta(1)} BTC to ${chalk.cyan('USD')} = ${chalk.blue(14325.57)}`);
done();
}, 300);
});
Expand All @@ -46,7 +46,7 @@ describe('ConvertBTC', () => {
.reply(200, responseMock);
convertBTC('USD', 10);
setTimeout(() => {
expect(consoleStub).to.be.calledWith('10 BTC to USD = 14325.57');
expect(consoleStub).to.be.calledWith(`${chalk.magenta(10)} BTC to ${chalk.cyan('USD')} = ${chalk.blue(14325.57)}`);
done();
}, 300);
});
Expand All @@ -58,7 +58,7 @@ describe('ConvertBTC', () => {
.reply(200, responseMock);
convertBTC('BRL');
setTimeout(() => {
expect(consoleStub).to.be.calledWith('1 BTC to BRL = 14325.57');
expect(consoleStub).to.be.calledWith(`${chalk.magenta(1)} BTC to ${chalk.cyan('BRL')} = ${chalk.blue(14325.57)}`);
done();
}, 300);
});
Expand All @@ -71,7 +71,7 @@ describe('ConvertBTC', () => {

convertBTC('BRL');
setTimeout(() => {
expect(consoleStub).to.be.calledWith('Something went wrong with the API. Try in a few minutes.');
expect(consoleStub).to.be.calledWith(chalk.red('Something went wrong with the API. Try in a few minutes.'));
done();
}, 300);
});
Expand Down

0 comments on commit 6ccd4e9

Please sign in to comment.