Skip to content

Commit

Permalink
[update] Fix coins logic, and fix test
Browse files Browse the repository at this point in the history
  • Loading branch information
duvanmonsa committed Oct 27, 2019
1 parent 944ee33 commit 3076f9a
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 9 deletions.
17 changes: 17 additions & 0 deletions constants/coinsSymbols.js
Original file line number Diff line number Diff line change
@@ -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
12 changes: 7 additions & 5 deletions lib/coinPrice.js
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
6 changes: 3 additions & 3 deletions test/coinPrice.js
Original file line number Diff line number Diff line change
Expand Up @@ -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')
})
})
2 changes: 1 addition & 1 deletion upgrade-checklist.md
Original file line number Diff line number Diff line change
Expand Up @@ -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?

0 comments on commit 3076f9a

Please sign in to comment.