Class: CoinQuery

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(autofind: true, dym: true, debug: false) ⇒ CoinQuery

Returns a new instance of CoinQuery.



21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
# File 'lib/coinquery.rb', line 21

def initialize(autofind: true, dym: true, debug: false)

  @autofind, @dym, @debug = autofind, dym, debug

  @url_base = 'https://api.coingecko.com/api/v3/'
  r = ping()

  if r then

    puts ('CoinQuery').highlight + ' (powered by CoinGecko)'
    puts
    puts ('ping response: ' + r.to_a.first.join(' ')).info

    if autofind then

      file = 'coinquery.dat'

      if not File.exists? file then

        puts ('fetching coins list ...').info  
        @list = api_call 'coins/list'
      

        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


      if not File.exists? file then

        File.open(file, 'w+') do |f|  
         Marshal.dump([@list, @dym], f)  
        end 

      else
      
        puts ('loading coins list ...').info  
        File.open(file) do |f|  
          @list, @dym = Marshal.load(f)  
        end
  
      end

    end
  end

end

Instance Attribute Details

#listObject (readonly)

Returns the value of attribute list.



19
20
21
# File 'lib/coinquery.rb', line 19

def list
  @list
end

Instance Method Details

#coins(limit: 5) ⇒ Object

lists the top coins (limited to 5 by default)



77
78
79
80
# File 'lib/coinquery.rb', line 77

def coins(limit: 5)
  currency = 'usd'
  api_call "coins/markets?vs_currency=#{currency}&per_page=#{limit}"
end

#coins_listObject

lists the names and identifiers of all coins



84
85
86
# File 'lib/coinquery.rb', line 84

def coins_list
  @list
end

#historical_price(coin, rawdate) ⇒ Object Also known as: history

returns the price of a coin for a given historical date e.g. historical_price(‘Bitcoin’, ‘01-05-2021’)



91
92
93
94
95
96
97
98
99
100
101
102
# File 'lib/coinquery.rb', line 91

def historical_price(coin, rawdate)

  uc = Unichron.new(rawdate.to_s, :little_endian)
  raise 'invalid date' unless uc.valid?
  date = uc.to_date.strftime("%d-%m-%Y")

  id = find_id coin
  r = api_call "coins/%s/history?date=%s" % [id, date]
  price = r['market_data']['current_price']['usd']
  price < 1 ? price : price.round(2)

end

#pingObject



106
107
108
# File 'lib/coinquery.rb', line 106

def ping
  api_call 'ping'
end

#price(coin) ⇒ Object

returns the price for a given a coin e.g. price(‘Litecoin’)



113
114
115
116
117
118
119
120
121
122
123
# File 'lib/coinquery.rb', line 113

def price(coin)

  currency = 'usd'
  id = find_id coin
  r = api_call("simple/price?ids=#{id}&vs_currencies=#{currency}")

  if r then
    val = r[id][currency]
    val < 1 ? val : val.round(2)
  end
end