Created
January 10, 2013 17:45
-
-
Save romansklenar/4504147 to your computer and use it in GitHub Desktop.
Apple stocks import
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 'open-uri' | |
require 'roo' | |
apple = Company.create! do |company| | |
company.name = "Apple Inc." | |
company.code = "AAPL" | |
company.messages_url = "http://finance.yahoo.com/rss/headline?s=#{company.code}" | |
company.stocks_url = "http://ichart.finance.yahoo.com/table.csv?s=#{company.code}" | |
end | |
spreadsheet = Csv.new("#{Rails.root}/db/seeds/stocks.csv", nil, :ignore) | |
header = spreadsheet.row(1).map { |s| s.parameterize.underscore } # [:date, :open, :high, :low, :close] | |
Stock.transaction do | |
spreadsheet.last_row.downto(2) do |i| | |
row = Hash[[header, spreadsheet.row(i).first(header.size)].transpose] | |
attributes = row.to_hash.slice(*Stock.attribute_names) | |
Stock.create!(attributes) { |stock| stock.company = apple } | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment