Class: CoinTools::CoinMarketCap

Inherits:
Object
  • Object
show all
Defined in:
lib/cointools/coinmarketcap.rb

Defined Under Namespace

Classes: BadRequestException, DataPoint, InvalidDateException, InvalidFiatCurrencyException, InvalidResponseException, NoDataException

Constant Summary collapse

BASE_URL =
"https://api.coinmarketcap.com"
USER_AGENT =
"cointools/#{CoinTools::VERSION}"
FIAT_CURRENCIES =
[
  'AUD', 'BRL', 'CAD', 'CHF', 'CLP', 'CNY', 'CZK', 'DKK', 'EUR', 'GBP',
  'HKD', 'HUF', 'IDR', 'ILS', 'INR', 'JPY', 'KRW', 'MXN', 'MYR', 'NOK',
  'NZD', 'PHP', 'PKR', 'PLN', 'RUB', 'SEK', 'SGD', 'THB', 'TRY', 'TWD',
  'ZAR'
]

Instance Method Summary collapse

Instance Method Details

#get_price(coin_name, convert_to: nil) ⇒ Object



51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
# File 'lib/cointools/coinmarketcap.rb', line 51

def get_price(coin_name, convert_to: nil)
  url = URI("#{BASE_URL}/v1/ticker/#{coin_name}/")

  if convert_to
    validate_fiat_currency(convert_to)
    url += "?convert=#{convert_to}"
  end

  response = make_request(url)

  case response
  when Net::HTTPSuccess
    json = JSON.load(response.body)
    record = json[0]

    usd_price = record['price_usd']&.to_f
    btc_price = record['price_btc']&.to_f
    timestamp = Time.at(record['last_updated'].to_i)

    if convert_to
      converted_price = record["price_#{convert_to.downcase}"]&.to_f
      raise NoDataException.new('Conversion to chosen fiat currency failed') if converted_price.nil?
    end

    return DataPoint.new(timestamp, usd_price, btc_price, converted_price)
  when Net::HTTPBadRequest
    raise BadRequestException.new(response)
  else
    raise InvalidResponseException.new(response)
  end
end

#get_price_by_symbol(coin_symbol, convert_to: nil) ⇒ Object



83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
# File 'lib/cointools/coinmarketcap.rb', line 83

def get_price_by_symbol(coin_symbol, convert_to: nil)
  url = URI("#{BASE_URL}/v1/ticker/?limit=0")
  symbol = coin_symbol.downcase

  if convert_to
    validate_fiat_currency(convert_to)
    url += "&convert=#{convert_to}"
  end

  response = make_request(url)

  case response
  when Net::HTTPSuccess
    json = JSON.load(response.body)
    record = json.detect { |r| r['symbol'].downcase == symbol }
    raise NoDataException.new('No coin found with given symbol') if record.nil?

    usd_price = record['price_usd']&.to_f
    btc_price = record['price_btc']&.to_f
    timestamp = Time.at(record['last_updated'].to_i)

    if convert_to
      converted_price = record["price_#{convert_to.downcase}"]&.to_f
      raise NoDataException.new('Conversion to chosen fiat currency failed') if converted_price.nil?
    end

    return DataPoint.new(timestamp, usd_price, btc_price, converted_price)
  when Net::HTTPBadRequest
    raise BadRequestException.new(response)
  else
    raise Exception.new(response)
  end
end