Skip to content

Instantly share code, notes, and snippets.

@romansklenar
Created January 10, 2013 17:45
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save romansklenar/4504147 to your computer and use it in GitHub Desktop.
Save romansklenar/4504147 to your computer and use it in GitHub Desktop.
Apple stocks import
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