Class: CryptoCoins::Market

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

Class Method Summary collapse

Class Method Details

.coins(market) ⇒ Object



56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
# File 'lib/cryptocoins.rb', line 56

def self.coins(market)
  coins_json = {
      market.downcase => []
  }
  begin
    coins_table = Nokogiri::HTML(open("https://coinmarketcap.com/exchanges/#{market.downcase}/"))
    coins_table = coins_table.xpath("//div[@class = 'table-responsive']/table")
    coins_table.search('tr').each_with_index do |row, i|
      if i > 0
        tds = row.search('td')
        item_json = {
            'rank' => tds[0].text,
            'name' => tds[1].text,
            'icon' => tds[1].xpath('./img/@src').first.value,
            'link' => tds[2].xpath('./a/@href').first.value,
            'pair' => tds[2].text,
            '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].text.gsub('[\s\r\n%]+', '')
        }
      end
      coins_json[market.downcase] << item_json
    end
    return coins_json
  rescue
    error = {
        'error' => "Invaild HTTP Request! **#{market.upcase}** Exchange Not Supported!"
    }
    coins_json[market.downcase] = []
    coins_json[market.downcase] << error
    return coins_json
  end
end