Skip to content

Commit

Permalink
Replace tryConversion parameter with an options object
Browse files Browse the repository at this point in the history
  • Loading branch information
RyanZim committed Feb 27, 2017
1 parent c37c21d commit ecc6590
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 20 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ Switched to https://min-api.cryptocompare.com/
- removed: `coinList()`
- changed: `price()` returns a different data structure
- changed: `priceHistorical()` returns a different data structure
- changed: `price()` and `priceHistorical)()` function signature has changed
- added: `priceMulti()`
- added: `priceFull()`
- added: `topPairs()`
Expand Down
20 changes: 12 additions & 8 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -40,11 +40,12 @@ Usage

Get the current price of any cryptocurrency in any other currency.

`price(fsym, tsyms[, tryConversion])`
`price(fsym, tsyms[, options])`

- `fsym` (String) From Symbol
- `tsyms` (Array of Strings | String) To Symbol(s)
- `tryConversion` (Boolean) By default, if the crypto does not trade directly into the toSymbol requested, BTC will be used for conversion. Set `tryConversion` to `false` to disable using BTC for conversion.
- `options` (Object)
- `tryConversion` (Boolean) By default, if the crypto does not trade directly into the toSymbol requested, BTC will be used for conversion. Set `tryConversion` to `false` to disable using BTC for conversion.

```js
const cc = require('cryptocompare')
Expand All @@ -70,11 +71,12 @@ cc.price('BTC', 'USD')

Works like `price()`, except it allows you to specify a matrix of From Symbols.

`priceMulti(fsyms, tsyms[, tryConversion])`
`priceMulti(fsyms, tsyms[, options])`

- `fsyms` (Array of Strings | String) From Symbol(s)
- `tsyms` (Array of Strings | String) To Symbol(s)
- `tryConversion` (Boolean) By default, if the crypto does not trade directly into the toSymbol requested, BTC will be used for conversion. Set `tryConversion` to `false` to disable using BTC for conversion.
- `options` (Object)
- `tryConversion` (Boolean) By default, if the crypto does not trade directly into the toSymbol requested, BTC will be used for conversion. Set `tryConversion` to `false` to disable using BTC for conversion.

```js
const cc = require('cryptocompare')
Expand All @@ -101,11 +103,12 @@ cc.priceMulti('BTC', 'USD')

Get all the current trading info (price, vol, open, high, low, etc.) of any list of cryptocurrencies in any other currency.

`priceFull(fsyms, tsyms[, tryConversion])`
`priceFull(fsyms, tsyms[, options])`

- `fsyms` (Array of Strings | String) From Symbol(s)
- `tsyms` (Array of Strings | String) To Symbol(s)
- `tryConversion` (Boolean) By default, if the crypto does not trade directly into the toSymbol requested, BTC will be used for conversion. Set `tryConversion` to `false` to disable using BTC for conversion.
- `options` (Object)
- `tryConversion` (Boolean) By default, if the crypto does not trade directly into the toSymbol requested, BTC will be used for conversion. Set `tryConversion` to `false` to disable using BTC for conversion.

```js
const cc = require('cryptocompare')
Expand Down Expand Up @@ -149,12 +152,13 @@ cc.priceFull(['BTC', 'ETH'], ['USD', 'EUR'])

Get the price of any cryptocurrency in any other currency at a given timestamp. The price comes from the daily info - so it would be the price at the end of the day GMT based on the requested timestamp.

`priceHistorical(fsym, tsyms, time[, tryConversion])`
`priceHistorical(fsym, tsyms, time[, options])`

- `fsym` (String) From Symbol
- `tsyms` (Array of Strings | String) To Symbol(s)
- `time` (Date) Date in history that you want price data for
- `tryConversion` (Boolean) By default, if the crypto does not trade directly into the toSymbol requested, BTC will be used for conversion. Set `tryConversion` to `false` to disable using BTC for conversion.
- `options` (Object)
- `tryConversion` (Boolean) By default, if the crypto does not trade directly into the toSymbol requested, BTC will be used for conversion. Set `tryConversion` to `false` to disable using BTC for conversion.

```js
const cc = require('cryptocompare')
Expand Down
20 changes: 12 additions & 8 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,29 +12,33 @@ function fetchJSON (url) {
})
}

