coinmarketcap_api

Coinmarketcap.com API wrapper in Ruby.

Gem Version Downloads GitHub license Maintainability Test Coverage Build Status

Installation

Add this line to your application's Gemfile:

gem 'coinmarketcap_api'

And then execute:

bundle install

Or install it yourself as:

gem install coinmarketcap_api

Usage

Make sure you require the library.

require 'coinmarketcap_api'

You can then make requests to the CoinMarketCap.request(type, options = {}) method.

Valid types in the types parameter are:

  • 'ticker'
  • 'global'

Default is 'ticker'.

Valid options in the options hash are:

  • :currency - [String], return results only for specified currency, for example 'bitcoin'.
  • :start - [Integer], return results from rank [start] and above.
  • :limit - [Integer], return a maximum of [limit] results (default is 100, use 0 to return all results).
  • :convert - [String], return price, daily_volume, and market cap in terms of another currency.
    • Valid values are: "AUD", "BRL", "CAD", "CHF", "CLP", "CNY", "CZK", "DKK", "EUR", "GBP", "HKD", "HUF", "IDR", "ILS", "INR", "JPY", "KRW", "MXN", "MYR", "NOK", "NZD", "PHP", "PKR", "PLN", "RUB", "SEK", "SGD", "THB", "TRY", "TWD", "ZAR".

Get top 100 currencies:

CoinMarketCap.request()

Get global data:

CoinMarketCap.request(type: 'global')

Get global data with additional properties comparing it to eur:

CoinMarketCap.request(type: 'global', options: {compare: 'eur'})

Get top 5 currencies:

CoinMarketCap.request(options: {limit: 5})

Get 5 currencies starting from 6th:

CoinMarketCap.request(options: {limit: 5, start: 5})

Get data for bitcoin:

CoinMarketCap.request(options: {currency: 'bitcoin'})

Get data for bitcoin with additional properties comparing it to eur:

CoinMarketCap.request(options: {currency: 'bitcoin', compare: 'eur'})

The request(...) method will return a response that you can interact with in a more-friendly way, such as:

request = CoinMarketCap.request(options: {currency: 'bitcoin'})
request.percent_change_hourly
request.rank
request = CoinMarketCap.request()
request.first.available_supply
request.first.symbol

Depending on request options, responce parameters may differ:

request = CoinMarketCap.request(type: 'ticker').first

# Type: 'ticker' request return [Array] of objects with following methods:

request.id
# => "bitcoin"
request.name
# => "Bitcoin"
request.symbol
# => "BTC"
request.rank
# => "1"
request.price_usd
# => "7952.59"
request.price_btc
# => "1.0"
request.daily_volume_usd
# => "4905290000.0"
request.market_cap_usd
# => "134748486145"
request.available_supply
# => "16943975.0"
request.total_supply
# => "16943975.0"
request.max_supply
# => "21000000.0"
request.percent_change_hourly
# => "0.16"
request.percent_change_daily
# => "-0.23"
request.percent_change_weekly
# => "-12.44"
request.last_updated
# => "1522227867" 
request = CoinMarketCap.request(type: 'ticker', options: {convert: 'eur'}).first

# When convert option specified, following methods added for each responce object:

request.price_eur
# => "6408.92070769",
request.daily_volume_eur
# => "3953129063.39",
request.market_cap_eur
# => "108592592248"

# _eur part may change depending on your options: convert value 
request = CoinMarketCap.request(type: 'ticker', options: {convert: 'aud'}).first

request.price_aud
# => "6408.92070769",
request.daily_volume_aud
# => "3953129063.39",
request.market_cap_aud
# => "108592592248"
request = CoinMarketCap.request(type: 'global')

# Type: 'global' requests return [Object] with following methods:

request.total_market_cap_usd
# => 301152981078.0
request.total_daily_volume_usd
# => 13548242090.0
request.bitcoin_percentage_of_market_cap
# => 44.74
request.active_currencies
# => 916
request.active_assets
# => 675
request.active_markets
# => 9753
request.last_updated
# => 1522227867 
request = CoinMarketCap.request(type: 'global', options: {convert: 'eur'})

# When convert option specified for type:global requests, 
# following methods added for a responce object:

request.total_market_cap_eur
# => 242696477074.0,
request.total_daily_volume_eur
# => 10918406367.0

# _eur part may change depending on your options: convert value 
request = CoinMarketCap.request(type: 'global', options: {convert: 'aud'})

request.total_market_cap_aud
# => 242696477074.0,
request.total_daily_volume_aud
# => 10918406367.0

Please refer to the coinmarketcap API documentation for more information on the full response properties.

NOTE!

Due to Ruby method naming restrictions coinmarketcap API properties which contain 24h, 1h, 7d was renamed to be daily, hourly and weekly respectively.

Configuration

The HTTP requests are made with Faraday, which uses Net::HTTP by default. Changing the adapter is easy. We will use typhoeus as an example.

Make sure to include the typhoeus gem in your Gemfile:

gem 'typhoeus'
require 'typhoeus/adapters/faraday'

Faraday.default_adapter = :typhoeus

Alternatively:

require 'typhoeus/adapters/faraday'

CoinMarketCap.connection = Faraday.new do |builder|
  builder.adapter :typhoeus
end

You can also customise the default parameters passed through on each API call:

CoinMarketCap.configure do |configuration|
  configuration.timeout = 500
end

Contributing to coinmarketcap_api

Bug Reports and Pull Requests are welcome on GitHub Project page.

This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the Contributor Covenant code of conduct.

Please read the CONTRIBUTING.md for details on our code of conduct, and the process for submitting pull requests to us.

License

The gem is available as open source under the terms of the MIT License.

Code of Conduct

Everyone interacting in the coinmarketcap_api project’s codebases, issue trackers, chat rooms and mailing lists is expected to follow the code of conduct.

Copyright (c) 2018 Yuriy Alekseyev. See LICENSE.txt for further details.