class Edgar::Edgar
Attributes
symbol[R]
Public Class Methods
config()
click to toggle source
# File lib/edgar.rb, line 23 def self.config @@config end
configure(opts = {})
click to toggle source
# File lib/edgar.rb, line 19 def self.configure(opts = {}) opts.each {|k,v| @@config[k.to_sym] = v if @@valid_config_keys.include? k.to_sym} end
new(symbol)
click to toggle source
# File lib/edgar.rb, line 27 def initialize(symbol) @symbol = symbol # Perform the actual lookup of historical data and parse it into arrays that # are cached in memory @data = CSV.parse(lookup) end
Public Instance Methods
closing_price(date = DateTime.now, running = 1)
click to toggle source
# File lib/edgar.rb, line 35 def closing_price(date = DateTime.now, running = 1) lookup_by_column(running, date, 4) end
high_price(date = DateTime.now, running = 1)
click to toggle source
# File lib/edgar.rb, line 43 def high_price(date = DateTime.now, running = 1) lookup_by_column(running, date, 2) end
low_price(date = DateTime.now, running = 1)
click to toggle source
# File lib/edgar.rb, line 47 def low_price(date = DateTime.now, running = 1) lookup_by_column(running, date, 3) end
opening_price(date = DateTime.now, running = 1)
click to toggle source
# File lib/edgar.rb, line 39 def opening_price(date = DateTime.now, running = 1) lookup_by_column(running, date, 1) end
volume(date = DateTime.now, running = 1)
click to toggle source
# File lib/edgar.rb, line 51 def volume(date = DateTime.now, running = 1) lookup_by_column(running, date, 5) end
Private Instance Methods
get_historical_data()
click to toggle source
# File lib/edgar.rb, line 61 def get_historical_data uri = URI("http://ichart.finance.yahoo.com/table.csv?s=#{@symbol}") response = Net::HTTP.get_response(uri) raise InvalidSymbolError.new("Unable to lookup '#{symbol}'") unless response.code == "200" response.body end
lookup()
click to toggle source
# File lib/edgar.rb, line 57 def lookup @content ||= get_historical_data end
lookup_by_column(running, date, column)
click to toggle source
# File lib/edgar.rb, line 69 def lookup_by_column(running, date, column) return nil if date > DateTime.now() running = 1 if running < 1 formatted_date = date.strftime('%Y-%m-%d') @data[1..@data.size].each_index do |index| row = @data[index+1] return sum_and_average(index+1, running, column) if row[0] == formatted_date if DateTime.parse(row[0]) < date if Edgar.config[:lookup_offset] < 0 return sum_and_average(index+1, running, column) else return sum_and_average(index, running, column) end end end return nil end
sum_and_average(start, days, column)
click to toggle source
# File lib/edgar.rb, line 93 def sum_and_average(start, days, column) total = @data[start, days].inject(0) do |sum, row| sum + row[column.to_i].to_f end # Round off to at most two digits round_to = Edgar.config[:round_to] round_to = 2 if round_to < 0 denom = @data[start, days].size (total / denom).round(round_to) end