Created
August 12, 2011 15:09
-
-
Save karmajunkie/1142247 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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