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://query2.finance.yahoo.com"

Instance Method Summary collapse

Constructor Details

#initialize(cache_url = nil) ⇒ Query

Returns a new instance of Query.



16
17
18
# File 'lib/basic_yahoo_finance.rb', line 16

def initialize(cache_url = nil)
  @cache_url = cache_url
end

Instance Method Details

#quotes(symbol, mod = "price") ⇒ Object

rubocop:disable Metrics/MethodLength



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

def quotes(symbol, mod = "price") # rubocop:disable Metrics/MethodLength
  hash_result = {}
  symbols = make_symbols_array(symbol)
  http = Net::HTTP::Persistent.new
  http.override_headers["User-Agent"] = "BYF/#{BasicYahooFinance::VERSION}"
  symbols.each do |sym|
    uri = URI("#{API_URL}/v6/finance/quoteSummary/#{sym}?modules=#{mod}")
    response = http.request(uri)
    hash_result.store(sym, process_output(JSON.parse(response.body), mod))
  rescue Net::HTTPBadResponse, Net::HTTPNotFound, Net::HTTPError, Net::HTTPServerError, JSON::ParserError
    hash_result.store(sym, "HTTP Error")
  end

  http.shutdown

  hash_result
end