Skip to content

Instantly share code, notes, and snippets.

@dchapman1988
Created June 23, 2011 15:36
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 dchapman1988/1042780 to your computer and use it in GitHub Desktop.
Save dchapman1988/1042780 to your computer and use it in GitHub Desktop.
Problem updating all stocks...
# As the method, I have tried :put and :update.
= button_to 'Update all quotes', admin_panel_path, :method => :put
def update
@symbol_list = Array.new
stocks = Stock.all
stocks.each do |stock|
@symbol_list << stock.symbol
end
YahooFinanceIntegrator.get_quotes(@symbol_list)
redirect_to admin_panel_path, :notice => "Stocks successfully updated!"
end
class YahooFinanceIntegrator
attr_accessor :stocks, :quotes
def initialize array
if verify array
add_quote_for_new_stock array
else
verify array
end
end
def verify array
raise "Must have something to integrate!" if array.nil?
raise "Must have pass in an actual array!" unless array.is_a?(Array)
raise "The array must not be empty!" if array.empty?
true
end
def get_quotes array
y_ext_hsh = YahooFinance.get_extended_quotes array
y_std_hsh = YahooFinance.get_standard_quotes array
y_ext_hsh.keys.each do |symbol|
co_name = y_ext_hsh[symbol].name
stock = Stock.find_or_create_by_symbol_and_company_name(symbol, co_name)
quote = Quote.new
stock.update_attributes( :company_name => co_name,
:symbol => y_ext_hsh[symbol].symbol )
quote.update_attributes( :ex_dividend_date => y_ext_hsh[symbol].exDividendDate,
:dividend_pay_date => y_ext_hsh[symbol].dividendPayDate,
:dividend_yield => y_ext_hsh[symbol].dividendYield,
:dividends_per_share => y_ext_hsh[symbol].dividendPerShare,
:last_price => y_std_hsh[symbol].lastTrade,
:stock_id => stock.id )
end
end
def add_quote_for_new_stock(array)
@symbol_list = Array.new
array.each do |symbol|
@symbol_list << symbol
Stock.create( :symbol => @symbol_list.first.upcase, :company_name => "new_company" )
end
get_quotes(@symbol_list)
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment