Class: YahooFinanceClient::Stock

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

Overview

This class provides methods to interact with Yahoo Finance API for stock data.

Constant Summary collapse

QUOTE_PATH =
"/v7/finance/quote"
CHART_PATH =
"/v8/finance/chart"
SEARCH_PATH =
"/v1/finance/search"
SEARCH_BASE_URL =
"https://query1.finance.yahoo.com"
CACHE_TTL =
300
MAX_RETRIES =
2
BATCH_SIZE =
50
AUTH_ERROR_PATTERNS =
[/invalid cookie/i, /invalid crumb/i, /unauthorized/i].freeze

Class Method Summary collapse

Class Method Details

.get_dividend_history(symbol, range: "2y") ⇒ Object



35
36
37
38
# File 'lib/yahoo_finance_client/stock.rb', line 35

def get_dividend_history(symbol, range: "2y")
  cache_key = "div_history_#{symbol}_#{range}"
  fetch_from_cache(cache_key) || fetch_and_cache_dividend_history(cache_key, symbol, range)
end

.get_fx_rate(from, to) ⇒ Float?

Fetch the current FX rate between two ISO 4217 currency codes via Yahoo’s ‘<FROM><TO>=X` quote symbol. Returns 1.0 for identity pairs without hitting the API.

Parameters:

  • from (String)

    source currency code (e.g. “EUR”)

  • to (String)

    target currency code (e.g. “USD”)

Returns:

  • (Float, nil)

    one unit of ‘from` expressed in `to`, or nil on error



46
47
48
49
50
51
# File 'lib/yahoo_finance_client/stock.rb', line 46

def get_fx_rate(from, to)
  return 1.0 if from == to

  cache_key = "fx_#{from}_#{to}"
  fetch_from_cache(cache_key) || fetch_and_cache_fx_rate(cache_key, from, to)
end

.get_quote(symbol) ⇒ Object



22
23
24
25
# File 'lib/yahoo_finance_client/stock.rb', line 22

def get_quote(symbol)
  cache_key = "quote_#{symbol}"
  fetch_from_cache(cache_key) || fetch_and_cache(cache_key, symbol)
end

.get_quotes(symbols) ⇒ Object



27
28
29
30
31
32
33
# File 'lib/yahoo_finance_client/stock.rb', line 27

def get_quotes(symbols)
  return {} if symbols.nil? || symbols.empty?

  results, uncached = partition_cached(symbols)
  fetch_uncached_quotes(uncached, results)
  results
end

.search(query, count: 10) ⇒ Array<Hash>

Search the Yahoo Finance autocomplete index for matching symbols.

Parameters:

  • query (String)

    free-text query (ticker or company name)

  • count (Integer) (defaults to: 10)

    max number of results to return

Returns:

  • (Array<Hash>)

    each entry has symbol, name, exchange, type, type_display



58
59
60
61
62
63
64
# File 'lib/yahoo_finance_client/stock.rb', line 58

def search(query, count: 10)
  normalized = query.to_s.strip
  return [] if normalized.empty?

  cache_key = "search_#{normalized.downcase}_#{count}"
  fetch_from_cache(cache_key) || fetch_and_cache_search(cache_key, normalized, count)
end