Skip to content

Commit

Permalink
Merge pull request #4 from radiovisual/fetch
Browse files Browse the repository at this point in the history
Replace popsicle with isomorphic-fetch
  • Loading branch information
brh55 committed Jun 11, 2017
2 parents 05abf59 + 3988026 commit 9de10b1
Show file tree
Hide file tree
Showing 5 changed files with 38 additions and 22 deletions.
4 changes: 1 addition & 3 deletions cli.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,7 @@ const cli = meow(`
USD: 261.91
`);

const input = cli.input.map(item => item.replace(/,|\s+/g, '')).join(',');

ethPrice(input)
ethPrice(cli.input.join(','))
.then(prices => {
spinner.stop();
prices.forEach(price => console.log(price));
Expand Down
21 changes: 7 additions & 14 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,23 +1,16 @@
'use strict';
const popsicle = require('popsicle');
const fetch = require('isomorphic-fetch');

module.exports = toSymbol => {
let symbols = 'USD';
if (typeof toSymbol === 'string') {
toSymbol = toSymbol.toUpperCase();
} else {
toSymbol = 'USD';
symbols = toSymbol.split(',').map(s => s.toUpperCase().replace(/\s+/g, '')).join(',');
}

return popsicle.request({
method: 'POST',
url: 'https://min-api.cryptocompare.com/data/price',
query: {
fsym: 'ETH',
tsyms: toSymbol
}
})
.use(popsicle.plugins.parse(['json']))
.then(resp => resp.body)
const url = `https://min-api.cryptocompare.com/data/price?fsym=ETH&tsyms=${symbols}`;

return fetch(url)
.then(resp => resp.json())
.then(data => {
const symbols = Object.keys(data);

Expand Down
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,9 @@
"cryptocurrency"
],
"dependencies": {
"isomorphic-fetch": "^2.2.1",
"meow": "^3.7.0",
"ora": "^1.2.0",
"popsicle": "^9.1.0"
"ora": "^1.2.0"
},
"devDependencies": {
"ava": "^0.17.0",
Expand Down
7 changes: 4 additions & 3 deletions test.js → test/currencies.js
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
import test from 'ava';
import nock from 'nock';
import fn from '.';
import fn from '../';

const apiMock = nock('https://min-api.cryptocompare.com/data')
.persist()
.defaultReplyHeaders({
'Content-Type': 'application/json'
})
.post('/price');
.get('/price');

apiMock
.query({
Expand All @@ -26,7 +27,7 @@ test('Get prices in format', async t => {
test('Forgives whitespace in currency list', async t => {
t.plan(2);

const prices = await fn('usd, btc');
const prices = await fn('usd, btc');
t.is(prices[0], 'USD: 260.21');
t.is(prices[1], 'BTC: 0.0973');
});
24 changes: 24 additions & 0 deletions test/default.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
import test from 'ava';
import nock from 'nock';
import fn from '../';

const apiMock = nock('https://min-api.cryptocompare.com/data')
.persist()
.defaultReplyHeaders({
'Content-Type': 'application/json'
})
.get('/price');

apiMock
.query({
fsym: 'ETH',
tsyms: 'USD'
})
.reply(200, '{"USD": 330.21}');

test('Defaults to USD', async t => {
t.plan(1);

const prices = await fn();
t.is(prices[0], 'USD: 330.21');
});

0 comments on commit 9de10b1

Please sign in to comment.