Module: Coinpare::Fetcher
- Defined in:
- lib/coinpare/fetcher.rb
Overview
Handle all data fetching from remote api
Constant Summary collapse
- API_URL =
"https://min-api.cryptocompare.com/data/"
Class Method Summary collapse
- .fetch_json(url) ⇒ Object
- .fetch_prices(from_symbols, to_symbols, options) ⇒ Object
- .fetch_top_coins_by_volume(to_symbol, options) ⇒ Object
- .fetch_top_exchanges_by_pair(from_symbol, to_symbol, options) ⇒ Object
- .handle_response(response) ⇒ Object
Instance Method Summary collapse
Class Method Details
.fetch_json(url) ⇒ Object
21 22 23 24 25 26 27 28 29 30 31 32 33 34 |
# File 'lib/coinpare/fetcher.rb', line 21 def fetch_json(url) uri = URI.parse(url) http = Net::HTTP.new(uri.host, uri.port) http.read_timeout = 5 http.use_ssl = uri.scheme == "https" response = http.start { |req| req.get(uri.request_uri) } case response when Net::HTTPSuccess handle_response(JSON.parse(response.body)) else response.value end rescue Net::ReadTimeout end |
.fetch_prices(from_symbols, to_symbols, options) ⇒ Object
55 56 57 58 59 60 61 62 |
# File 'lib/coinpare/fetcher.rb', line 55 def fetch_prices(from_symbols, to_symbols, ) url = ["#{API_URL}pricemultifull"] url << "?fsyms=#{from_symbols}&tsyms=#{to_symbols}" url << "&e=#{options['exchange']}" if ["exchange"] url << "&tryConversion=true" fetch_json(url.join) end |
.fetch_top_coins_by_volume(to_symbol, options) ⇒ Object
46 47 48 49 50 51 52 |
# File 'lib/coinpare/fetcher.rb', line 46 def fetch_top_coins_by_volume(to_symbol, ) url = ["#{API_URL}top/totalvol"] url << "?tsym=#{to_symbol}" url << "&limit=#{options['top']}&page=0" if ["top"] fetch_json(url.join) end |
.fetch_top_exchanges_by_pair(from_symbol, to_symbol, options) ⇒ Object
65 66 67 68 69 70 71 |
# File 'lib/coinpare/fetcher.rb', line 65 def fetch_top_exchanges_by_pair(from_symbol, to_symbol, ) url = ["#{API_URL}top/exchanges/full"] url << "?fsym=#{from_symbol}&tsym=#{to_symbol}" url << "&limit=#{options['top']}&page=0" if ["top"] fetch_json(url.join) end |
.handle_response(response) ⇒ Object
11 12 13 14 15 16 17 18 |
# File 'lib/coinpare/fetcher.rb', line 11 def handle_response(response) status = response["Response"] if status == "Error" puts response["Message"] exit end response end |
Instance Method Details
#fetch_daily_hist(from_symbol, to_symbol, options) ⇒ Object
37 38 39 40 41 42 43 44 |
# File 'lib/coinpare/fetcher.rb', line 37 def fetch_daily_hist(from_symbol, to_symbol, ) url = ["#{API_URL}histoday"] url << "?fsym=#{from_symbol}&tsym=#{to_symbol}" url << "&limit=#{options['top']}" if ["top"] url << "&aggregate=7" fetch_json(url.join) end |