Module: CoingeckoRuby::Client::Coins

Included in:
CoingeckoRuby::Client
Defined in:
lib/coingecko_ruby/client/coins.rb

Instance Method Summary collapse

Instance Method Details

#coin(id, **options) ⇒ Hash

Fetches detailed current data for a coin.

Examples:

Fetch Bitcoin’s current data.

client.coin('bitcoin')

Parameters:

  • id (String)

    the coin id to fetch.

  • options (Hash)

    a customizable set of options

Options Hash (**options):

  • :tickers (Boolean) — default: true

    include ticker data (e.g. BTC-ETH).

  • :market_data (Boolean) — default: true

    include market data (e.g. market cap).

  • :community_data (Boolean) — default: true

    include community data (e.g. subreddit info, discussion forums).

  • :developer_data (Boolean) — default: true

    include developer data (e.g. Github stats).

  • :sparkline (Boolean) — default: false

    include sparkline data from the last 7 days.

Returns:

  • (Hash)

    returns comprehensive current data for the given coin.



40
41
42
# File 'lib/coingecko_ruby/client/coins.rb', line 40

def coin(id, **options)
  get "coins/#{id}", **options
end

#coins_list(**options) ⇒ Array<Hash>

Fetches the id, name, and symbol of every coin supported by CoinGecko’s API.

Examples:

Fetch the list of coins supported by CoinGecko’s API.

client.coins_list

Sample response object (truncated)

