yahoo_finance_client 0.5.0 → 0.7.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 7e0827edebfc4f170f5a750a27e04cac4fb5dd158fcc85ab47d684a777d6dac8
4
- data.tar.gz: 3922682a35baff70fb5492f6f21a8feca3ceb24cf549c7678caca26f922814bb
3
+ metadata.gz: 396333c73bb2a3098fb03607b6c9f4ac31d6ad2fee6abc425ebacae6a3329ec2
4
+ data.tar.gz: 852fe8f267af779dd3d4beda42f8bc488b09aca401c98fa784351846dad7cb19
5
5
  SHA512:
6
- metadata.gz: 6b699a914a9e2b929b14180e0251ff4ab77ab41e53ec5cf7becff1abf0210fbd477d0f7a4b92f4a77015cf1b659f622d26eb3828e74fc8957edcf6d1f03449f8
7
- data.tar.gz: 177c5c3577a4a7b31e480b9c85c097def09cdb1aa546da8dc82626239afd6b97b138bc5707f2a7df07c342015be3ad5cd3160f28df27e25ef55633afc1f11957
6
+ metadata.gz: 14e2e611856c718242e81eab276c2a988f5ea98e26ca57b3a951104785b96787e023a38c786b88dd198482a97901220a93e3070c783a1ffc71e1a3bcd82cc8c6
7
+ data.tar.gz: cd126e9ac493657f40ecd7429706d40ee954db197be71bdb37f5f93083d766763db7aa2bb24971aa2fcfa352df60a2e13b87421094f9208949bbeb3a36b43f79
data/.rubocop.yml CHANGED
@@ -11,7 +11,7 @@ Metrics/AbcSize:
11
11
  Max: 25
12
12
 
13
13
  Metrics/ClassLength:
14
- Max: 280
14
+ Max: 310
15
15
 
16
16
  Metrics/MethodLength:
17
17
  Max: 11
data/CHANGELOG.md CHANGED
@@ -1,5 +1,13 @@
1
1
  # Change Log
2
2
 
3
+ ## [0.7.0] - 2026-05-16
4
+
5
+ - Add `Stock.get_fx_rate(from, to)` returning 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; returns `nil` on error.
6
+
7
+ ## [0.6.0] - 2026-05-16
8
+
9
+ - Expose Yahoo's `currency` field in `get_quote` / `get_quotes` results so callers can support multi-currency portfolios
10
+
3
11
  ## [0.5.0] - 2026-05-15
4
12
 
5
13
  - Add `Stock.search(query, count:)` returning matching tickers from Yahoo's `v1/finance/search` autocomplete endpoint
