Module: Coinmarketcap

Defined in:
lib/coinmarketcap.rb,
lib/coinmarketcap/version.rb

Constant Summary collapse

VERSION =
"0.2.3"

Class Method Summary collapse

Class Method Details

.coin(id) ⇒ Object



16
17
18
# File 'lib/coinmarketcap.rb', line 16

def self.coin(id)
  HTTParty.get("https://api.coinmarketcap.com/v1/ticker/#{id}/")
end

.coins(limit = nil) ⇒ Object



8
9
10
11
12
13
14
# File 'lib/coinmarketcap.rb', line 8

def self.coins(limit = nil)
  if limit.nil?
    HTTParty.get('https://api.coinmarketcap.com/v1/ticker/')
  else
    HTTParty.get("https://api.coinmarketcap.com/v1/ticker/?limit=#{limit}")
  end
end

.get_historical_price(id, start_date, end_date) ⇒ Object

20170908



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
# File 'lib/coinmarketcap.rb', line 24

def self.get_historical_price(id, start_date, end_date) #20170908
  prices = []
  doc = Nokogiri::HTML(open("https://coinmarketcap.com/currencies/#{id}/historical-data/?start=#{start_date}&end=#{end_date}"))
  rows = doc.css('tr')
  if rows.count == 31 || rows.count == 2
    doc = Nokogiri::HTML(open("https://coinmarketcap.com/assets/#{id}/historical-data/?start=#{start_date}&end=#{end_date}"))
    rows = doc.css('tr')
  end
  rows.shift
  rows.each do |row|
    begin
      price_bundle = {}
      each_row = Nokogiri::HTML(row.to_s).css('td')
      price_bundle[:date] = Date.parse(each_row[0].text)
      price_bundle[:open] = each_row[1].text.to_f
      price_bundle[:high] = each_row[2].text.to_f
      price_bundle[:low] = each_row[3].text.to_f
      price_bundle[:close] = each_row[4].text.to_f
      price_bundle[:avg] = ( price_bundle[:high] + price_bundle[:low] ) / 2.0
      prices << price_bundle
    rescue => error
      next
    end
  end
  prices
end

.global(currency = 'USD') ⇒ Object



20
21
22
# File 'lib/coinmarketcap.rb', line 20

def self.global(currency = 'USD')
  HTTParty.get("https://api.coinmarketcap.com/v1/global/?convert=#{currency}")
end