Class: Exchange::ExternalAPI::Ecb

Inherits:
XML
  • Object
show all
Defined in:
lib/exchange/external_api/ecb.rb

Overview

The ECB class, handling communication with the European Central Bank XML File API You can find further information on the European Central Bank XML API API here: www.ecb.int/stats/exchange/eurofxref/html/index.en.html

Author:

  • Beat Richartz

Since:

  • 0.3

Version:

  • 0.7

Constant Summary collapse

API_URL =

The base of the ECB API URL

Since:

  • 0.3

"www.ecb.europa.eu/stats/eurofxref"
CURRENCIES =

The currencies the ECB API URL can handle

Since:

  • 0.3

[:eur, :usd, :jpy, :bgn, :czk, :dkk, :gbp, :huf, :ltl, :lvl, :pln, :ron, :sek, :chf, :nok, :hrk, :rub, :try, :aud, :brl, :cad, :cny, :hkd, :idr, :ils, :inr, :krw, :mxn, :myr, :nzd, :php, :sgd, :thb, :zar]

Instance Attribute Summary collapse

Attributes inherited from Base

#base, #cache, #helper, #rates, #timestamp

Instance Method Summary collapse

Methods inherited from XML

#initialize

Methods inherited from Base

#convert, #initialize, #rate, #test_for_rates_and_raise_if_nil, #to_hash!

Constructor Details

This class inherits a constructor from Exchange::ExternalAPI::XML

Instance Attribute Details

#callresultObject

The result of the api call to the Central bank

Since:

  • 0.3


22
23
24
# File 'lib/exchange/external_api/ecb.rb', line 22

def callresult
  @callresult
end

Instance Method Details

#update(opts = {}) ⇒ Object

Updates the rates by getting the information from ECB API for today or a defined historical date The call gets cached for a maximum of 24 hours. Getting history from ECB is a bit special, since they do not seem to have any smaller portion history than an epic 4MB XML history file and a 90 day recent history file. We get each of that once and cache it in smaller portions.

Examples:

Update the ecb API to use the file of March 2, 2010

Exchange::ExternalAPI::Ecb.new.update(:at => Time.gm(3,2,2010))

Parameters:

  • opts (Hash) (defaults to: {})

    Options to define for the API Call

Options Hash (opts):

  • :at (Time, String)

    a historical date to get the exchange rates for

Since:

  • 0.1

Version:

  • 0.7


35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
# File 'lib/exchange/external_api/ecb.rb', line 35

def update opts={}
  time          = helper.assure_time(opts[:at], :default => :now)
  times         = map_retry_times time
  
  Call.new(api_url(time), call_opts(time)) do |result|
    t = time
    
    # Weekends do not have rates present
    #
    t = times.shift while (r = find_rate!(result, t)).empty? && !times.empty?
              
    @base                 = :eur # We just have to assume, since it's the ECB
    @rates                = extract_rates(r.children)
    @timestamp            = time.to_i
  end
end