Skip to content
This repository was archived by the owner on Oct 15, 2025. It is now read-only.

Commit 3076f9a

Browse files
committed
[update] Fix coins logic, and fix test
1 parent 944ee33 commit 3076f9a

4 files changed

Lines changed: 28 additions & 9 deletions

File tree

constants/coinsSymbols.js

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
'use strict'
2+
3+
const coins = Object.freeze({
4+
BTC: 'bitcoin',
5+
BCH: 'bitcoin-cash',
6+
ETH: 'ethereum',
7+
ETC: 'ethereum-classic',
8+
LTC: 'litecoin',
9+
XRP: 'ripple',
10+
ADA: 'cardano',
11+
IOT: 'iota',
12+
XEM: 'nem',
13+
XLM: 'stellar',
14+
DASH: 'dash'
15+
})
16+
17+
module.exports = coins

lib/coinPrice.js

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,15 +3,17 @@
33
const rp = require('request-promise')
44
const logError = require('debug')('bot:error')
55

6+
const coinsSymbols = require('./../constants/coinsSymbols')
7+
68
function coinPrice (bot, message) {
79
const re = /(BTC|BCH|ETH|LTC|XRP|ADA|IOT|XEM|XLM|DASH)/gi
8-
const coin = message.text.match(re) ? message.text.match(re)[0].toUpperCase() : null
10+
const coinSymbol = message.text.match(re) ? message.text.match(re)[0].toUpperCase() : null
911

10-
if (coin) {
11-
return rp(`http://www.coincap.io/page/${coin}`)
12+
if (coinSymbol) {
13+
return rp(`https://api.coincap.io/v2/rates/${coinsSymbols[coinSymbol]}`)
1214
.then((coin) => {
13-
const coinInfo = JSON.parse(coin)
14-
return bot.reply(message, `*${coinInfo.id} = ${coinInfo.price}* _Price from coincap.io API_`)
15+
const { data: coinInfo } = JSON.parse(coin)
16+
return bot.reply(message, `*${coinInfo.id} = ${parseFloat(coinInfo.rateUsd, 10).toFixed(2)} USD* _Price from coincap.io API_`)
1517
})
1618
.catch(err => {
1719
logError('caught', err)

test/coinPrice.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -25,14 +25,14 @@ test.beforeEach(t => {
2525
}
2626
})
2727

28-
test.skip('it returns the actual price of the selected coin', t => {
28+
test('it returns the actual price of the selected coin', t => {
2929
t.plan(1)
3030

3131
const { bot, message } = t.context
32-
const reply = '*BTC ='
32+
const reply = '*bitcoin ='
3333

3434
// make coin request
3535
return coinPrice(bot, message).then(() => {
36-
t.is(bot.reply.args[0][1].slice(0, 6), reply, 'bot replied')
36+
t.is(bot.reply.args[0][1].slice(0, 10), reply, 'bot replied')
3737
})
3838
})

upgrade-checklist.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@
2424
- [x] standard 10.0.3 10.0.3 14.3.1
2525
- [x] update files to match new standard
2626
- [ ] superagent 3.8.2 3.8.3 5.1.0
27-
- [ ] fix coin command
27+
- [x] fix coin command
2828
- [ ] use native Promises
2929
- [ ] can we remove babel-preset-env?
3030
- [ ] can we get nock tests to be parallel?

0 commit comments

Comments
 (0)