Class: CryptoCoins::Coin

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

Class Method Summary collapse

Class Method Details

.find(ticker) ⇒ Object



12
13
14
15
16
17
18
19
# File 'lib/cryptocoins.rb', line 12

def self.find(ticker)
  response = HTTParty.get('https://api.coinmarketcap.com/v1/ticker/')
  response.each do |coin|
    if coin['symbol'] == ticker.upcase or coin['id'] == ticker.downcase or coin['name'].downcase == ticker.downcase
      return coin
    end
  end
end

.markets(ticker) ⇒ Object



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

def self.markets(ticker)
  markets_json = {
      ticker.downcase => []
  }
  begin
    markets_table = Nokogiri::HTML(open("https://coinmarketcap.com/currencies/#{self.find(ticker)['id']}/"))
    markets_table = markets_table.xpath("//table[@id = 'markets-table']/tbody")
    markets_table.search('tr').each do |row|
      tds = row.search('td')
      item_json = {
          'rank' => tds[0].text,
          'name' => tds[1].text,
          'pair' => tds[2].text,
          'link' => tds[2].xpath('./a/@href').first.value,
          '24h_volume_usd' => tds[3].xpath('./span/@data-usd').first.value,
          '24h_volume_btc' => tds[3].xpath('./span/@data-btc').first.value,
          '24h_volume_native' => tds[3].xpath('./span/@data-native').first.value,
          'price_usd' => tds[4].xpath('./span/@data-usd').first.value,
          'price_btc' => tds[4].xpath('./span/@data-btc').first.value,
          'price_native' => tds[4].xpath('./span/@data-native').first.value,
          'percent_volume' => tds[5].xpath('./span/@data-format-value').first.value
      }
      markets_json[ticker.downcase] << item_json
    end
    return markets_json
  rescue
    error = {
        'error' => "Invaild HTTP Request! **#{ticker.upcase}** Coin Not Supported!"
    }
    markets_json[ticker.downcase] = []
    markets_json[ticker.downcase] << error
    return markets_json
  end
end

.top(limit) ⇒ Object



8
9
10
11
# File 'lib/cryptocoins.rb', line 8

def self.top(limit)
  options = {query: { limit: limit.to_s}}
  return HTTParty.get('https://api.coinmarketcap.com/v1/ticker/?', options)
end