Class: YahooStock::Quote

Inherits:
Base
  • Object
show all
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

Methods inherited from Base

#find, #results

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(options)
  if options.nil? || !options.is_a?(Hash)
    raise QuoteException, "You must provide a hash of stock symbols to fetch data"
  end
  if options[:stock_symbols].nil? || options[:stock_symbols].empty?
    raise QuoteException, "You must provide atleast one stock symbol to fetch data"
  end
  if !(options[:read_parameters] && options[:read_parameters].any?)
    options[:read_parameters] = [:last_trade_price_only, :last_trade_date]
  end
  options[:stock_symbols] = Array.new << options[:stock_symbols] unless options[:stock_symbols].is_a?(Array)
  @interface = YahooStock::Interface::Quote.new(options)
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.


117
118
119
# File 'lib/yahoo_stock/quote.rb', line 117

def add_parameters(*parameters)
  @interface.add_parameters(*parameters)
end

#add_symbols(*symbols) ⇒ Object

Adds more stock symbols to the existing instance. One or more stock symbols can be passed as parameter.


95
96
97
# File 'lib/yahoo_stock/quote.rb', line 95

def add_symbols(*symbols)
  @interface.add_symbols(*symbols)
end

#clear_parametersObject

Clear all existing parameters from the current instance.


141
142
143
144
# File 'lib/yahoo_stock/quote.rb', line 141

def clear_parameters
  @interface.clear_parameters
  current_parameters
end

#clear_symbolsObject

Clear all existing stock symbols from the current instance.


106
107
108
# File 'lib/yahoo_stock/quote.rb', line 106

def clear_symbols
  @interface.clear_symbols
end

#current_parametersObject Also known as: data_attributes

Shows all parameters in the current instance that will be used to get results.


128
129
130
# File 'lib/yahoo_stock/quote.rb', line 128

def current_parameters
  @interface.yahoo_url_parameters
end

#current_symbolsObject

Show all stock symbols in the current instance that will be used to get results.


111
112
113
# File 'lib/yahoo_stock/quote.rb', line 111

def current_symbols
  @interface.stock_symbols
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.


123
124
125
# File 'lib/yahoo_stock/quote.rb', line 123

def remove_parameters(*parameters)
  @interface.remove_parameters(*parameters)
end

#remove_symbols(*symbols) ⇒ Object

Removes stock symbols from the existing instance. One of more stock symbols can be passed to remove.


101
102
103
# File 'lib/yahoo_stock/quote.rb', line 101

def remove_symbols(*symbols)
  @interface.remove_symbols(*symbols)
end

#use_all_parametersObject

Set current instance to use all parameters to fetch values for current symbols.


135
136
137
138
# File 'lib/yahoo_stock/quote.rb', line 135

def use_all_parameters
  params = valid_parameters.each {|parameter| add_parameters(parameter)}
  sort_symbols(params)
end

#valid_parametersObject

Returns an array of all allowed parameters that can be used.


147
148
149
# File 'lib/yahoo_stock/quote.rb', line 147

def valid_parameters
  sort_symbols(@interface.allowed_parameters)
end