Class: ExchangeRatesGenerator::Sources::Ecb

Inherits:
Base
  • Object
show all
Defined in:
lib/exchange-rates-generator/sources/ecb.rb

Overview

ECB: European Central Bank home page

Class Method Summary collapse

Methods inherited from Base

inherited, rates_for, sources

Class Method Details

.all_ratesHash

Retrieves all the currency rates from the external source, this is a Hash of currency codes and exchange rates. The currency codes will be upcased, Symbols (e.g. :USD, :NZD). The exchange rates will be with respect to the base_currency.

Returns:

  • (Hash)

    The rates as a Hash of currency codes => exchange rates.



25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
# File 'lib/exchange-rates-generator/sources/ecb.rb', line 25

def all_rates
  session = Patron::Session.new
  session.timeout = 30000               # 10 secs
  session.connect_timeout = 2000        # 2 secs

  ExchangeRatesGenerator.logger.info "Retrieving exchange rates from #{source}..."

  http_response = session.get(source)

  if ['404', '405', '406', '410'].include?(http_response.status)
    # Not Found, auth failure, etc. Some form of it wasn't there.
    raise Errors::NotFoundError, "The exchange rate source file seems to be unavailable"
  elsif !['1', '2'].include?(http_response.status.to_s[0..0])
    # Other non-specific failures.
    raise Errors::UnexpectedError, "Error while making a request to #{source}\nResponse: #{http_response.inspect}"
  end

  convert_to_exchange_rate_hash(http_response.body)
end

.base_currencySymbol

Retrieves the currency code (as an uppercase symbol) that represents this Source’s Base currency.

Returns:

  • (Symbol)

    The currency code of this Source’s base currency



9
10
11
# File 'lib/exchange-rates-generator/sources/ecb.rb', line 9

def base_currency
  :EUR
end

.descriptionString

Retrieves a human readable description of the Source.

Returns:

  • (String)

    The description.



16
17
18
# File 'lib/exchange-rates-generator/sources/ecb.rb', line 16

def description
  "European Central Bank currency feed"
end