Class: ExchangeRate::RateSources::ECBRateRetriever

Inherits:
Object
  • Object
show all
Defined in:
lib/exchange_rate/rate_sources/e_c_b_rate_retriever.rb

Overview

Retrieves FX rates from the 90 day European Central Bank (ECB) feed.

Constant Summary collapse

DEFAULT_FEED_URL =

The ECB feed URL

'http://www.ecb.europa.eu/stats/eurofxref/eurofxref-hist-90d.xml'

Instance Method Summary collapse

Constructor Details

#initialize(feed_url: DEFAULT_FEED_URL) ⇒ ECBRateRetriever

Builds a new ECBRateRetriever.

  • feed_url - The URL to request the ECB feed from.



21
22
23
# File 'lib/exchange_rate/rate_sources/e_c_b_rate_retriever.rb', line 21

def initialize(feed_url: DEFAULT_FEED_URL)
  @feed_url = feed_url
end

Instance Method Details

#save!Object

Retrieve the feed, parse the contents, and update the local cache.

Returns nothing Raises ExchangeRate::RetrievalFailedError if the feed could not be loaded Raises ExchangeRate::RetrievalFailedError if the cache could not be updated – TODO: This makes 1 database call per record to be saved. A bulk-insert would speed

things up considerably.

++



35
36
37
38
39
40
41
42
43
44
# File 'lib/exchange_rate/rate_sources/e_c_b_rate_retriever.rb', line 35

def save!
  retrieve_feed
    .yield_self { |feed_body| parse_feed(feed_body) }
    .map { |rate_date_hash| rates_from(rate_date_hash, date_from(rate_date_hash)) }
    .flatten
    .map(&:save)
  nil
rescue StandardError
  raise ExchangeRate::RetrievalFailedError
end