Class: EuCentralBank

Inherits:
Money::Bank::VariableExchange
  • Object
show all
Defined in:
lib/eu_central_bank.rb

Constant Summary collapse

CURRENCIES =
%w(USD JPY BGN CZK DKK GBP HUF ILS PLN RON SEK CHF NOK HRK RUB TRY AUD BRL CAD CNY HKD IDR INR KRW MXN MYR NZD PHP SGD THB ZAR).map(&:freeze).freeze
ECB_RATES_URL =
'http://www.ecb.europa.eu/stats/eurofxref/eurofxref-daily.xml'.freeze
ECB_90_DAY_URL =
'http://www.ecb.europa.eu/stats/eurofxref/eurofxref-hist-90d.xml'.freeze

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#historical_last_updatedObject

Returns the value of attribute historical_last_updated.



11
12
13
# File 'lib/eu_central_bank.rb', line 11

def historical_last_updated
  @historical_last_updated
end

#historical_rates_updated_atObject

Returns the value of attribute historical_rates_updated_at.



12
13
14
# File 'lib/eu_central_bank.rb', line 12

def historical_rates_updated_at
  @historical_rates_updated_at
end

#last_updatedObject

Returns the value of attribute last_updated.



9
10
11
# File 'lib/eu_central_bank.rb', line 9

def last_updated
  @last_updated
end

#rates_updated_atObject

Returns the value of attribute rates_updated_at.



10
11
12
# File 'lib/eu_central_bank.rb', line 10

def rates_updated_at
  @rates_updated_at
end

Instance Method Details

#exchange(cents, from_currency, to_currency, date = nil) ⇒ Object



42
43
44
# File 'lib/eu_central_bank.rb', line 42

def exchange(cents, from_currency, to_currency, date=nil)
  exchange_with(Money.new(cents, from_currency), to_currency, date)
end

#exchange_with(from, to_currency, date = nil) ⇒ Object



46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
# File 'lib/eu_central_bank.rb', line 46

def exchange_with(from, to_currency, date=nil)
  from_base_rate, to_base_rate = nil, nil
  rate = get_rate(from, to_currency, {:date => date})

  unless rate
    @mutex.synchronize do
      opts = { :date => date, :without_mutex => true }
      from_base_rate = get_rate("EUR", from.currency.to_s, opts)
      to_base_rate = get_rate("EUR", to_currency, opts)
    end
    rate = to_base_rate / from_base_rate
  end

  calculate_exchange(from, to_currency, rate)
end

#get_rate(from, to, opts = {}) ⇒ Object



62
63
64
65
66
67
68
69
70
# File 'lib/eu_central_bank.rb', line 62

def get_rate(from, to, opts = {})
  fn = -> { @rates[rate_key_for(from, to, opts)] }

  if opts[:without_mutex]
    fn.call
  else
    @mutex.synchronize { fn.call }
  end
end

#save_rates(cache, url = ECB_RATES_URL) ⇒ Object

Raises:



26
27
28
29
30
31
32
# File 'lib/eu_central_bank.rb', line 26

def save_rates(cache, url=ECB_RATES_URL)
  raise InvalidCache unless cache
  File.open(cache, "w") do |file|
    io = open(url);
    io.each_line { |line| file.puts line }
  end
end

#save_rates_to_s(url = ECB_RATES_URL) ⇒ Object



38
39
40
# File 'lib/eu_central_bank.rb', line 38

def save_rates_to_s(url=ECB_RATES_URL)
  open(url).read
end

#set_rate(from, to, rate, opts = {}) ⇒ Object



72
73
74
75
76
77
78
79
80
# File 'lib/eu_central_bank.rb', line 72

def set_rate(from, to, rate, opts = {})
  fn = -> { @rates[rate_key_for(from, to, opts)] = rate }

  if opts[:without_mutex]
    fn.call
  else
    @mutex.synchronize { fn.call }
  end
end

#update_historical_rates(cache = nil) ⇒ Object



22
23
24
# File 'lib/eu_central_bank.rb', line 22

def update_historical_rates(cache=nil)
  update_parsed_historical_rates(doc(cache, ECB_90_DAY_URL))
end

#update_rates(cache = nil) ⇒ Object



18
19
20
# File 'lib/eu_central_bank.rb', line 18

def update_rates(cache=nil)
  update_parsed_rates(doc(cache))
end

#update_rates_from_s(content) ⇒ Object



34
35
36
# File 'lib/eu_central_bank.rb', line 34

def update_rates_from_s(content)
  update_parsed_rates(doc_from_s(content))
end