Class: YahooFinance::PastQuote

Inherits:
Object
  • Object
show all
Defined in:
lib/stocktracker/yahoofinance.rb

Constant Summary collapse

PAST_FORMAT =
[
  :date,
  [:open,"to_f"],
  [:high,"to_f"],
  [:low,"to_f"],
  [:close,"to_f"],
  [:volume,"to_f"],
  [:adj_close,"to_f"]
]

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(symbol, date) ⇒ PastQuote

Returns a new instance of PastQuote.



100
101
102
103
104
105
# File 'lib/stocktracker/yahoofinance.rb', line 100

def initialize(symbol, date)
  self.symbol= symbol
  self.start_date= date
  self.end_date= date
  self.results= get_past_quote
end

Instance Attribute Details

#end_dateObject

Returns the value of attribute end_date.



88
89
90
# File 'lib/stocktracker/yahoofinance.rb', line 88

def end_date
  @end_date
end

#resultsObject

Returns the value of attribute results.



88
89
90
# File 'lib/stocktracker/yahoofinance.rb', line 88

def results
  @results
end

#start_dateObject

Returns the value of attribute start_date.



88
89
90
# File 'lib/stocktracker/yahoofinance.rb', line 88

def start_date
  @start_date
end

#symbolObject

Returns the value of attribute symbol.



88
89
90
# File 'lib/stocktracker/yahoofinance.rb', line 88

def symbol
  @symbol
end

Instance Method Details

#get_past_quoteObject



107
108
109
110
111
# File 'lib/stocktracker/yahoofinance.rb', line 107

def get_past_quote
  past_map
rescue
  nil
end

#past_mapObject



135
136
137
138
139
140
141
142
143
144
145
146
# File 'lib/stocktracker/yahoofinance.rb', line 135

def past_map
  map = {}
  values = past_values
  PAST_FORMAT.each_with_index do |v,i|
    if v.is_a?(Array)
      map[v[0]] = values[i].send(v[1])
    else
      map[v] = values[i]
    end
  end
  map
end

#past_valuesObject



131
132
133
# File 'lib/stocktracker/yahoofinance.rb', line 131

def past_values
  CSV.parse(yahoo_past_quote)[1]
end

#yahoo_past_quoteObject



113
114
115
# File 'lib/stocktracker/yahoofinance.rb', line 113

def yahoo_past_quote
  open(yahoo_past_url).read
end

#yahoo_past_urlObject



117
118
119
120
121
122
123
124
125
126
127
128
# File 'lib/stocktracker/yahoofinance.rb', line 117

def yahoo_past_url
  URI.encode(
    "http://itable.finance.yahoo.com/table.csv?"+
    "s=#{symbol}&g=d&"+
    "a=#{start_date.month-1}&"+
    "b=#{start_date.mday}&"+
    "c=#{start_date.year}&"+
    "d=#{end_date.month-1}&"+
    "e=#{end_date.mday}&"+
    "f=#{end_date.year}"
  )
end