Class: Coin360Api21::Coin

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

Instance Method Summary collapse

Constructor Details

#initializeCoin

Returns a new instance of Coin.



15
16
17
# File 'lib/coin360api21.rb', line 15

def initialize()
  @url_base = 'https://api.coin360.com'
end

Instance Method Details

#historical(coin, date_start: nil, date_end: nil, period: nil) ⇒ Object

returns the historical price for a given coin

if no date is provided, returns the previous 17 days
if no period is provided, defaults to a 5 minute period

Examples

historical :btc
historical :btc, date_start: 1620120300
historical :btc, date_start: 1620120300, period: '1h'
historical :btc, date_start: 1620120300, date_end: 1620121200


29
30
31
32
33
34
35
36
37
38
# File 'lib/coin360api21.rb', line 29

def historical(coin, date_start: nil, date_end: nil, period: nil)

  params = ['coin=' + coin.to_s]
  params << 'start=' + timeify(date_start) if date_start
  params << 'end=' + timeify(date_end) if date_end
  params << 'period=' + period if period
  r = api_call "/coin/historical?%s" % [params.join('&')]
  r.map {|x| OpenStruct.new x}

end

#info(coin = nil) ⇒ Object



40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
# File 'lib/coin360api21.rb', line 40

def info(coin=nil)
  
  if not @info then
    r = api_call "/info/currency"
    @info = r.map {|x| OpenStruct.new x}
  end
  
  return @info unless coin
  
  r = @info.find {|x| x.symbol.downcase == coin || x.name.downcase == coin}
  
  return r if r
  
  dym = DidYouMean::SpellChecker.new(dictionary: @info.flat_map \
                        {|x| [x.symbol, x.name]})
  found = dym.correct(coin)
  return unless found
  
  raise "Did you mean? '%s'" % found.first
  
end

#latest(coin = :btc, convert: :usd) ⇒ Object



62
63
64
65
66
67
68
# File 'lib/coin360api21.rb', line 62

def latest(coin=:btc, convert: :usd)

  params = ['coin=' + coin.to_s]
  params << 'convert=' + convert.to_s
  api_call "/coin/latest?%s" % [params.join('&')]

end

#price(coin = :btc) ⇒ Object



70
71
72
73
# File 'lib/coin360api21.rb', line 70

def price(coin=:btc)
  r = latest(coin)
  r.first.last['quotes']['USD']['price']
end