Class: YahooStock::Interface::Quote

Inherits:
YahooStock::Interface show all
Defined in:
lib/yahoo_stock/interface/quote.rb

Overview

DESCRIPTION:

Class to generate the right url and interface with yahoo to get daily stock data

Defined Under Namespace

Classes: QuoteError

Constant Summary collapse

STD_PARAMETERS =
{
  :symbol => 's',
  :name => 'n',
  :last_trade_price_only => 'l1',
  :last_trade_date => 'd1',
  :last_trade_time => 't1',
  :change_with_percent_change => 'c',
  :change => 'c1',
  :previous_close => 'p',
  :change_in_percent => 'p2',
  :open => 'o',
  :day_low => 'g',
  :day_high => 'h',
  :volume => 'v',
  :last_trade_with_time => 'l',
  :day_range => 'm',
  :ticker_trend => 't7',
  :ask => 'a',
  :average_daily_volume => 'a2',
  :bid => 'b',
  :bid_size => 'b6'
}
EXTENDED_PARAMETERS =
{
  :symbol => 's',
  :name => 'n',
  :fifty_two_week_range => 'w',
  :change_from_52_week_low => 'j5',
  :percent_change_from_52_week_low => 'j6',
  :change_from_52_week_high => 'k4',
  :percent_change_from_52_week_high => 'k5',
  :earnings_per_share => 'e',
  :short_ratio => 's7',
  :p_e_ratio => 'r',
  :dividend_pay_date => 'r1',
  :ex_dividend_date => 'q',
  :dividend_per_share => 'd',
  :dividend_yield => 'y',  
  :one_yr_target_price => 't8',
  :market_capitalization => 'j1',  
  :eps_estimate_current_year => 'e7',
  :eps_estimate_next_year => 'e8',
  :eps_estimate_next_quarter => 'e9',
  :peg_ratio => 'r5',
  :price_eps_estimate_current_year => 'r6',
  :price_eps_estimate_next_year => 'r7',
  :book_value => 'b4',
  :ebitda => 'j4',
  :fifty_day_moving_average => 'm3',
  :two_hundred_day_moving_average => 'm4',
  :change_from_200_day_moving_average => 'm5',
  :percent_change_from_200_day_moving_average => 'm6',
  :change_from_50_day_moving_average => 'm7',
  :percent_change_from_50_day_moving_average => 'm8',
  :shares_owned => 's1',
  :price_paid => 'p1',
  :commission => 'c3',
  :holdings_value => 'v1',
  :day_value_change => 'w1',
  :trade_date => 'd2',
  :holdings_gain_percent => 'g1',
  :annualized_gain => 'g3',
  :holdings_gain => 'g4',
  :stock_exchange => 'x',
  :high_limit => 'l2',
  :low_limit => 'l3',
  :notes => 'n4',
  :fifty_two_week_low => 'j',
  :fifty_two_week_high => 'k',
  :more_info => 'i',
}
REALTIME_PARAMETERS =
{
  :symbol => 's',
  :name => 'n',
  :ask_real_time => 'b2',
  :bid_real_time => 'b3',
  :change => 'c1',
  :change_real_time => 'c6',
  :after_hours_change_real_time => 'c8',
  :holdings_gain_percent_real_time => 'g5',
  :holdings_gain_real_time => 'g6',
  :order_book_real_time => 'i5',
  :market_cap_real_time => 'j3',
  :last_trade_real_time_with_time => 'k1',
  :change_percent_real_time => 'k2',
  :day_range_real_time => 'm2',
  :p_e_ratio_real_time => 'r2',
  :holdings_value_real_time => 'v7',
  :day_value_change_real_time => 'w4',
}

Constants inherited from YahooStock::Interface

BASE_URLS

Instance Attribute Summary collapse

Attributes inherited from YahooStock::Interface

#base_url, #uri_parameters

Instance Method Summary collapse

Methods inherited from YahooStock::Interface

#update, #values

Constructor Details

#initialize(stock_params_hash) ⇒ Quote

The stock_params_hash parameter expects a hash with two key value pairs

:stock_symbols => 'Array of stock symbols'

e.g. :stock_symbols => ['YHOO']

another hash :read_parameters => ['array of values']

e.g. :read_parameters => [:last_trade_price_only, :last_trade_date]


121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
# File 'lib/yahoo_stock/interface/quote.rb', line 121

def initialize(stock_params_hash)
  unless stock_params_hash
    raise QuoteError, 'You must pass a hash of stock symbols and the data you would like to see' 
  end
  if !stock_params_hash[:stock_symbols]  || stock_params_hash[:stock_symbols].length.zero?
    raise(QuoteError, 'No stocks passed')
  end
  if !stock_params_hash[:read_parameters] || stock_params_hash[:read_parameters].length.zero?
    raise QuoteError, 'Read parameters are not provided'
  end
  @stock_symbols        = stock_params_hash[:stock_symbols]
  @yahoo_url_parameters = stock_params_hash[:read_parameters]
  @base_url             = BASE_URLS[:quote]
  add_observer(self)
end

Instance Attribute Details

#stock_symbolsObject

Returns the value of attribute stock_symbols


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

def stock_symbols
  @stock_symbols
end

Instance Method Details

#add_extended_paramsObject

Add extended parameters


248
249
250
# File 'lib/yahoo_stock/interface/quote.rb', line 248

def add_extended_params
  add_grouped_parameters(EXTENDED_PARAMETERS.keys)
end

#add_parameters(*parameters) ⇒ Object

Add parameters to the url.


219
220
221
# File 'lib/yahoo_stock/interface/quote.rb', line 219

def add_parameters(*parameters)
  parameters.each {|parameter| self.yahoo_url_parameters = parameter }
end

#add_realtime_paramsObject

Add realtime parameters


253
254
255
# File 'lib/yahoo_stock/interface/quote.rb', line 253

def add_realtime_params
  add_grouped_parameters(REALTIME_PARAMETERS.keys)
end

#add_standard_paramsObject

Add standard parameters


243
244
245
# File 'lib/yahoo_stock/interface/quote.rb', line 243

def add_standard_params
  add_grouped_parameters(STD_PARAMETERS.keys)
end

#add_symbols(*symbols) ⇒ Object

Add stock symbols to the url.


200
201
202
# File 'lib/yahoo_stock/interface/quote.rb', line 200

def add_symbols(*symbols)
  symbols.each {|symbol| self.stock_symbols = symbol }
end

#allowed_parametersObject

Returns an array of parameters that can be passed to yahoo.


238
239
240
# File 'lib/yahoo_stock/interface/quote.rb', line 238

def allowed_parameters
  parameters.keys
end

#clear_parametersObject

Clear all existing parameters from the current instance.


258
259
260
261
# File 'lib/yahoo_stock/interface/quote.rb', line 258

def clear_parameters
  @yahoo_url_parameters.clear
  run_observers
end

#clear_symbolsObject

Clear all existing symbols from the current instance.


264
265
266
267
# File 'lib/yahoo_stock/interface/quote.rb', line 264

def clear_symbols
  stock_symbols.clear
  run_observers
end

#getObject

Read the result using get method in super class


178
179
180
181
# File 'lib/yahoo_stock/interface/quote.rb', line 178

def get
  uri
  super()
end

#remove_parameters(*parameters) ⇒ Object

Remove parameters from the url.


224
225
226
227
228
229
230
231
232
233
234
235
# File 'lib/yahoo_stock/interface/quote.rb', line 224

def remove_parameters(*parameters)
  parameters.each do |parameter|
    unless yahoo_url_parameters.include?(parameter)
      raise QuoteError, "Parameter #{parameter} is not present in current list"
    end
    if yahoo_url_parameters.length == 1
      raise QuoteError, "Parameter #{parameter} is the last parameter. Please add another parameter before removing this."     
    end
    @yahoo_url_parameters.reject!{|parameter_key| parameter_key == parameter}
    run_observers
  end
end

#remove_symbols(*symbols) ⇒ Object

Remove stock symbols from the url.


205
206
207
208
209
210
211
212
213
214
215
216
# File 'lib/yahoo_stock/interface/quote.rb', line 205

def remove_symbols(*symbols)
  symbols.each do |symbol|
    unless stock_symbols.include?(symbol)
      raise QuoteError, "Cannot remove stock symbol #{symbol} as it is currently not present."
    end
    if stock_symbols.length == 1
      raise QuoteError, "Symbol #{symbol} is the last symbol. Please add another symbol before removing this."
    end
    stock_symbols.reject!{|stock_sym| stock_sym == symbol}
    run_observers
  end
end

#resultsObject

TODO MOVE TO THE HASH CLASS 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.


187
188
189
190
191
192
193
194
195
196
197
# File 'lib/yahoo_stock/interface/quote.rb', line 187

def results
  stock = {}
  values.each_with_index do |values, index|
    parsed_values = values.split(',')
    stock[stock_symbols[index]] ||= {}
    parsed_values.each_with_index do |value, i|
      stock[stock_symbols[index]][yahoo_url_parameters[i]] = value
    end
  end
  stock
end

#uriObject

Generate full url to be sent to yahoo


160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
# File 'lib/yahoo_stock/interface/quote.rb', line 160

def uri
  @all_stock_symbols = stock_symbols.join('+')
  invalid_params = yahoo_url_parameters-allowed_parameters
  unless invalid_params.length.zero?
    raise QuoteError, "The parameters '#{invalid_params.join(', ')}' are not valid. Please check using YahooStock::Interface::Quote#allowed_parameters or YahooStock::Quote#valid_parameters" 
  end
  @parameter_values  = yahoo_url_parameters.collect {|v| parameters[v]}.join('')
  if @all_stock_symbols.empty?
    raise QuoteError, "You must add atleast one stock symbol to get stock data" 
  end
  if @parameter_values.empty?
    raise QuoteError, "You must add atleast one parameter to get stock data" 
  end
  @uri_parameters = {:s => @all_stock_symbols, :f => @parameter_values}
  super()
end

#yahoo_url_parametersObject


144
145
146
147
# File 'lib/yahoo_stock/interface/quote.rb', line 144

def yahoo_url_parameters
  return [] if !@yahoo_url_parameters || @yahoo_url_parameters.empty?
  @yahoo_url_parameters.map(&:id2name).sort.map(&:to_sym)
end

#yahoo_url_parameters=(yahoo_url_parameter) ⇒ Object


149
150
151
152
153
154
155
156
157
# File 'lib/yahoo_stock/interface/quote.rb', line 149

def yahoo_url_parameters=(yahoo_url_parameter)
  params_on_read = @yahoo_url_parameters.dup
  unless allowed_parameters.include?(yahoo_url_parameter)
    raise QuoteError, "Interface parameter #{yahoo_url_parameter} is not a valid parameter."
  end
  @yahoo_url_parameters ||= []
  @yahoo_url_parameters << yahoo_url_parameter unless @yahoo_url_parameters.include?(yahoo_url_parameter)
  run_observers if params_on_read != @yahoo_url_parameters
end