Class: Capwatch::Providers::CoinMarketCap
- Inherits:
-
Object
- Object
- Capwatch::Providers::CoinMarketCap
- Defined in:
- lib/capwatch/providers/coin_market_cap.rb
Constant Summary collapse
- NoCoinInProvider =
Class.new(RuntimeError)
- TICKER_URL =
"https://api.coinmarketcap.com/v1/ticker/"
Instance Attribute Summary collapse
-
#body ⇒ Object
Returns the value of attribute body.
Instance Method Summary collapse
- #fetch ⇒ Object
- #fetched_json ⇒ Object
- #parse(response) ⇒ Object
- #update_coin(coin) ⇒ Object
- #update_rates(response) ⇒ Object
Instance Attribute Details
#body ⇒ Object
Returns the value of attribute body.
10 11 12 |
# File 'lib/capwatch/providers/coin_market_cap.rb', line 10 def body @body end |
Instance Method Details
#fetch ⇒ Object
22 23 24 |
# File 'lib/capwatch/providers/coin_market_cap.rb', line 22 def fetch @body ||= open(TICKER_URL).read end |
#fetched_json ⇒ Object
16 17 18 19 20 |
# File 'lib/capwatch/providers/coin_market_cap.rb', line 16 def fetched_json response = parse(fetch) update_rates(response) response end |
#parse(response) ⇒ Object
26 27 28 |
# File 'lib/capwatch/providers/coin_market_cap.rb', line 26 def parse(response) JSON.parse(response) end |
#update_coin(coin) ⇒ Object
39 40 41 42 43 44 45 46 47 48 |
# File 'lib/capwatch/providers/coin_market_cap.rb', line 39 def update_coin(coin) provider_coin = fetched_json.find { |c| c["symbol"] == coin.symbol } fail NoCoinInProvider, "No #{coin.symbol} in provider response" if provider_coin.nil? coin.name = provider_coin["name"] coin.price_usd = provider_coin["price_usd"].to_f coin.price_btc = provider_coin["price_btc"].to_f coin.percent_change_1h = provider_coin["percent_change_1h"].to_f coin.percent_change_24h = provider_coin["percent_change_24h"].to_f coin.percent_change_7d = provider_coin["percent_change_7d"].to_f end |
#update_rates(response) ⇒ Object
30 31 32 33 34 35 36 37 |
# File 'lib/capwatch/providers/coin_market_cap.rb', line 30 def update_rates(response) response.each do |coin_json| Capwatch::Exchange.rate( coin_json['symbol'], coin_json["price_btc"].to_f ) end end |