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
|