function price (fsym, tsyms, tryConversion) {
function price (fsym, tsyms, options) {
options = options || {}
let url = `${baseUrl}price?fsym=${fsym}&tsyms=${tsyms}`
if (tryConversion === false) url += '&tryConversion=false'
if (options.tryConversion === false) url += '&tryConversion=false'
return fetchJSON(url)
}

function priceMulti (fsyms, tsyms, tryConversion) {
function priceMulti (fsyms, tsyms, options) {
options = options || {}
let url = `${baseUrl}pricemulti?fsyms=${fsyms}&tsyms=${tsyms}`
if (tryConversion === false) url += '&tryConversion=false'
if (options.tryConversion === false) url += '&tryConversion=false'
return fetchJSON(url)
}

function priceFull (fsyms, tsyms, tryConversion) {
function priceFull (fsyms, tsyms, options) {
options = options || {}
let url = `${baseUrl}pricemultifull?fsyms=${fsyms}&tsyms=${tsyms}`
if (tryConversion === false) url += '&tryConversion=false'
if (options.tryConversion === false) url += '&tryConversion=false'
// We want the RAW data, not the DISPLAY data:
return fetchJSON(url).then(result => result.RAW)
}

function priceHistorical (fsym, tsyms, time, tryConversion) {
function priceHistorical (fsym, tsyms, time, options) {
options = options || {}
time = dateToTimestamp(time)
let url = `${baseUrl}pricehistorical?fsym=${fsym}&tsyms=${tsyms}&ts=${time}`
if (tryConversion === false) url += '&tryConversion=false'
if (options.tryConversion === false) url += '&tryConversion=false'
// The API returns json with an extra layer of nesting, so remove it
return fetchJSON(url).then(result => result[fsym])
}
Expand Down
14 changes: 10 additions & 4 deletions test/cryptocompare.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ test('price() allows passing a string as the second parameter', t => {
})

test("price()'s tryConversion=false works", t => {
helpers.testTryConversion(cc.price(helpers.NOT_USD_TRADABLE, 'USD', false), t)
helpers.testTryConversion(cc.price(helpers.NOT_USD_TRADABLE, 'USD', { tryConversion: false }), t)
})

test('priceMulti()', t => {
Expand All @@ -46,7 +46,7 @@ test('priceMulti() allows passing strings instead of arrays', t => {
})

test("priceMulti()'s tryConversion=false works", t => {
helpers.testTryConversion(cc.priceMulti(helpers.NOT_USD_TRADABLE, 'USD', false), t)
helpers.testTryConversion(cc.priceMulti(helpers.NOT_USD_TRADABLE, 'USD', { tryConversion: false }), t)
})

test('priceFull()', t => {
Expand Down Expand Up @@ -80,7 +80,7 @@ test('priceFull() allows passing strings instead of arrays', t => {
})

test("priceFull()'s tryConversion=false works", t => {
helpers.testTryConversion(cc.priceFull(helpers.NOT_USD_TRADABLE, 'USD', false), t)
helpers.testTryConversion(cc.priceFull(helpers.NOT_USD_TRADABLE, 'USD', { tryConversion: false }), t)
})

test('priceHistorical()', t => {
Expand All @@ -97,7 +97,13 @@ test('priceHistorical()', t => {

test("priceHistorical()'s tryConversion=false works", t => {
const timestamp = new Date('2017-01-01')
helpers.testTryConversion(cc.priceHistorical(helpers.NOT_USD_TRADABLE, 'USD', timestamp, false), t)
helpers.testTryConversion(
cc.priceHistorical(
helpers.NOT_USD_TRADABLE,
'USD',
timestamp,
{ tryConversion: false }
), t)
})

test('generateAvg()', t => {
Expand Down

0 comments on commit ecc6590

Please sign in to comment.