Class: YQuotes::Yahoo

Inherits:
Object
  • Object
show all
Defined in:
lib/yquotes/yahoo.rb

Instance Method Summary collapse

Constructor Details

#initializeYahoo

Get cookie and crumb



13
14
15
# File 'lib/yquotes/yahoo.rb', line 13

def initialize
  fetch_credentials
end

Instance Method Details

#fetch_csv(ticker, start_date = nil, end_date = nil, period = 'd') ⇒ Object Also known as: get_csv, get_data

fetch_csv: fetch historical quotes in csv format



18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
# File 'lib/yquotes/yahoo.rb', line 18

def fetch_csv(ticker, start_date = nil, end_date = nil, period = 'd')
  connection = nil

  # retry 3-times in case it sends unauthorized
  3.times do |_i|
    begin
      url = build_url(ticker, start_date, end_date, period)
      connection = open(url, 'Cookie' => @cookie)
      break
    rescue OpenURI::HTTPError => e
      fetch_credentials
    end
  end

  data = CSV.parse(connection.read, converters: :numeric)

  raise 'Yahoo.fetch_csv unable to fetch data' unless data.is_a? Array
  data
end