module Coinpare::Fetcher

Handle all data fetching from remote api

Constants

API_URL

Public Class Methods

fetch_json(url) click to toggle source
# 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) click to toggle source
# File lib/coinpare/fetcher.rb, line 55
def fetch_prices(from_symbols, to_symbols, options)
  url = ["#{API_URL}pricemultifull"]
  url << "?fsyms=#{from_symbols}&tsyms=#{to_symbols}"
  url << "&e=#{options['exchange']}" if options["exchange"]
  url << "&tryConversion=true"

  fetch_json(url.join)
end
fetch_top_coins_by_volume(to_symbol, options) click to toggle source
# File lib/coinpare/fetcher.rb, line 46
def fetch_top_coins_by_volume(to_symbol, options)
  url = ["#{API_URL}top/totalvol"]
  url << "?tsym=#{to_symbol}"
  url << "&limit=#{options['top']}&page=0" if options["top"]

  fetch_json(url.join)
end
fetch_top_exchanges_by_pair(from_symbol, to_symbol, options) click to toggle source
# File lib/coinpare/fetcher.rb, line 65
def fetch_top_exchanges_by_pair(from_symbol, to_symbol, options)
  url = ["#{API_URL}top/exchanges/full"]
  url << "?fsym=#{from_symbol}&tsym=#{to_symbol}"
  url << "&limit=#{options['top']}&page=0" if options["top"]

  fetch_json(url.join)
end
handle_response(response) click to toggle source
# File lib/coinpare/fetcher.rb, line 11
def handle_response(response)
  status = response["Response"]
  if status == "Error"
    puts response["Message"]
    exit
  end
  response
end

Public Instance Methods

fetch_daily_hist(from_symbol, to_symbol, options) click to toggle source
# File lib/coinpare/fetcher.rb, line 37
def fetch_daily_hist(from_symbol, to_symbol, options)
  url = ["#{API_URL}histoday"]
  url << "?fsym=#{from_symbol}&tsym=#{to_symbol}"
  url << "&limit=#{options['top']}" if options["top"]
  url << "&aggregate=7"

  fetch_json(url.join)
end

Private Instance Methods

fetch_json(url) click to toggle source
# 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) click to toggle source
# File lib/coinpare/fetcher.rb, line 55
def fetch_prices(from_symbols, to_symbols, options)
  url = ["#{API_URL}pricemultifull"]
  url << "?fsyms=#{from_symbols}&tsyms=#{to_symbols}"
  url << "&e=#{options['exchange']}" if options["exchange"]
  url << "&tryConversion=true"

  fetch_json(url.join)
end
fetch_top_coins_by_volume(to_symbol, options) click to toggle source
# File lib/coinpare/fetcher.rb, line 46
def fetch_top_coins_by_volume(to_symbol, options)
  url = ["#{API_URL}top/totalvol"]
  url << "?tsym=#{to_symbol}"
  url << "&limit=#{options['top']}&page=0" if options["top"]

  fetch_json(url.join)
end
fetch_top_exchanges_by_pair(from_symbol, to_symbol, options) click to toggle source
# File lib/coinpare/fetcher.rb, line 65
def fetch_top_exchanges_by_pair(from_symbol, to_symbol, options)
  url = ["#{API_URL}top/exchanges/full"]
  url << "?fsym=#{from_symbol}&tsym=#{to_symbol}"
  url << "&limit=#{options['top']}&page=0" if options["top"]

  fetch_json(url.join)
end
handle_response(response) click to toggle source
# File lib/coinpare/fetcher.rb, line 11
def handle_response(response)
  status = response["Response"]
  if status == "Error"
    puts response["Message"]
    exit
  end
  response
end