Skip to content

Commit

Permalink
Merge pull request #14 from tiaanduplessis/fixv2
Browse files Browse the repository at this point in the history
Rewrote getTicker method for CMC API v2 (Breaking changes)
  • Loading branch information
tiaanduplessis committed May 15, 2018
2 parents 3ef066c + 565838b commit 39a881b
Show file tree
Hide file tree
Showing 4 changed files with 87 additions and 12 deletions.
10 changes: 8 additions & 2 deletions README.md
Expand Up @@ -77,6 +77,8 @@ client.getTicker().then(console.log).catch(console.error)
client.getGlobal().then(console.log).catch(console.error)
```

Check out the [CoinMarketCap API documentation](https://coinmarketcap.com/api/) for more information!

## API

<!-- Generated by documentation.js. Update this documentation by updating the source code. -->
Expand All @@ -100,7 +102,9 @@ client.getListings().then(console.log).catch(console.error)

### getTicker

Get ticker information
Get ticker information.
Start and limit options can only be used when currency or ID is not given.
Currency and ID cannot be passed in at the same time.

**Parameters**

Expand All @@ -110,15 +114,17 @@ Get ticker information
- `options.limit` **Int?** Only returns limit number of results
- `options.convert` **[String](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/String)?** Return price, 24h volume, and market cap in terms of another currency
- `options.currency` **[String](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/String)?** Return only specific currency
- `options.id` **Int?** Return only specific currency associated with its ID from listings

**Examples**

```javascript
const client = new CoinMarketCap()
client.getTicker({limit: 3}).then(console.log).catch(console.error)
client.getTicker({limit: 1, currency: 'bitcoin'}).then(console.log).catch(console.error)
client.getTicker({convert: 'EUR'}).then(console.log).catch(console.error)
client.getTicker({start: 0, limit: 5}).then(console.log).catch(console.error)
client.getTicker({currency: 'BTC'}).then(console.log).catch(console.error)
client.getTicker({convert: 'JPY', id: 2}).then(console.log).catch(console.error)
```

### getGlobal
Expand Down
22 changes: 17 additions & 5 deletions docs/index.html
Expand Up @@ -2,7 +2,7 @@
<html>
<head>
<meta charset='utf-8' />
<title>coinmarketcap-api 1.0.3 | Documentation</title>
<title>coinmarketcap-api 1.1.0 | Documentation</title>
<meta name='viewport' content='width=device-width,initial-scale=1'>
<link href='assets/bass.css' type='text/css' rel='stylesheet' />
<link href='assets/style.css' type='text/css' rel='stylesheet' />
Expand All @@ -14,7 +14,7 @@
<div id='split-left' class='overflow-auto fs0 height-viewport-100'>
<div class='py1 px2'>
<h3 class='mb0 no-anchor'>coinmarketcap-api</h3>
<div class='mb1'><code>1.0.3</code></div>
<div class='mb1'><code>1.1.0</code></div>
<input
placeholder='Filter'
id='filter-input'
Expand Down Expand Up @@ -130,7 +130,9 @@ <h3 class='fl m0' id='getticker'>
</div>


<p>Get ticker information</p>
<p>Get ticker information.
Start and limit options can only be used when currency or ID is not given.
Currency and ID cannot be passed in at the same time.</p>


<div class='pre p1 fill-light mt0'>getTicker(args: any, options: <a href="https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Object">Object</a>?)</div>
Expand Down Expand Up @@ -213,6 +215,15 @@ <h3 class='fl m0' id='getticker'>



<tr>
<td class='break-word'><span class='code bold'>options.id</span> <code class='quiet'>Int?</code>
</td>
<td class='break-word'><span>Return only specific currency associated with its ID from listings
</span></td>
</tr>



</tbody>
</table>

Expand All @@ -233,9 +244,10 @@ <h3 class='fl m0' id='getticker'>

<pre class='p1 overflow-auto round fill-light'><span class="hljs-keyword">const</span> client = <span class="hljs-keyword">new</span> CoinMarketCap()
client.getTicker({<span class="hljs-attr">limit</span>: <span class="hljs-number">3</span>}).then(<span class="hljs-built_in">console</span>.log).catch(<span class="hljs-built_in">console</span>.error)
client.getTicker({<span class="hljs-attr">limit</span>: <span class="hljs-number">1</span>, <span class="hljs-attr">currency</span>: <span class="hljs-string">'bitcoin'</span>}).then(<span class="hljs-built_in">console</span>.log).catch(<span class="hljs-built_in">console</span>.error)
client.getTicker({<span class="hljs-attr">convert</span>: <span class="hljs-string">'EUR'</span>}).then(<span class="hljs-built_in">console</span>.log).catch(<span class="hljs-built_in">console</span>.error)
client.getTicker({<span class="hljs-attr">start</span>: <span class="hljs-number">0</span>, <span class="hljs-attr">limit</span>: <span class="hljs-number">5</span>}).then(<span class="hljs-built_in">console</span>.log).catch(<span class="hljs-built_in">console</span>.error)</pre>
client.getTicker({<span class="hljs-attr">start</span>: <span class="hljs-number">0</span>, <span class="hljs-attr">limit</span>: <span class="hljs-number">5</span>}).then(<span class="hljs-built_in">console</span>.log).catch(<span class="hljs-built_in">console</span>.error)
client.getTicker({<span class="hljs-attr">currency</span>: <span class="hljs-string">'BTC'</span>}).then(<span class="hljs-built_in">console</span>.log).catch(<span class="hljs-built_in">console</span>.error)
client.getTicker({<span class="hljs-attr">convert</span>: <span class="hljs-string">'JPY'</span>, <span class="hljs-attr">id</span>: <span class="hljs-number">2</span>}).then(<span class="hljs-built_in">console</span>.log).catch(<span class="hljs-built_in">console</span>.error)</pre>



Expand Down
30 changes: 25 additions & 5 deletions index.js
Expand Up @@ -29,26 +29,46 @@ class CoinMarketCap {
}

/**
* Get ticker information
* Get ticker information.
* Start and limit options can only be used when currency or ID is not given.
* Currency and ID cannot be passed in at the same time.
*
* @param {Object=} options Options for the request:
* @param {Int=} options.start Return results from rank start + 1 and above
* @param {Int=} options.limit Only returns limit number of results
* @param {String=} options.convert Return price, 24h volume, and market cap in terms of another currency
* @param {String=} options.currency Return only specific currency
* @param {Int=} options.id Return only specific currency associated with its ID from listings
*
* @example
* const client = new CoinMarketCap()
* client.getTicker({limit: 3}).then(console.log).catch(console.error)
* client.getTicker({limit: 1, currency: 'bitcoin'}).then(console.log).catch(console.error)
* client.getTicker({convert: 'EUR'}).then(console.log).catch(console.error)
* client.getTicker({start: 0, limit: 5}).then(console.log).catch(console.error)
* client.getTicker({currency: 'BTC'}).then(console.log).catch(console.error)
* client.getTicker({convert: 'JPY', id: 2}).then(console.log).catch(console.error)
*/
getTicker (args = {}) {
const { start, limit, convert, currency } = args
async getTicker (args = {}) {
let { start, limit, convert, currency, id } = args

if ((currency || id) && (start || limit)) {
throw new Error(
'Start and limit options can only be used when currency or ID is not given.'
)
}
if (currency && id) {
throw new Error('Currency and ID cannot be passed in at the same time.')
}

if (currency) {
let listings = await this.getListings()
id = listings.data.find(
listing => listing.symbol === currency.toUpperCase()
).id
}

return createRequest({
url: `${this.url}/ticker${currency ? `/${currency}/`.toLowerCase() : ''}`,
url: `${this.url}/ticker/${id ? `${id}/` : ''}`,
headers: this.headers,
query: { start, convert, limit }
})
Expand Down
37 changes: 37 additions & 0 deletions test.js
Expand Up @@ -21,6 +21,42 @@ test('should get latest ticker', async () => {
expect(Object.keys(ticker2.data).length).toBe(10)
})

test('should get Bitcoin ticker from currency symbol', async () => {
const client = new CoinMarketCap()
const ticker = await client.getTicker({currency: 'BTC'})

expect(typeof ticker).toBe('object')
expect(ticker.data.id).toBe(1)
expect(ticker.data.name).toMatch('Bitcoin')
expect(ticker.data.symbol).toMatch('BTC')
})

test('should get Bitcoin ticker from its listings ID', async () => {
const client = new CoinMarketCap()
const ticker = await client.getTicker({ id: 1 })

expect(typeof ticker).toBe('object')
expect(ticker.data.id).toBe(1)
expect(ticker.data.name).toMatch('Bitcoin')
expect(ticker.data.symbol).toMatch('BTC')
})

test('should result in errors due to wrong parameter usage', async () => {
const client = new CoinMarketCap()

try {
await client.getTicker({currency: 'BTC', id: 1})
} catch (e) {
expect(e.toString()).toMatch('Error: Currency and ID cannot be passed in at the same time.')
}

try {
await client.getTicker({start: 1, currency: 'ETH'})
} catch (e) {
expect(e.toString()).toMatch('Error: Start and limit options can only be used when currency or ID is not given.')
}
})

test('should get latest global', async () => {
const client = new CoinMarketCap()
const global = await client.getGlobal()
Expand All @@ -33,5 +69,6 @@ test(`should get latest listings`, async () => {
const listings = await client.getListings()

expect(typeof listings).toBe('object')
expect(Array.isArray(listings.data)).toBe(true)
})

0 comments on commit 39a881b

Please sign in to comment.