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

#history(symbol, period1, period2, interval = "1d") ⇒ Object



42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
# File 'lib/basic_yahoo_finance.rb', line 42

def history(symbol, period1, period2, interval = "1d")
  hash_result = {}

  with_http do |http|
    make_symbols_array(symbol).each do |sym|
      uri = URI("#{API_URL}/v8/finance/chart/#{sym}?period1=#{period1}&period2=#{period2}&interval=#{interval}&crumb=#{@crumb}")
      response = http.request(uri)
      hash_result.store(sym, process_history_output(JSON.parse(response.body)))
    rescue Net::HTTPBadResponse, Net::HTTPNotFound, Net::HTTPError, Net::HTTPServerError, JSON::ParserError
      hash_result.store(sym, "HTTP Error")
    end
  end

  hash_result
end

#quotes(symbol) ⇒ Object



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

def quotes(symbol)
  hash_result = {}

  with_http do |http|
    make_symbols_array(symbol).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
  end

  hash_result
end