[
  {
    "id" => "01coin",
    "symbol" => "zoc",
    "name" => "01coin"
  }, {
    "id" => "0-5x-long-algorand-token",
    "symbol" => "algohalf",
    "name" => "0.5X Long Algorand Token"
  }, {
]

Parameters:

  • options (Hash)

    a customizable set of options

Options Hash (**options):

  • include_platform (Boolean)

    displays the coin’s platform contract address (e.g. 0x… for ETH-based tokens)

Returns:

  • (Array<Hash>)

    returns an array of hashes for every supported coin’s id, name, symbol, and platform contract address (optional).



24
25
26
# File 'lib/coingecko_ruby/client/coins.rb', line 24

def coins_list(**options)
  get 'coins/list', **options
end

#get_coin_data(id:, options: {}) ⇒ Object

Deprecated.

Use #coin instead



45
46
47
# File 'lib/coingecko_ruby/client/coins.rb', line 45

def get_coin_data(id:, options: {})
  coin(id, **options)
end

#get_markets(ids:, currency: 'usd', options: {}) ⇒ Object

Deprecated.

Use #markets instead



223
224
225
# File 'lib/coingecko_ruby/client/coins.rb', line 223

def get_markets(ids:, currency: 'usd', options: {})
  markets(ids, vs_currency: currency, **options)
end

#get_tickers(id:, options: {}) ⇒ Object

Deprecated.

Use #tickers instead



135
136
137
# File 'lib/coingecko_ruby/client/coins.rb', line 135

def get_tickers(id:, options: {})
  tickers(id, **options)
end

#markets(ids, **options) ⇒ Array<Hash>

Fetches market data for a coin or a list of coins.

Examples:

Fetch market data for Bitcoin and Ethereum in USD.

client.markets('bitcoin, ethereum', vs_currency: 'gbp')

Sample response object

[{
    "id" => "bitcoin",
    "symbol" => "btc",
    "name" => "Bitcoin",
    "image" =>
    "https://assets.coingecko.com/coins/images/1/large/bitcoin.png?1547033579",
    "current_price" => 34781,
    "market_cap" => 651147609834,
    "market_cap_rank" => 1,
    "fully_diluted_valuation" => 730792236092,
    "total_volume" => 47231436126,
    "high_24h" => 35335,
    "low_24h" => 33105,
    "price_change_24h" => 41.43,
    "price_change_percentage_24h" => 0.11926,
    "market_cap_change_24h" => 1033983115,
    "market_cap_change_percentage_24h" => 0.15905,
    "circulating_supply" => 18711337.0,
    "total_supply" => 21000000.0,
    "max_supply" => 21000000.0,
    "ath" => 47095,
    "ath_change_percentage" => -26.10691,
    "ath_date" => "2021-04-14T11:54:46.763Z",
    "atl" => 43.9,
    "atl_change_percentage" => 79166.25611,
    "atl_date" => "2013-07-05T00:00:00.000Z",
    "roi" => nil,
    "last_updated" => "2021-05-16T07:07:38.393Z"
  },
  {
    "id" => "ethereum",
    "symbol" => "eth",
    "name" => "Ethereum",
    "image" =>
    "https://assets.coingecko.com/coins/images/279/large/ethereum.png?1595348880",
    "current_price" => 2737.81,
    "market_cap" => 318383298158,
    "market_cap_rank" => 2,
    "fully_diluted_valuation" => nil,
    "total_volume" => 43302287477,
    "high_24h" => 2812.64,
    "low_24h" => 2579.9,
    "price_change_24h" => -67.10729241,
    "price_change_percentage_24h" => -2.39249,
    "market_cap_change_24h" => -7076221191.603394,
    "market_cap_change_percentage_24h" => -2.17422,
    "circulating_supply" => 115894196.749,
    "total_supply" => nil,
    "max_supply" => nil,
    "ath" => 3090.18,
    "ath_change_percentage" => -11.07563,
    "ath_date" => "2021-05-12T14:41:48.623Z",
    "atl" => 0.280314,
    "atl_change_percentage" => 980202.46141,
    "atl_date" => "2015-10-20T00:00:00.000Z",
    "roi" => {
      "times" => 104.29403414512163,
      "currency" => "btc",
      "percentage" => 10429.403414512162
    },
    "last_updated" => "2021-05-16T07:06:00.946Z"
  }
]

Parameters:

  • ids (String)

    the coin id or ids to fetch.

  • options (Hash)

    a customizable set of options

Options Hash (**options):

  • :vs_currency (String)

    the currency to display market price data.

  • :category (String)

    filter results by the given coin category.

  • :order (String) — default: 'market_cap_desc'

    sets the sort order for results. Valid values: market_cap_desc, gecko_desc, gecko_asc, market_cap_asc, market_cap_desc, volume_asc, volume_desc, id_asc, id_desc.

  • :per_page (Integer) — default: 100

    sets the number of results to return per page.

  • :page (Integer)

    sets the page for results.

  • :sparkline (Boolean) — default: false

    include sparkline data from the last 7 days.

  • :price_change_percentage (String)

    include price change percentage for each coin. Valid values: 1h, 24h, 7d, 14d, 30d, 200d, 1y.

Returns:

  • (Array<Hash>)

    returns market data for the given coin or coins.



218
219
220
# File 'lib/coingecko_ruby/client/coins.rb', line 218

def markets(ids, **options)
  get 'coins/markets', ids: ids, **options
end

#tickers(id, **options) ⇒ Hash

Fetches the list of tickers (e.g: BTC-USD) for a coin

Examples:

Fetch Bitcoin’s tickers.

client.tickers('bitcoin')

Sample response object (truncated)

{
  "name" => "Bitcoin",
  "tickers" => [{
    "base" => "BTC",
    "target" => "USDT",
    "market" => {
      "name" => "FTX.US",
      "identifier" => "ftx_us",
      "has_trading_incentive" => false
    },
    "last" => 48953.0,
    "volume" => 206.41378108798236,
    "converted_last" => {
      "btc" => 0.99774619, "eth" => 12.641434, "usd" => 48956
    },
    "converted_volume" => {
      "btc" => 205.949, "eth" => 2609, "usd" => 10105129
    },
    "trust_score" => "green",
    "bid_ask_spread_percentage" => 0.059238,
    "timestamp" => "2021-05-16T07:02:40+00:00",
    "last_traded_at" => "2021-05-16T07:02:40+00:00",
    "last_fetch_at" => "2021-05-16T07:02:40+00:00",
    "is_anomaly" => false,
    "is_stale" => false,
    "trade_url" => "https://ftx.us/trade/BTC/USDT",
    "token_info_url" => nil,
    "coin_id" => "bitcoin",
    "target_coin_id" => "tether"
  }],
}

Fetch Bitcoin’s tickers from Binance with 2% orderbook depth data.

client.tickers('bitcoin', exchange_id: 'binance', depth: true)

Sample response object (truncated)

{
  "name" => "Bitcoin",
  "tickers" => [{
    "base" => "BTC",
    "target" => "USDT",
    "market" => {
      "name" => "FTX.US",
      "identifier" => "ftx_us",
      "has_trading_incentive" => false
    },
    "last" => 48953.0,
    "volume" => 206.41378108798236,
    "cost_to_move_up_usd" => 41991391.6823387,
    "cost_to_move_down_usd" => 44281410.3725698,
    "converted_last" => {
      "btc" => 0.99774619, "eth" => 12.641434, "usd" => 48956
    },
    "converted_volume" => {
      "btc" => 205.949, "eth" => 2609, "usd" => 10105129
    },
    "trust_score" => "green",
    "bid_ask_spread_percentage" => 0.059238,
    "timestamp" => "2021-05-16T07:02:40+00:00",
    "last_traded_at" => "2021-05-16T07:02:40+00:00",
    "last_fetch_at" => "2021-05-16T07:02:40+00:00",
    "is_anomaly" => false,
    "is_stale" => false,
    "trade_url" => "https://ftx.us/trade/BTC/USDT",
    "token_info_url" => nil,
    "coin_id" => "bitcoin",
    "target_coin_id" => "tether"
  }],
}

Parameters:

  • id (String)

    the coin id to fetch.

  • options (Hash)

    a customizable set of options

Options Hash (**options):

  • :exchange_ids (String)

    filter ticker results from the given exchange ids (e.g. ‘binance, bitfinex’).

  • :include_exchange_logo (Boolean)

    flag to show the exchange’s logo.

  • :page (Integer)

    sets the page for results.

  • :order (String) — default: 'trust_score_desc'

    sets the sort order for results. Valid values: trust_score_desc’, ‘trust_score_asc’, ‘volume_desc.

  • :depth (Boolean) — default: false

    displays orderbook depth (2%).

Returns:

  • (Hash)

    returns the list of tickers for the given coin.



130
131
132
# File 'lib/coingecko_ruby/client/coins.rb', line 130

def tickers(id, **options)
  get "coins/#{id}/tickers", **options
end