Rails:访问CoinmarketCap API

时间:2017-10-29 01:35:19

标签: ruby-on-rails json ruby api

我想访问coinmarketcap结果

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

这是结果。是一个哈希数组。有一种方法可以访问每个哈希而不是数字(Array [0]),因为这些数字每天都在变化,但是像[[= id ='比特币']或类似的东西?

[
{
    "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"
}
]

2 个答案:

答案 0 :(得分:1)

鉴于你的数组:

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"
  }
]

你可以这样做:

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

哪将返回比特币哈希:

 => {
      :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"
    }

我不知道这些哈希值是否每天都在变化,但如果您想根据给定日期的内容迭代它们,您可以执行以下操作:

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

答案 1 :(得分:0)

您可以使用他们的代码(特定货币)API

端点:/ 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"
        }
    ]

来源:https://coinmarketcap.com/api/