class CryptoCoins::Market

Public Class Methods

coins(market) click to toggle source
# 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