RubyMoney / eu_central_bank

A gem that calculates the exchange rate using published rates from European Central Bank. Compatible with the money gem
MIT License
217 stars 130 forks source link

update_rates fails to connect to server #109

Closed vanboom closed 3 years ago

vanboom commented 3 years ago

We download eurofxref-daily.xml externally to our application and then make this call in our money initializer. The intent of this is to not have our server reaching out to fetch the exchange rate file as described in the README.

eu_bank = EuCentralBank.new
eu_bank.update_rates(File.join(Rails.root, "eurofxref-daily.xml"))

We have discovered an issue if the update_rates method is not able to read the cache file, it attempts to download it anyway. This behavior is subtle and not expected when updated_rates is advertised as simply loading a cache file.

See eu_central_bank.rb around line 168

  def doc(cache, url=ECB_RATES_URL)
    rates_source = !!cache ? cache : url
    Nokogiri::XML(open_url(rates_source)).tap { |doc| 
    doc.xpath('gesmes:Envelope/xmlns:Cube/xmlns:Cube//xmlns:Cube') }
  rescue Nokogiri::XML::XPath::SyntaxError
    Nokogiri::XML(open_url(url))
  end

The rescue path here should not attempt to make a network connection. Thank you for considering this change.