class CoinmarketcapLite

Public Class Methods

new(apikey: '', apiurl: 'https://pro-api.coinmarketcap.com', apibase: '/v1/cryptocurrency', filepath: '.', dym: true) click to toggle source
# File lib/coinmarketcap_lite.rb, line 21
def initialize(apikey: '', apiurl: 'https://pro-api.coinmarketcap.com', 
                apibase: '/v1/cryptocurrency', filepath: '.', dym: true)

  @url_base = apiurl + apibase
  @apikey = apikey
  @filepath = filepath
  
  file = 'coinmarketlite.dat'
  if File.exists? file then
    
    File.open(File.join(@filepath, file)) do |f|  
      @list = Marshal.load(f)  
    end
      
  else

    r = get_map()
    puts 'r: ' +r.inspect[0..2000]
    #exit
    @list = r['data']

    File.open(File.join(@filepath, file), 'w+') do |f|  
      Marshal.dump(@list, f)  
    end      

  end
  
  if dym then

    puts 'loading did_you_mean ...'.info if @debug          

    @dym = DidYouMean::SpellChecker.new(dictionary: @list.flat_map \
              {|x| [x['symbol'], x['name']]})
  end    

end

Public Instance Methods

coins(symbols=[]) click to toggle source

symbols = %w(BTC USDT BNB)

# File lib/coinmarketcap_lite.rb, line 60
def coins(symbols=[])

  if symbols.any? then
    get_map(symbols)
  else
    # return the top 100 coins latest prices
    api_call('/listings/latest',
                {"convert" => "USD,BTC", "limit" => "1","start" => "1"})
  end

end
find_coin(coin_name) click to toggle source
# File lib/coinmarketcap_lite.rb, line 72
def find_coin(coin_name)

  #return coin_name unless @autofind

  s = coin_name.to_s.downcase
  puts 's: ' + s.inspect if @debug
  r = @list.find {|coin| coin['symbol'].downcase == s || coin['name'].downcase == s}
  puts 'r: ' + r.inspect if @debug
  
  if r.nil? then

    if @dym then

      suggestion = @dym.correct coin_name
      raise CoinmarketcapLiteException, "unknown coin or token name. \n"  \
          + "Did you mean %s?" % [suggestion.first]

    else

      raise CoinmarketcapLiteException, "unknown coin or token name."

    end

  end

  r

end
find_id(name) click to toggle source
# File lib/coinmarketcap_lite.rb, line 101
def find_id(name)
  r = find_coin(name)
  r['id']
end
find_name(s) click to toggle source
# File lib/coinmarketcap_lite.rb, line 106
def find_name(s)
  r = find_coin s
  r['name']
end
get_historical_price() click to toggle source
# File lib/coinmarketcap_lite.rb, line 111
def get_historical_price()
end
prices(coins=[]) click to toggle source
# File lib/coinmarketcap_lite.rb, line 114
def prices(coins=[])
  a = quotes(coins)
end
quotes(coins=[]) click to toggle source
# File lib/coinmarketcap_lite.rb, line 118
def quotes(coins=[])
  
  coins = [coins] if coins.is_a? String
  ids = coins.map {|x| find_id(x)}
  api_call '/quotes/latest?id=' + ids.compact.join(',')    
end

Private Instance Methods

api_call(api, data={}) click to toggle source
# File lib/coinmarketcap_lite.rb, line 135
def api_call(api, data={})
  
  url = @url_base + api
  h = {}
  h["X-CMC_PRO_API_KEY"] = @apikey
  h["Accept"] = "application/json"
  connection = Excon.new(url, :headers => h)
  r = connection.request(:method => 'GET')
  #return r
  return JSON.parse(r.body)    

end
get_map(symbols=[]) click to toggle source

returns the coin mapping info (i.e. symbol, slug, name etc.)

# File lib/coinmarketcap_lite.rb, line 129
def get_map(symbols=[])
  return get_request('/map') if symbols.empty?
  api_call('/map?symbol=' + symbols.map {|x| x.to_s.upcase}).join(',')

end