Module: CoinCapper

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

Constant Summary collapse

API_URL =
'https://api.coinmarketcap.com/v1'.freeze
BASE_URL =
'https://coinmarketcap.com'.freeze
VERSION =
'1.0.0'

Class Method Summary collapse

Class Method Details

.coin(id, currency: nil) ⇒ Hash

Parameters:

  • id (Integer)

    Coinmarketcap coin id

  • currency (String) (defaults to: nil)

    Country currency code to convert price

Returns:

  • (Hash)


35
36
37
38
39
40
41
42
43
44
45
46
# File 'lib/coincapper.rb', line 35

def self.coin(id, currency: nil)
  params = {
    convert: currency
  }.compact.to_param

  url = "#{API_URL}/ticker/#{id}/"
  url << "?#{params}" if params.present?

  response = HTTP.get(url)
  json = JSON.parse(response.body.to_s, symbolize_names: true)
  json.is_a?(Array) ? json.first : json
end

.coin_by_symbol(symbol) ⇒ Hash

Parameters:

  • symbol (String)

    Coin symbol

Returns:

  • (Hash)


50
51
52
53
54
# File 'lib/coincapper.rb', line 50

def self.coin_by_symbol(symbol)
  response = HTTP.get("#{API_URL}/ticker/?limit=0")
  json = JSON.parse(response.body.to_s, symbolize_names: true)
  json.find { |x| x[:symbol].strip.casecmp(symbol.strip.upcase).zero? }
end

.coin_markets(id: nil, symbol: nil) ⇒ Array<Hash>

Parameters:

  • id (String) (defaults to: nil)

    Coin market cap id

  • symbol (String) (defaults to: nil)

    Coin symbol

Returns:

  • (Array<Hash>)

Raises:

  • (ArgumentError)


59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
# File 'lib/coincapper.rb', line 59

def self.coin_markets(id: nil, symbol: nil)
  raise ArgumentError.new('id or symbol is required') if id.blank? && symbol.blank?

  coin_id = symbol.present? ? coin_by_symbol(symbol)[:id] : id
  response = HTTP.get("#{BASE_URL}/currencies/#{coin_id}/\#markets")
  html = Nokogiri::HTML(response.body.to_s)
  rows = html.css('table#markets-table tbody tr')

  markets = rows.each_with_object([]) do |row, arr|
    td = row.css('td')
    arr << {
      source: td[1].text.strip,
      pair: td[2].text.strip,
      volume_usd: td[3].text.strip[/\$(.+)/, 1].delete(',').to_f,
      price_usd: td[4].text.strip[/\$(.+)/, 1].delete(',').to_f,
      volume_percentage: td[5].text.to_f,
      last_updated: td[6].text.strip
    }
  end

  markets
end

.coins(limit: 0, rank: nil, currency: nil) ⇒ Array<Hash>

Parameters:

  • rank (Integer) (defaults to: nil)

    Coins market cap rank greater than or equal

  • limit (Integer) (defaults to: 0)

    Maximum limit set. Defaults to 0 to return all results

  • currency (String) (defaults to: nil)

    Country currency code to convert price

Returns:

  • (Array<Hash>)

See Also:



18
19
20
21
22
23
24
25
26
27
28
29
30
# File 'lib/coincapper.rb', line 18

def self.coins(limit: 0, rank: nil, currency: nil)
  params = {
    limit: limit,
    start: rank,
    convert: currency
  }.compact.to_param

  url = "#{API_URL}/ticker/"
  url << "?#{params}" if params.present?

  response = HTTP.get(url)
  JSON.parse(response.body.to_s, symbolize_names: true)
end

.global(currency: nil) ⇒ Hash

Parameters:

  • currency (String) (defaults to: nil)

    Country currency code to convert price

Returns:

  • (Hash)


84
85
86
87
88
89
90
91
92
93
94
# File 'lib/coincapper.rb', line 84

def self.global(currency: nil)
  params = {
    convert: currency
  }.compact.to_param

  url = "#{API_URL}/global/"
  url << "?#{params}" if params.present?

  response = HTTP.get(url)
  JSON.parse(response.body.to_s, symbolize_names: true)
end

.historical_price(id, start_date, end_date) ⇒ Array<Hash>

Parameters:

  • id (String)

    Coinmarketcap coin id

  • start_date (String)

    Start date (YYYY-MM-DD)

  • end_date (String)

    End date (YYYY-MM-DD)

Returns:

  • (Array<Hash>)


100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
# File 'lib/coincapper.rb', line 100

def self.historical_price(id, start_date, end_date)
  sd = start_date.to_date.to_s.delete('-')
  ed = end_date.to_date.to_s.delete('-')

  url = "#{BASE_URL}/currencies/#{id}/historical-data/?start=#{sd}&end=#{ed}"
  response = HTTP.get(url)
  html = Nokogiri::HTML(response.body.to_s)
  rows = html.css('#historical-data table tbody tr')

  prices = rows.each_with_object([]) do |row, arr|
    td = row.css('td')
    daily = {
      date: Date.parse(td[0].text).to_s,
      open: td[1].text.to_f,
      high: td[2].text.to_f,
      low: td[3].text.to_f,
      close: td[4].text.to_f
    }

    daily[:average] = ((daily[:high] + daily[:low]).to_d / 2).to_f
    arr << daily
  end

  prices
end