The personal finance app for everyone (by everyone)
Ruby
Map Frankfurt, Brussels, Lisbon, Vienna, Dublin and German regional EODHD exchanges (#5150)
arnaurodondev1f0a70ea46de
diff --git a/app/models/provider/eodhd.rb b/app/models/provider/eodhd.rbindex 0f32345265..4a77fa6b3b 100644--- a/app/models/provider/eodhd.rb+++ b/app/models/provider/eodhd.rb@@ -49,9 +49,16 @@ class Provider::Eodhd < Provider"XBOM" => "BSE","XNSE" => "NSE","XWAR" => "WAR",+ # Further European venues, using the MICs from EODHD's own exchange list+ # (https://eodhd.com/list-of-stock-markets)+ "XFRA" => "F", "XBRU" => "BR", "XLIS" => "LS", "XWBO" => "VI", "XDUB" => "IR",+ # German regional exchanges+ "XBER" => "BE", "XDUS" => "DU", "XHAM" => "HM", "XHAN" => "HA", "XMUN" => "MU", "XSTU" => "STU",# Still accept legacy rows / inputs that store the raw EODHD code as MIC.# Persistence always canonicalizes WAR → XWAR via Security::MIC_ALIASES."WAR" => "WAR",+ "F" => "F", "BR" => "BR", "LS" => "LS", "VI" => "VI", "IR" => "IR",+ "BE" => "BE", "DU" => "DU", "HM" => "HM", "HA" => "HA", "MU" => "MU", "STU" => "STU","EUFUND" => "EUFUND"}.freeze@@ -62,7 +69,9 @@ class Provider::Eodhd < Provider"TSE" => "XTKS", "MI" => "XMIL", "MC" => "XMAD","OL" => "XOSL", "HE" => "XHEL", "CO" => "XCSE","ST" => "XSTO", "KS" => "XKRX", "BSE" => "XBOM",- "NSE" => "XNSE", "WAR" => "XWAR"+ "NSE" => "XNSE", "WAR" => "XWAR",+ "F" => "XFRA", "BR" => "XBRU", "LS" => "XLIS", "VI" => "XWBO", "IR" => "XDUB",+ "BE" => "XBER", "DU" => "XDUS", "HM" => "XHAM", "HA" => "XHAN", "MU" => "XMUN", "STU" => "XSTU"}.freezeEODHD_COUNTRY_TO_CODE = {@@ -82,6 +91,8 @@ class Provider::Eodhd < Provider"ST" => "SEK", "KS" => "KRW", "BSE" => "INR","NSE" => "INR","WAR" => "PLN",+ "F" => "EUR", "BR" => "EUR", "LS" => "EUR", "VI" => "EUR", "IR" => "EUR",+ "BE" => "EUR", "DU" => "EUR", "HM" => "EUR", "HA" => "EUR", "MU" => "EUR", "STU" => "EUR","EUFUND" => "EUR"}.freezediff --git a/app/models/security.rb b/app/models/security.rbindex 9ca84e9c04..d82e34d098 100644--- a/app/models/security.rb+++ b/app/models/security.rb@@ -13,7 +13,10 @@ class Security < ApplicationRecord# (e.g. raw EODHD exchange codes) mapped to their ISO MIC.MIC_ALIASES = {"WAR" => "XWAR",- "GER" => "XETR" # Yahoo search code for Xetra, stored before it was mapped+ "GER" => "XETR", # Yahoo search code for Xetra, stored before it was mapped+ # Raw EODHD codes that Provider::Eodhd still accepts for rows stored before it mapped them+ "F" => "XFRA", "BR" => "XBRU", "LS" => "XLIS", "VI" => "XWBO", "IR" => "XDUB",+ "BE" => "XBER", "DU" => "XDUS", "HM" => "XHAM", "HA" => "XHAN", "MU" => "XMUN", "STU" => "XSTU"}.freezeKINDS = %w[standard cash].freezediff --git a/test/models/provider/eodhd_test.rb b/test/models/provider/eodhd_test.rbindex 56f0abbf64..54d0f655b6 100644--- a/test/models/provider/eodhd_test.rb+++ b/test/models/provider/eodhd_test.rb@@ -118,6 +118,58 @@ class Provider::EodhdTest < ActiveSupport::TestCaseassert_equal "NSRGY.US", @provider.send(:eodhd_symbol, "NSRGY", "XOTC")end+ EODHD_EUROPEAN_VENUES = {+ "XFRA" => "F", "XBRU" => "BR", "XLIS" => "LS", "XWBO" => "VI", "XDUB" => "IR",+ "XBER" => "BE", "XDUS" => "DU", "XHAM" => "HM", "XHAN" => "HA", "XMUN" => "MU", "XSTU" => "STU"+ }.freeze++ test "eodhd_symbol maps further European MICs to their EODHD codes" do+ EODHD_EUROPEAN_VENUES.each do |mic, code|+ assert_equal "ABC.#{code}", @provider.send(:eodhd_symbol, "ABC", mic), "#{mic} should map to #{code}"+ end+ end++ test "eodhd_symbol still accepts the raw EODHD code stored by earlier searches" do+ EODHD_EUROPEAN_VENUES.each_value do |code|+ assert_equal "ABC.#{code}", @provider.send(:eodhd_symbol, "ABC", code)+ end+ end++ test "currency_for the further European venues falls back to EUR" do+ EODHD_EUROPEAN_VENUES.each do |mic, code|+ assert_equal "EUR", @provider.send(:currency_for, exchange_operating_mic: mic, symbol: "ABC")+ assert_equal "EUR", @provider.send(:currency_for, exchange_operating_mic: code, symbol: "ABC")+ end+ end++ test "currency_for reads the currency a search cached under the MIC for a legacy raw-code row" do+ EODHD_EUROPEAN_VENUES.each do |mic, code|+ Rails.cache.stubs(:read).with("eodhd:currency:ABC:#{mic}").returns("NOK")+ Rails.cache.stubs(:read).with("eodhd:currency:ABC:#{code}").returns(nil)++ assert_equal "NOK", @provider.send(:currency_for, exchange_operating_mic: code, symbol: "ABC"), "legacy #{code} should find the #{mic} cache"+ end+ end++ test "search maps further European EODHD exchange codes to MICs Sure can name" do+ body = EODHD_EUROPEAN_VENUES.values.map do |code|+ { "Code" => "ABC#{code}", "Name" => "Fund #{code}", "Exchange" => code, "Country" => "Germany", "Currency" => "EUR" }+ end.to_json++ mock_response = mock+ mock_response.stubs(:body).returns(body)+ @provider.stubs(:enforce_daily_limit!)+ @provider.stubs(:throttle_request)+ @provider.stubs(:client).returns(mock_client = mock)+ mock_client.stubs(:get).returns(mock_response)++ result = @provider.search_securities("ABC")++ assert result.success?+ assert_equal EODHD_EUROPEAN_VENUES.keys, result.data.map(&:exchange_operating_mic)+ assert result.data.all? { |s| ::Security::EXCHANGES.key?(s.exchange_operating_mic) }+ end+test "eodhd_symbol builds Warsaw ticker from XWAR MIC" doassert_equal "KTY.WAR", @provider.send(:eodhd_symbol, "KTY", "XWAR")enddiff --git a/test/models/security_test.rb b/test/models/security_test.rbindex de641740d2..fe0805b7a5 100644--- a/test/models/security_test.rb+++ b/test/models/security_test.rb@@ -186,6 +186,19 @@ class SecurityTest < ActiveSupport::TestCaseassert_equal "XETR", found.reload.exchange_operating_micend+ test "find_by_ticker_and_exchange reuses legacy rows stored under raw EODHD codes" do+ { "F" => "XFRA", "BR" => "XBRU", "LS" => "XLIS", "VI" => "XWBO", "IR" => "XDUB", "BE" => "XBER",+ "DU" => "XDUS", "HM" => "XHAM", "HA" => "XHAN", "MU" => "XMUN", "STU" => "XSTU" }.each do |raw, mic|+ legacy = Security.create!(ticker: "ABC#{raw}", exchange_operating_mic: mic)+ legacy.update_columns(exchange_operating_mic: raw)++ found = Security.find_by_ticker_and_exchange(ticker: "ABC#{raw}", exchange_operating_mic: mic)++ assert_equal legacy.id, found&.id, "#{raw} row should be found for #{mic}"+ assert_equal mic, found.reload.exchange_operating_mic+ end+ end+test "saving a legacy GER row keeps its MIC when an XETR row already exists" doSecurity.create!(ticker: "SXR8.DE", exchange_operating_mic: "XETR")legacy = Security.new(ticker: "SXR8.DE", exchange_operating_mic: "GER")