Skip to content

armgit / sure

Public Imported from GitHub
0

The personal finance app for everyone (by everyone)

Ruby

EODHD: lift the one-year history clamp for paid keys and stop retrying failed fundamentals (#5022)

arnaurodondev656a008090fc
diff --git a/app/models/provider/eodhd.rb b/app/models/provider/eodhd.rb
index f03e85f20b..1268378805 100644
--- a/app/models/provider/eodhd.rb
+++ b/app/models/provider/eodhd.rb
@@ -13,9 +13,10 @@ class Provider::Eodhd < Provider
# Maximum API calls per day (EODHD free/basic plans are very restrictive)
MAX_REQUESTS_PER_DAY = 20
- # EODHD free tier provides ~1 year of EOD data
+ # The free tier provides ~1 year of EOD data; paid plans go back 30+ years.
+ # Raising EODHD_MAX_REQUESTS_PER_DAY above the free cap signals a paid key.
def max_history_days
- 365
+ 365 if max_requests_per_day <= MAX_REQUESTS_PER_DAY
end
# EODHD uses {SYMBOL}.{EXCHANGE} ticker format with its own exchange codes
@@ -168,19 +169,28 @@ class Provider::Eodhd < Provider
def fetch_security_info(symbol:, exchange_operating_mic:)
with_provider_response do
+ ticker = eodhd_symbol(symbol, exchange_operating_mic)
+ # Fundamentals cost 10 API calls and fail on plans without them, so don't
+ # retry a permanently failed lookup (no plan access, unknown ticker) on every sync.
+ failed_key = "eodhd:fundamentals_failed:#{Digest::SHA256.hexdigest(api_key.to_s)[0, 8]}:#{ticker}"
+ raise Error, "Fundamentals for #{ticker} failed recently; retrying later" if Rails.cache.read(failed_key)
+
enforce_daily_limit!
throttle_request
- ticker = eodhd_symbol(symbol, exchange_operating_mic)
+ begin
+ response = client.get("#{base_url}/api/fundamentals/#{CGI.escape(ticker)}") do |req|
+ req.params["api_token"] = api_key
+ req.params["fmt"] = "json"
+ end
- response = client.get("#{base_url}/api/fundamentals/#{CGI.escape(ticker)}") do |req|
- req.params["api_token"] = api_key
- req.params["fmt"] = "json"
+ parsed = JSON.parse(response.body)
+ check_api_error!(parsed)
+ rescue Faraday::ForbiddenError, Faraday::ResourceNotFound, Error
+ Rails.cache.write(failed_key, true, expires_in: 7.days)
+ raise
end
- parsed = JSON.parse(response.body)
- check_api_error!(parsed)
-
general = parsed.dig("General") || {}
SecurityInfo.new(
diff --git a/config/locales/views/settings/hostings/en.yml b/config/locales/views/settings/hostings/en.yml
index 731dcebdc0..f47a7c9531 100644
--- a/config/locales/views/settings/hostings/en.yml
+++ b/config/locales/views/settings/hostings/en.yml
@@ -284,7 +284,7 @@ en:
step_1_html: 'Visit <a href="https://eodhd.com/register" target="_blank" rel="noopener noreferrer" class="underline">eodhd.com</a> and create a free account.'
step_2_html: 'Go to your <a href="https://eodhd.com/cp/dashboard" target="_blank" rel="noopener noreferrer" class="underline">Dashboard</a> to find your API token.'
step_3: Copy your API token and paste it below.
- rate_limit_warning: "EODHD free tier is limited to 20 API calls per day. Best used as a supplementary provider for EU ETFs not available from other providers."
+ rate_limit_warning: "EODHD free tier is limited to 20 API calls per day and 1 year of history. Paid plan only: set EODHD_MAX_REQUESTS_PER_DAY to your plan's daily limit; this also lifts the history limit."
mansa_settings:
title: Mansa
description: Enter the API key from Mansa API. Supports 20 African stock exchanges, including NGX (Nigerian Exchange), JSE, NSE (Kenya), GSE (Ghana) and BRVM -- several of which no other provider here covers.
diff --git a/test/models/provider/eodhd_test.rb b/test/models/provider/eodhd_test.rb
index f89ed7daf0..876c263bad 100644
--- a/test/models/provider/eodhd_test.rb
+++ b/test/models/provider/eodhd_test.rb
@@ -204,4 +204,42 @@ class Provider::EodhdTest < ActiveSupport::TestCase
assert result.success?
assert_equal "USD", result.data.first.currency
end
+
+ test "clamps history to one year at the free-tier request cap" do
+ with_env_overrides("EODHD_MAX_REQUESTS_PER_DAY" => nil) do
+ assert_equal 365, @provider.max_history_days
+ end
+ end
+
+ test "does not clamp history when the request cap is raised for a paid plan" do
+ with_env_overrides("EODHD_MAX_REQUESTS_PER_DAY" => "100000") do
+ assert_nil @provider.max_history_days
+ end
+ end
+
+ test "fetch_security_info does not retry a failed fundamentals lookup" do
+ Rails.stubs(:cache).returns(ActiveSupport::Cache::MemoryStore.new)
+ @provider.stubs(:throttle_request)
+ @provider.stubs(:client).returns(mock_client = mock)
+ mock_client.expects(:get).once.raises(Faraday::ForbiddenError.new("403"))
+
+ 2.times do
+ result = @provider.fetch_security_info(symbol: "AAPL", exchange_operating_mic: "XNYS")
+ assert_not result.success?
+ end
+ assert_equal 1, Rails.cache.read("eodhd:daily:#{Date.current}")
+ end
+
+ test "fetch_security_info retries after a transient failure" do
+ Rails.stubs(:cache).returns(ActiveSupport::Cache::MemoryStore.new)
+ @provider.stubs(:throttle_request)
+ ok = mock
+ ok.stubs(:body).returns({ "General" => { "Name" => "Apple Inc" } }.to_json)
+ @provider.stubs(:client).returns(mock_client = mock)
+ mock_client.expects(:get).times(3).raises(Faraday::ServerError.new("502"))
+ .then.raises(Faraday::TimeoutError.new("timeout")).then.returns(ok)
+
+ 2.times { assert_not @provider.fetch_security_info(symbol: "AAPL", exchange_operating_mic: "XNYS").success? }
+ assert @provider.fetch_security_info(symbol: "AAPL", exchange_operating_mic: "XNYS").success?
+ end
end