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 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]
53 54 55 56 57 58 59 60 61 62 63 64 65 |
# File 'lib/yahoo_stock/quote.rb', line 53 def initialize() if .nil? || ![:stock_symbols] 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] = [:stock_symbols].to_a unless [:stock_symbols].kind_of?(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.
98 99 100 |
# File 'lib/yahoo_stock/quote.rb', line 98 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.
76 77 78 |
# File 'lib/yahoo_stock/quote.rb', line 76 def add_symbols(*symbols) symbols.each { |symbol| @interface.add_symbols(symbol) } end |
#clear_parameters ⇒ Object
Clear all existing parameters from the current instance.
120 121 122 |
# File 'lib/yahoo_stock/quote.rb', line 120 def clear_parameters @interface.yahoo_url_parameters.clear end |
#clear_symbols ⇒ Object
Clear all existing stock symbols from the current instance.
87 88 89 |
# File 'lib/yahoo_stock/quote.rb', line 87 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.
109 110 111 |
# File 'lib/yahoo_stock/quote.rb', line 109 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.
92 93 94 |
# File 'lib/yahoo_stock/quote.rb', line 92 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.
70 71 72 |
# File 'lib/yahoo_stock/quote.rb', line 70 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.
104 105 106 |
# File 'lib/yahoo_stock/quote.rb', line 104 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.
82 83 84 |
# File 'lib/yahoo_stock/quote.rb', line 82 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.
114 115 116 117 |
# File 'lib/yahoo_stock/quote.rb', line 114 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.
125 126 127 |
# File 'lib/yahoo_stock/quote.rb', line 125 def valid_parameters sort_symbols(@interface.allowed_parameters) end |