Class: BasicYahooFinance::Query

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

Overview

Class to send queries to Yahoo Finance API

Constant Summary collapse

API_URL =
"https://query1.finance.yahoo.com"
"https://fc.yahoo.com"
CRUMB_URL =
"https://query1.finance.yahoo.com/v1/test/getcrumb"
USER_AGENT =
"Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) " \
"Chrome/90.0.4421.0 Safari/537.36 Edg/90.0.810.1"

Instance Method Summary collapse

Constructor Details

#initialize(cache_url = nil) ⇒ Query

Returns a new instance of Query.



20
21
22
23
24
# File 'lib/basic_yahoo_finance.rb', line 20

def initialize(cache_url = nil)
  @cache_url = cache_url
  @cookie = fetch_cookie
  @crumb = fetch_crumb(@cookie)
end

Instance Method Details

#quotes(symbol) ⇒ Object

rubocop:disable Metrics/MethodLength



26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
# File 'lib/basic_yahoo_finance.rb', line 26

def quotes(symbol) # rubocop:disable Metrics/MethodLength
  hash_result = {}
  symbols = make_symbols_array(symbol)
  http = Net::HTTP::Persistent.new
  http.override_headers["User-Agent"] = USER_AGENT
  http.override_headers["Cookie"] = @cookie
  symbols.each do |sym|
    uri = URI("#{API_URL}/v7/finance/quote?symbols=#{sym}&crumb=#{@crumb}")
    response = http.request(uri)
    hash_result.store(sym, process_output(JSON.parse(response.body)))
  rescue Net::HTTPBadResponse, Net::HTTPNotFound, Net::HTTPError, Net::HTTPServerError, JSON::ParserError
    hash_result.store(sym, "HTTP Error")
  end

  http.shutdown

  hash_result
end