Created
June 23, 2011 15:36
-
-
Save dchapman1988/1042780 to your computer and use it in GitHub Desktop.
Problem updating all stocks...
This file contains hidden or 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
# As the method, I have tried :put and :update. | |
= button_to 'Update all quotes', admin_panel_path, :method => :put |
This file contains hidden or 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
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 |
This file contains hidden or 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
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