Skip to content

Instantly share code, notes, and snippets.

@karmajunkie
Created August 12, 2011 15:09
Show Gist options
  • Save karmajunkie/1142247 to your computer and use it in GitHub Desktop.
Save karmajunkie/1142247 to your computer and use it in GitHub Desktop.
require 'fastercsv'
class StockParser::Parser
attr_reader :rows
def initialize
@rows=[]
end
def import(data)
@rows = FasterCSV.parse(data, :headers => true)
end
def companies
@rows.map{|row| row['stock_symbol']}.uniq
end
def start_date
@rows.map{|row| Date.parse row['date']}.min
end
def end_date
@rows.map{|row| Date.parse row['date']}.max
end
# { "KBG" => {"max_open" => 22.0, "min_open" => 23.0}}
def price_data
@price_date ||= @rows.inject({}) do |hash, row|
company = row['stock_symbol']
price = row['stock_price_open'].to_f
hash[company] = {"max_open" => price, "min_open" => price} unless hash.has_key?(company)
hash[company]['max_open'] = price if price > hash[company]['max_open']
hash[company]['min_open'] = price if price < hash[company]['min_open']
hash
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment