Class: YahooStock::Interface::History

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

Overview

DESCRIPTION:

Class to generate the right url and interface with yahoo to get history of a stock

Defined Under Namespace

Classes: HistoryError

Constant Summary

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(options) ⇒ History

The options parameter expects a hash with 4 key value pairs

:stock_symbol => ‘goog’, :start_date => Date.today-30, :end_date => Date.today-10, :type => :weekly

The type option accepts :daily, :monthly, :weekly and :dividend values

The type key is optional, if not used then by default uses :daily

and provides daily history for the specified date range



37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
# File 'lib/yahoo_stock/interface/history.rb', line 37

def initialize(options)
  @base_url = BASE_URLS[:history]
  validate_keys(options)
  
  @stock_symbol = options[:stock_symbol]
  @start_date   = options[:start_date]
  @end_date     = options[:end_date]
  @type     = options[:type].nil? ? :daily : options[:type].to_sym
  
  validate_type_values(options[:type]) if options[:type]
  validate_stock_symbol(options)
  validate_start_date(options)
  validate_end_date(options)
  validate_history_range
end

Instance Attribute Details

#end_dateObject

Returns the value of attribute end_date.



24
25
26
# File 'lib/yahoo_stock/interface/history.rb', line 24

def end_date
  @end_date
end

#start_dateObject

Returns the value of attribute start_date.



24
25
26
# File 'lib/yahoo_stock/interface/history.rb', line 24

def start_date
  @start_date
end

#stock_symbolObject

Returns the value of attribute stock_symbol.



24
25
26
# File 'lib/yahoo_stock/interface/history.rb', line 24

def stock_symbol
  @stock_symbol
end

#typeObject

Returns the value of attribute type.



24
25
26
# File 'lib/yahoo_stock/interface/history.rb', line 24

def type
  @type
end

Instance Method Details

#getObject

Get uri content with the help of get method of the super class



99
100
101
102
103
104
105
106
107
108
# File 'lib/yahoo_stock/interface/history.rb', line 99

def get
  if stock_symbol.nil? || start_date.nil? || end_date.nil?
    raise HistoryError, 'Cannot send request unless all parameters, ie stock_symbol, start and end date are present'
  end
  if !(start_date.is_a?(Date) || end_date.is_a?(Date))
    raise HistoryError, 'Start and end date should be of type Date'
  end
  uri
  super()
end

#uriObject

Generate full uri with the help of uri method of the superclass



84
85
86
87
88
89
90
91
92
93
94
95
96
# File 'lib/yahoo_stock/interface/history.rb', line 84

def uri
  history_type = case type
                   when :daily     then 'd'
                   when :weekly    then 'w'
                   when :monthly   then 'm'
                   when :dividend  then 'v'
                 end
  @uri_parameters = {:a => sprintf("%02d", start_date.month-1), :b => start_date.day, 
                     :c => start_date.year, :d => sprintf("%02d", end_date.month-1),
                     :e => end_date.day, :f => end_date.year, :s => stock_symbol,
                     :g => history_type, :ignore => '.csv'}
  super()  
end