Class: YahooStock::Quote
- Inherits:
-
Object
- Object
- YahooStock::Quote
- Defined in:
- lib/yahoo_stock/quote.rb
Overview
DESCRIPTION:
Provides the stock related current data.
Uses YahooStock::Interface to connect to yahoo and get relevant data.
USAGE:
-
Initialize quote object
quote = YahooStock::Quote.new(:stock_symbols => ['YHOO', 'GOOG'], :read_parameters => [:last_trade_price_only, :last_trade_date])
If read_parameters are not provided then by default the above two parameters are used.
-
To get data for all stocks
quote.get
-
To get data with real time values
quote.realtime
-
To get data with standard values
quote.standard
-
To get data with extra parameter values
quote.extended
-
To view the valid parameters that can be passed
quote.valid_parameters
-
To view the current parameters used
quote.current_parameters
-
To view the current stock symbols used
quote.current_symbols
-
To add more stocks to the list
quote.add_symbols('MSFT', 'AAPL')
-
To remove stocks from list
quote.remove_symbols('MSFT', 'AAPL')
Defined Under Namespace
Classes: QuoteException
Instance Method Summary collapse
-
#add_parameters(*parameters) ⇒ Object
Adds more parameters for the stock symbols to the existing instance for getting data.
-
#add_symbols(*symbols) ⇒ Object
Adds more stock symbols to the existing instance.
-
#clear_parameters ⇒ Object
Clear all existing parameters from the current instance.
-
#clear_symbols ⇒ Object
Clear all existing stock symbols from the current instance.
-
#current_parameters ⇒ Object
Shows all parameters in the current instance that will be used to get results.
-
#current_symbols ⇒ Object
Show all stock symbols in the current instance that will be used to get results.
-
#get ⇒ Object
Returns results for the stock symbols as a hash.
-
#initialize(options) ⇒ Quote
constructor
The options parameter expects a hash with two key value pairs.
-
#remove_parameters(*parameters) ⇒ Object
Removes parameters for the stock symbols to get values for from the existing instance.
-
#remove_symbols(*symbols) ⇒ Object
Removes stock symbols from the existing instance.
-
#use_all_parameters ⇒ Object
Set current instance to use all parameters to fetch values for current symbols.
-
#valid_parameters ⇒ Object
Returns an array of all allowed parameters that can be used.
Constructor Details
#initialize(options) ⇒ Quote
The options parameter expects a hash with two key value pairs
:stock_symbols => ‘Array of stock symbols’ or a single symbol
e.g. :stock_symbols => [‘MSFT’,‘YHOO’] or :stock_symbols => ‘YHOO’
another hash :read_parameters => ‘array of values’
e.g. :read_parameters => [:last_trade_price_only, :last_trade_date]
65 66 67 68 69 70 71 72 73 74 75 76 77 |
# File 'lib/yahoo_stock/quote.rb', line 65 def initialize() if .nil? || !.is_a?(Hash) raise QuoteException, "You must provide a hash of stock symbols to fetch data" end if [:stock_symbols].nil? || [:stock_symbols].empty? raise QuoteException, "You must provide atleast one stock symbol to fetch data" end if !([:read_parameters] && [:read_parameters].any?) [:read_parameters] = [:last_trade_price_only, :last_trade_date] end [:stock_symbols] = Array.new << [:stock_symbols] unless [:stock_symbols].is_a?(Array) @interface = YahooStock::Interface.new() end |
Instance Method Details
#add_parameters(*parameters) ⇒ Object
Adds more parameters for the stock symbols to the existing instance for getting data. One or more parameters can be passed as argument.
125 126 127 |
# File 'lib/yahoo_stock/quote.rb', line 125 def add_parameters(*parameters) parameters.each { |parameter| @interface.add_parameters(parameter) } end |
#add_symbols(*symbols) ⇒ Object
Adds more stock symbols to the existing instance. One or more stock symbols can be passed as parameter.
103 104 105 |
# File 'lib/yahoo_stock/quote.rb', line 103 def add_symbols(*symbols) symbols.each { |symbol| @interface.add_symbols(symbol) } end |
#clear_parameters ⇒ Object
Clear all existing parameters from the current instance.
147 148 149 |
# File 'lib/yahoo_stock/quote.rb', line 147 def clear_parameters @interface.yahoo_url_parameters.clear end |
#clear_symbols ⇒ Object
Clear all existing stock symbols from the current instance.
114 115 116 |
# File 'lib/yahoo_stock/quote.rb', line 114 def clear_symbols @interface.stock_symbols.clear end |
#current_parameters ⇒ Object
Shows all parameters in the current instance that will be used to get results.
136 137 138 |
# File 'lib/yahoo_stock/quote.rb', line 136 def current_parameters sort_symbols(@interface.yahoo_url_parameters) end |
#current_symbols ⇒ Object
Show all stock symbols in the current instance that will be used to get results.
119 120 121 |
# File 'lib/yahoo_stock/quote.rb', line 119 def current_symbols @interface.stock_symbols end |
#get ⇒ Object
Returns results for the stock symbols as a hash. The hash keys are the stock symbols and the values are a hash of the keys and values asked for that stock.
82 83 84 |
# File 'lib/yahoo_stock/quote.rb', line 82 def get @interface.results end |
#remove_parameters(*parameters) ⇒ Object
Removes parameters for the stock symbols to get values for from the existing instance. One of more parameters can be passed to remove.
131 132 133 |
# File 'lib/yahoo_stock/quote.rb', line 131 def remove_parameters(*parameters) parameters.each { |parameter| @interface.remove_parameters(parameter) } end |
#remove_symbols(*symbols) ⇒ Object
Removes stock symbols from the existing instance. One of more stock symbols can be passed to remove.
109 110 111 |
# File 'lib/yahoo_stock/quote.rb', line 109 def remove_symbols(*symbols) symbols.each { |symbol| @interface.remove_symbols(symbol) } end |
#use_all_parameters ⇒ Object
Set current instance to use all parameters to fetch values for current symbols.
141 142 143 144 |
# File 'lib/yahoo_stock/quote.rb', line 141 def use_all_parameters params = valid_parameters.each {|parameter| add_parameters(parameter)} sort_symbols(params) end |
#valid_parameters ⇒ Object
Returns an array of all allowed parameters that can be used.
152 153 154 |
# File 'lib/yahoo_stock/quote.rb', line 152 def valid_parameters sort_symbols(@interface.allowed_parameters) end |