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)
ECB_RATES_URL =
'http://www.ecb.europa.eu/stats/eurofxref/eurofxref-daily.xml'
ECB_90_DAY_URL =
'http://www.ecb.europa.eu/stats/eurofxref/eurofxref-hist-90d.xml'

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#historical_last_updatedObject

Returns the value of attribute historical_last_updated.



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

def historical_last_updated
  @historical_last_updated
end

#historical_rates_updated_atObject

Returns the value of attribute historical_rates_updated_at.



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

def historical_rates_updated_at
  @historical_rates_updated_at
end

#last_updatedObject

Returns the value of attribute last_updated.



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

def last_updated
  @last_updated
end

#rates_updated_atObject

Returns the value of attribute rates_updated_at.



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

def rates_updated_at
  @rates_updated_at
end

Instance Method Details

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



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

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



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

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



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

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:



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

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



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

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

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



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

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



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

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

#update_rates(cache = nil) ⇒ Object



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

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

#update_rates_from_s(content) ⇒ Object



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

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