From 3076f9abe760ad976eb9154214fcb16eac318010 Mon Sep 17 00:00:00 2001 From: Duvan Monsalve Date: Sun, 27 Oct 2019 11:41:38 -0500 Subject: [PATCH] [update] Fix coins logic, and fix test --- constants/coinsSymbols.js | 17 +++++++++++++++++ lib/coinPrice.js | 12 +++++++----- test/coinPrice.js | 6 +++--- upgrade-checklist.md | 2 +- 4 files changed, 28 insertions(+), 9 deletions(-) create mode 100644 constants/coinsSymbols.js diff --git a/constants/coinsSymbols.js b/constants/coinsSymbols.js new file mode 100644 index 0000000..33613d3 --- /dev/null +++ b/constants/coinsSymbols.js @@ -0,0 +1,17 @@ +'use strict' + +const coins = Object.freeze({ + BTC: 'bitcoin', + BCH: 'bitcoin-cash', + ETH: 'ethereum', + ETC: 'ethereum-classic', + LTC: 'litecoin', + XRP: 'ripple', + ADA: 'cardano', + IOT: 'iota', + XEM: 'nem', + XLM: 'stellar', + DASH: 'dash' +}) + +module.exports = coins diff --git a/lib/coinPrice.js b/lib/coinPrice.js index 7e7f6c4..2bcd545 100644 --- a/lib/coinPrice.js +++ b/lib/coinPrice.js @@ -3,15 +3,17 @@ const rp = require('request-promise') const logError = require('debug')('bot:error') +const coinsSymbols = require('./../constants/coinsSymbols') + function coinPrice (bot, message) { const re = /(BTC|BCH|ETH|LTC|XRP|ADA|IOT|XEM|XLM|DASH)/gi - const coin = message.text.match(re) ? message.text.match(re)[0].toUpperCase() : null + const coinSymbol = message.text.match(re) ? message.text.match(re)[0].toUpperCase() : null - if (coin) { - return rp(`http://www.coincap.io/page/${coin}`) + if (coinSymbol) { + return rp(`https://api.coincap.io/v2/rates/${coinsSymbols[coinSymbol]}`) .then((coin) => { - const coinInfo = JSON.parse(coin) - return bot.reply(message, `*${coinInfo.id} = ${coinInfo.price}* _Price from coincap.io API_`) + const { data: coinInfo } = JSON.parse(coin) + return bot.reply(message, `*${coinInfo.id} = ${parseFloat(coinInfo.rateUsd, 10).toFixed(2)} USD* _Price from coincap.io API_`) }) .catch(err => { logError('caught', err) diff --git a/test/coinPrice.js b/test/coinPrice.js index 8a61d91..b305f90 100644 --- a/test/coinPrice.js +++ b/test/coinPrice.js @@ -25,14 +25,14 @@ test.beforeEach(t => { } }) -test.skip('it returns the actual price of the selected coin', t => { +test('it returns the actual price of the selected coin', t => { t.plan(1) const { bot, message } = t.context - const reply = '*BTC =' + const reply = '*bitcoin =' // make coin request return coinPrice(bot, message).then(() => { - t.is(bot.reply.args[0][1].slice(0, 6), reply, 'bot replied') + t.is(bot.reply.args[0][1].slice(0, 10), reply, 'bot replied') }) }) diff --git a/upgrade-checklist.md b/upgrade-checklist.md index 423dad7..528f931 100644 --- a/upgrade-checklist.md +++ b/upgrade-checklist.md @@ -24,7 +24,7 @@ - [x] standard 10.0.3 10.0.3 14.3.1 - [x] update files to match new standard - [ ] superagent 3.8.2 3.8.3 5.1.0 -- [ ] fix coin command +- [x] fix coin command - [ ] use native Promises - [ ] can we remove babel-preset-env? - [ ] can we get nock tests to be parallel?