Module: Wowecon

Defined in:
lib/wowecon.rb,
lib/wowecon/currency.rb,
lib/wowecon/currency_helpers.rb

Defined Under Namespace

Modules: CurrencyHelpers Classes: Currency

Constant Summary collapse

VERSION =
'0.2.5'

Class Method Summary collapse

Class Method Details

.price(item_name, opts = {}) ⇒ Object



9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
# File 'lib/wowecon.rb', line 9

def self.price(item_name, opts={})
  request_url = "http://data.wowecon.com/?type=price&item_name=#{uri_escape(item_name)}"
  opts.each_pair {|name, value| request_url += "&#{name.to_s}=#{uri_escape(value)}" }
  response = Nokogiri::HTML(open(request_url))
  
  if response.css('table.wowecon_wowcurrency tr td').length > 0
    currency_types = {:g => :gold, :s => :silver, :c => :copper}
    currency_type  = nil
    currency       = {:gold => 0, :silver => 0, :copper => 0}
    
    cells = response.css('table.wowecon_wowcurrency tr td')
    cells.reverse_each do |cell|
      if img = cell.at_css('img')
        coin = img.attribute('alt').content.match(/([gsc])/)
        currency_type = currency_types[coin[0].to_sym]
        next
      end
      
      value = cell.inner_text.strip.match(/([0-9]+)/)
      if value
        currency[currency_type] = value[0].to_i
      end
    end

    {:value => "#{currency[:gold]}#{sprintf("%02d", currency[:silver])}#{sprintf("%02d", currency[:copper])}".to_i}
  else
    {:error => "price not found"}
  end
end