Skip to content

Instantly share code, notes, and snippets.

@dchapman1988
Created June 24, 2011 01:04
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/1044018 to your computer and use it in GitHub Desktop.
Save dchapman1988/1044018 to your computer and use it in GitHub Desktop.
def index
if current_user.admin
@stocks = Stock.all
else
redirect_to root_path
flash[:error] = "You must be an administrator to access this page!"
end
end
class Admin::StocksController < AdminController
def create
end
def destroy
if Stock.find(params[:id]).destroy
redirect_to admin_panel_path, :notice => "Stock successfully removed!"
else
redirect_to admin_panel_path
flash[:error] = "failed to remove stock!"
end
end
def update_multiple
if params[:commit] == "Update selected" && !params[:selected].nil?
number_of_updated_quotes = pluralize(params[:selected].count, "quote")
process_updates
redirect_to admin_panel_path
flash[:notice] = "Updated #{number_of_updated_quotes}."
elsif params[:commit] == "Remove selected" && !params[:selected].nil?
number_of_removed_stocks = pluralize(params[:selected].count, "stock")
remove_multiple
redirect_to admin_panel_path
flash[:alert] = "Removed #{number_of_removed_stocks}."
else
redirect_to admin_panel_path
flash[:info] = "Select something first."
end
end
def edit
end
def new
@symbol_list = params[:symbol].split(",").map{ |s| s.strip }
if @symbol_list.any?
YahooFinanceIntegrator.new @symbol_list
number_of_added_stocks = pluralize(@symbol_list.count, 'Stock')
redirect_to admin_panel_path, :notice => "Added #{number_of_added_stocks}"
else
redirect_to admin_panel_path
flash[:error] = "Didn\'t find any symbols. Was the text area empty? ;-)"
end
end
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
def pluralize(count, singular, plural = nil)
"#{count || 0} " + ((count == 1 || count =~ /^1(\.0+)?$/) ? singular : (plural || singular.pluralize))
end
protected
def remove_multiple
Stock.destroy_all(["stocks.id IN (?)", params[:selected].keys] ) if params[:selected].any?
end
protected :remove_multiple
def process_updates
YahooFinanceIntegrator.new( params[:selected].keys ) if params[:selected].any?
end
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 pass in an actual array list of symbol strings!" unless array.is_a?(Array)
raise "The list 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|
symbol.inspect
co_name = y_ext_hsh[symbol].name
stock = Stock.find_or_create_by_symbol_and_company_name(symbol, co_name)
puts stock.nil?
stock.update_attributes( :company_name => co_name,
:symbol => y_ext_hsh[symbol].symbol )
Quote.create( :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 => stock
)
end
end
def add_quote_for_new_stock(array)
@symbol_list = Array.new
array.each do |symbol|
@symbol_list << symbol
#Stock.create( :symbol => symbol, :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