data/README.md CHANGED
@@ -38,7 +38,7 @@ Fetch stock data by passing a ticker symbol:
38
38
  ```ruby
39
39
  YahooFinanceClient::Stock.get_quote("AAPL")
40
40
  # => {
41
- # symbol: "AAPL", name: "Apple Inc.", price: 182.52,
41
+ # symbol: "AAPL", name: "Apple Inc.", price: 182.52, currency: "USD",
42
42
  # change: 1.25, percent_change: 0.69, volume: 48123456,
43
43
  # pe_ratio: 28.5, eps: 6.40,
44
44
  # dividend: 0.96, dividend_yield: 0.53, payout_ratio: 15.0,
@@ -74,6 +74,16 @@ YahooFinanceClient::Stock.search("apple", count: 5)
74
74
 
75
75
  Search returns `[]` if the query is empty or the upstream request fails.
76
76
 
77
+ ### FX Rates
78
+
79
+ Fetch a current FX rate between two ISO 4217 currency codes via Yahoo's `<FROM><TO>=X` quote symbol:
80
+ ```ruby
81
+ YahooFinanceClient::Stock.get_fx_rate("EUR", "USD")
82
+ # => 1.0823
83
+ ```
84
+
85
+ Identity pairs short-circuit to `1.0` without hitting the API. Returns `nil` on any upstream error.
86
+
77
87
  ### Dividend History
78
88
 
79
89
  Fetch historical dividend payments via the chart API:
@@ -37,6 +37,19 @@ module YahooFinanceClient
37
37
  fetch_from_cache(cache_key) || fetch_and_cache_dividend_history(cache_key, symbol, range)
38
38
  end
39
39
 
40
+ # Fetch the current FX rate between two ISO 4217 currency codes via Yahoo's
41
+ # `<FROM><TO>=X` quote symbol. Returns 1.0 for identity pairs without hitting the API.
42
+ #
43
+ # @param from [String] source currency code (e.g. "EUR")
44
+ # @param to [String] target currency code (e.g. "USD")
45
+ # @return [Float, nil] one unit of `from` expressed in `to`, or nil on error
46
+ def get_fx_rate(from, to)
47
+ return 1.0 if from == to
48
+
49
+ cache_key = "fx_#{from}_#{to}"
50
+ fetch_from_cache(cache_key) || fetch_and_cache_fx_rate(cache_key, from, to)
51
+ end
52
+
40
53
  # Search the Yahoo Finance autocomplete index for matching symbols.
41
54
  #
42
55
  # @param query [String] free-text query (ticker or company name)
@@ -162,7 +175,7 @@ module YahooFinanceClient
162
175
 
163
176
  def build_quote_hash(quote, price, dividend, eps)
164
177
  {
165
- symbol: quote["symbol"], name: quote["shortName"], price: price,
178
+ symbol: quote["symbol"], name: quote["shortName"], price: price, currency: quote["currency"],
166
179
  change: quote["regularMarketChange"], percent_change: quote["regularMarketChangePercent"],
167
180
  volume: quote["regularMarketVolume"], pe_ratio: quote["trailingPE"], eps: eps,
168
181
  dividend: dividend, dividend_yield: calculate_yield(dividend, price),
@@ -192,6 +205,36 @@ module YahooFinanceClient
192
205
  Time.at(value).utc.to_date
193
206
  end
194
207
 
208
+ def fetch_and_cache_fx_rate(cache_key, from, to)
209
+ rate = fetch_fx_rate_data("#{from}#{to}=X")
210
+ store_in_cache(cache_key, rate) if rate
211
+ rate
212
+ end
213
+
214
+ def fetch_fx_rate_data(symbol)
215
+ retries = 0
216
+ begin
217
+ parse_fx_response(make_authenticated_request(symbol))
218
+ rescue AuthenticationError
219
+ retries += 1
220
+ retry if retries <= MAX_RETRIES
221
+ nil
222
+ end
223
+ end
224
+
225
+ def parse_fx_response(response)
226
+ if auth_error?(response)
227
+ Session.instance.invalidate!
228
+ raise AuthenticationError, "Authentication failed"
229
+ end
230
+ return nil unless response.success?
231
+
232
+ rate = JSON.parse(response.body).dig("quoteResponse", "result", 0, "regularMarketPrice")
233
+ rate&.to_f
234
+ rescue JSON::ParserError
235
+ nil
236
+ end
237
+
195
238
  def fetch_and_cache_dividend_history(cache_key, symbol, range)
196
239
  data = fetch_dividend_history_data(symbol, range)
197
240
  store_in_cache(cache_key, data) unless data.empty?
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module YahooFinanceClient
4
- VERSION = "0.5.0"
4
+ VERSION = "0.7.0"
5
5
  end
metadata CHANGED
@@ -1,13 +1,13 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: yahoo_finance_client
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.5.0
4
+ version: 0.7.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Francesc Leveque
8
8
  bindir: exe
9
9
  cert_chain: []
10
- date: 2026-05-15 00:00:00.000000000 Z
10
+ date: 2026-05-16 00:00:00.000000000 Z
11
11
  dependencies:
12
12
  - !ruby/object:Gem::Dependency
13
13
  name: csv