Mr McDonald
Mr McDonald

Reputation: 483

Rails: Access to CoinmarketCap API

I want to access to coinmarketcap result

https://api.coinmarketcap.com/v1/ticker/?limit=3

This is the result. Is a Array of Hashes. There is a way to access to each hash not with number (Array[0]) because that numbers change everyday, but something like Array[:id = 'bitcoin'] or something similar?

[
{
    "id": "bitcoin", 
    "name": "Bitcoin", 
    "symbol": "BTC", 
    "rank": "1", 
    "price_usd": "5751.67", 
    "price_btc": "1.0", 
    "24h_volume_usd": "1404240000.0", 
    "market_cap_usd": "95774433400.0", 
    "available_supply": "16651587.0", 
    "total_supply": "16651587.0", 
    "percent_change_1h": "0.14", 
    "percent_change_24h": "-1.5", 
    "percent_change_7d": "-4.24", 
    "last_updated": "1509240553"
}, 
{
    "id": "ethereum", 
    "name": "Ethereum", 
    "symbol": "ETH", 
    "rank": "2", 
    "price_usd": "298.258", 
    "price_btc": "0.0519853", 
    "24h_volume_usd": "267318000.0", 
    "market_cap_usd": "28449497378.0", 
    "available_supply": "95385530.0", 
    "total_supply": "95385530.0", 
    "percent_change_1h": "0.58", 
    "percent_change_24h": "0.13", 
    "percent_change_7d": "-0.74", 
    "last_updated": "1509240550"
}, 
{
    "id": "ripple", 
    "name": "Ripple", 
    "symbol": "XRP", 
    "rank": "3", 
    "price_usd": "0.20106", 
    "price_btc": "0.00003504", 
    "24h_volume_usd": "28961600.0", 
    "market_cap_usd": "7747151216.0", 
    "available_supply": "38531538922.0", 
    "total_supply": "99993667738.0", 
    "percent_change_1h": "-0.21", 
    "percent_change_24h": "-0.97", 
    "percent_change_7d": "-2.36", 
    "last_updated": "1509240541"
}
]

Upvotes: 0

Views: 1225

Answers (2)

jvillian
jvillian

Reputation: 20253

Given your array:

ary = [
  {
    "id": "bitcoin", 
    "name": "Bitcoin", 
    "symbol": "BTC", 
    "rank": "1", 
    "price_usd": "5751.67", 
    "price_btc": "1.0", 
    "24h_volume_usd": "1404240000.0", 
    "market_cap_usd": "95774433400.0", 
    "available_supply": "16651587.0", 
    "total_supply": "16651587.0", 
    "percent_change_1h": "0.14", 
    "percent_change_24h": "-1.5", 
    "percent_change_7d": "-4.24", 
    "last_updated": "1509240553"
  }, 
  {
    "id": "ethereum", 
    "name": "Ethereum", 
    "symbol": "ETH", 
    "rank": "2", 
    "price_usd": "298.258", 
    "price_btc": "0.0519853", 
    "24h_volume_usd": "267318000.0", 
    "market_cap_usd": "28449497378.0", 
    "available_supply": "95385530.0", 
    "total_supply": "95385530.0", 
    "percent_change_1h": "0.58", 
    "percent_change_24h": "0.13", 
    "percent_change_7d": "-0.74", 
    "last_updated": "1509240550"
  }, 
  {
    "id": "ripple", 
    "name": "Ripple", 
    "symbol": "XRP", 
    "rank": "3", 
    "price_usd": "0.20106", 
    "price_btc": "0.00003504", 
    "24h_volume_usd": "28961600.0", 
    "market_cap_usd": "7747151216.0", 
    "available_supply": "38531538922.0", 
    "total_supply": "99993667738.0", 
    "percent_change_1h": "-0.21", 
    "percent_change_24h": "-0.97", 
    "percent_change_7d": "-2.36", 
    "last_updated": "1509240541"
  }
]

You can do:

ary.find{|hsh| hsh[:id] == 'bitcoin'}

Which will return the bitcoin hash:

 => {
      :id=>"bitcoin", 
      :name=>"Bitcoin", 
      :symbol=>"BTC", 
      :rank=>"1", 
      :price_usd=>"5751.67", 
      :price_btc=>"1.0", 
      :"24h_volume_usd"=>"1404240000.0", 
      :market_cap_usd=>"95774433400.0", 
      :available_supply=>"16651587.0", 
      :total_supply=>"16651587.0", 
      :percent_change_1h=>"0.14", 
      :percent_change_24h=>"-1.5", 
      :percent_change_7d=>"-4.24", 
      :last_updated=>"1509240553"
    }

I don't know if those hashes change from day to day, but if you want to iterate over them based on the content on a given day, you could do something like:

ary.map{ |hsh| hsh[:id] }.each do |id|
  ary.find{ |hsh| hsh[:id] == id }.tap do |hsh|
    #do something clever with hsh
  end
end

Upvotes: 2

Andrew
Andrew

Reputation: 810

You can use their Ticker (Specific Currency) API

Endpoint: /ticker/{id}/

https://api.coinmarketcap.com/v1/ticker/bitcoin/

[
{
  "id": "bitcoin", 
  "name": "Bitcoin", 
  "symbol": "BTC", 
  "rank": "1", 
  "price_usd": "573.137", 
  "price_btc": "1.0", 
  "24h_volume_usd": "72855700.0", 
  "market_cap_usd": "9080883500.0", 
  "available_supply": "15844176.0", 
  "total_supply": "15844176.0", 
  "percent_change_1h": "0.04", 
  "percent_change_24h": "-0.3", 
  "percent_change_7d": "-0.57", 
  "last_updated": "1472762067"
}
]

https://api.coinmarketcap.com/v1/ticker/ethereum/

    [
        {
            "id": "ethereum", 
            "name": "Ethereum", 
            "symbol": "ETH", 
            "rank": "2", 
            "price_usd": "297.64", 
            "price_btc": "0.0518885", 
            "24h_volume_usd": "267038000.0", 
            "market_cap_usd": "28390640590.0", 
            "available_supply": "95385837.0", 
            "total_supply": "95385837.0", 
            "percent_change_1h": "0.36", 
            "percent_change_24h": "-0.05", 
            "percent_change_7d": "-0.95", 
            "last_updated": "1509241149"
        }
    ]

Source: https://coinmarketcap.com/api/

Upvotes: 0

Related Questions