Class: Bovespa

Inherits:
Object
  • Object
show all
Defined in:
lib/bovespa-prices.rb

Defined Under Namespace

Classes: Stock

Constant Summary collapse

VERSION =
"1.0.3"

Instance Method Summary collapse

Instance Method Details

#get(*stock_codes) ⇒ Object



15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
# File 'lib/bovespa-prices.rb', line 15

def get *stock_codes
	result = Hash.new

	xml = Nokogiri.XML(HTTPClient.new.get_content("http://www.bmfbovespa.com.br/Pregao-Online/ExecutaAcaoAjax.asp?intEstado=1&CodigoPapel=#{URI.escape(stock_codes.join('|'))}"))
	xml.search('Papel').each do |p|
		st = Stock.new
		st.name = p['Nome']
		st.date = p['Data']
		st.opening_price = p['Abertura'].gsub(',','.').to_f
		st.min_price = p['Minimo'].gsub(',','.').to_f
		st.max_price = p['Maximo'].gsub(',','.').to_f
		st.average_price = p['Medio'].gsub(',','.').to_f
		st.last_price = p['Ultimo'].gsub(',','.').to_f
		st.variation = p['Oscilacao'].gsub(',','.').to_f
		st.code = p['Codigo']

		result[st.code.to_sym] = st
	end

	return (stock_codes.size > 1 ? result : result[stock_codes.first])
end