Class: CoinTools::CoinMarketCap
- Inherits:
-
Object
- Object
- CoinTools::CoinMarketCap
- Defined in:
- lib/cointools/coinmarketcap.rb
Defined Under Namespace
Constant Summary collapse
- BASE_URL =
"https://api.coinmarketcap.com"
- 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
- #get_all_prices(convert_to: nil, &block) ⇒ Object
- #get_price(coin_name, convert_to: nil) ⇒ Object
- #get_price_by_symbol(coin_symbol, convert_to: nil) ⇒ Object
- #get_price_for_listing(listing, convert_to: nil) ⇒ Object
- #id_map ⇒ Object
- #load_listings ⇒ Object
- #symbol_map ⇒ Object
Instance Method Details
#get_all_prices(convert_to: nil, &block) ⇒ Object
170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 |
# File 'lib/cointools/coinmarketcap.rb', line 170 def get_all_prices(convert_to: nil, &block) url = URI("#{BASE_URL}/v2/ticker/?structure=array&sort=id&limit=100") if convert_to validate_fiat_currency(convert_to) url.query += "&convert=#{convert_to.upcase}" else url.query += "&convert=BTC" end coins = [] loop do new_batch = fetch_full_ticker_page(url, convert_to, coins.length + 1, &block) if new_batch.empty? break else coins.concat(new_batch) end end coins.sort_by(&:rank) end |
#get_price(coin_name, convert_to: nil) ⇒ Object
119 120 121 122 123 124 125 126 127 128 |
# File 'lib/cointools/coinmarketcap.rb', line 119 def get_price(coin_name, convert_to: nil) raise InvalidSymbolError if coin_name.to_s.empty? validate_fiat_currency(convert_to) if convert_to listing = id_map[coin_name.to_s] raise InvalidSymbolError if listing.nil? get_price_for_listing(listing, convert_to: convert_to) end |
#get_price_by_symbol(coin_symbol, convert_to: nil) ⇒ Object
130 131 132 133 134 135 136 137 138 139 |
# File 'lib/cointools/coinmarketcap.rb', line 130 def get_price_by_symbol(coin_symbol, convert_to: nil) raise InvalidSymbolError if coin_symbol.to_s.empty? validate_fiat_currency(convert_to) if convert_to listing = symbol_map[coin_symbol.to_s] raise InvalidSymbolError if listing.nil? get_price_for_listing(listing, convert_to: convert_to) end |
#get_price_for_listing(listing, convert_to: nil) ⇒ Object
141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 |
# File 'lib/cointools/coinmarketcap.rb', line 141 def get_price_for_listing(listing, convert_to: nil) url = URI("#{BASE_URL}/v2/ticker/#{listing.numeric_id}/") if convert_to url.query = "convert=#{convert_to.upcase}" else url.query = "convert=BTC" end response = Request.get(url) case response when Net::HTTPSuccess record = parse_response(response, Hash) begin return CoinData.new(record, convert_to) rescue ArgumentError => e raise JSONError.new(response, e.) end when Net::HTTPNotFound raise UnknownCoinError.new(response) when Net::HTTPClientError raise BadRequestError.new(response) else raise ServiceUnavailableError.new(response) end end |
#id_map ⇒ Object
82 83 84 85 |
# File 'lib/cointools/coinmarketcap.rb', line 82 def id_map load_listings if @id_map.nil? @id_map end |
#load_listings ⇒ Object
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 116 117 |
# File 'lib/cointools/coinmarketcap.rb', line 87 def load_listings url = URI("#{BASE_URL}/v2/listings/") response = Request.get(url) case response when Net::HTTPSuccess data = parse_response(response, Array) @id_map = {} @symbol_map = {} begin data.each do |record| listing = Listing.new(record) @id_map[listing.text_id] = listing @symbol_map[listing.symbol] = listing end rescue ArgumentError => e # TODO: JSONError vs. NoDataError? + error class docs raise JSONError.new(response, e.) end data.length when Net::HTTPClientError raise BadRequestError.new(response) else raise ServiceUnavailableError.new(response) end end |
#symbol_map ⇒ Object
77 78 79 80 |
# File 'lib/cointools/coinmarketcap.rb', line 77 def symbol_map load_listings if @symbol_map.nil? @symbol_map end |