Class: EuCentralBank

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

Defined Under Namespace

Classes: RatesDocument

Constant Summary collapse

SERIALIZER_DATE_SEPARATOR =
'_AT_'
DECIMAL_PRECISION =
5
CURRENCIES =
%w(USD JPY BGN CZK DKK GBP HUF ILS ISK 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 =
'https://www.ecb.europa.eu/stats/eurofxref/eurofxref-daily.xml'.freeze
ECB_90_DAY_URL =
'https://www.ecb.europa.eu/stats/eurofxref/eurofxref-hist-90d.xml'.freeze
ECB_ALL_HIST_URL =
'https://www.ecb.europa.eu/stats/eurofxref/eurofxref-hist.xml'.freeze
LEGACY_CURRENCIES =
%w(CYP SIT ROL TRL)

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(st = Money::RatesStore::StoreWithHistoricalDataSupport.new, &block) ⇒ EuCentralBank

Returns a new instance of EuCentralBank.



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

def initialize(st = Money::RatesStore::StoreWithHistoricalDataSupport.new, &block)
  super
  @currency_string = nil
end

Instance Attribute Details

#historical_last_updatedObject

Returns the value of attribute historical_last_updated.



15
16
17
# File 'lib/eu_central_bank.rb', line 15

def historical_last_updated
  @historical_last_updated
end

#historical_rates_updated_atObject

Returns the value of attribute historical_rates_updated_at.



16
17
18
# File 'lib/eu_central_bank.rb', line 16

def historical_rates_updated_at
  @historical_rates_updated_at
end

#last_updatedObject

Returns the value of attribute last_updated.



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

def last_updated
  @last_updated
end

#rates_updated_atObject

Returns the value of attribute rates_updated_at.



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

def rates_updated_at
  @rates_updated_at
end

Instance Method Details

#check_currency_available(currency) ⇒ Object



166
167
168
169
170
171
# File 'lib/eu_central_bank.rb', line 166

def check_currency_available(currency)
  currency_string = currency.to_s
  return true if currency_string == "EUR"
  return true if CURRENCIES.include?(currency_string)
  raise CurrencyUnavailable, "No rates available for #{currency_string}"
end

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



62
63
64
# File 'lib/eu_central_bank.rb', line 62

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



66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
# File 'lib/eu_central_bank.rb', line 66

def exchange_with(from, to_currency, date=nil)
  from_base_rate, to_base_rate = nil, nil
  rate = get_rate(from.currency, to_currency, date)

  unless rate
    store.transaction true do
      from_base_rate = get_rate("EUR", from.currency.to_s, date)
      to_base_rate = get_rate("EUR", to_currency, date)
    end

    unless from_base_rate && to_base_rate
      message = "No conversion rate known for '#{from.currency.iso_code}' -> '#{to_currency}'"
      message << " on #{date.to_s}" if date

      raise Money::Bank::UnknownRate, message
    end

    rate = to_base_rate / from_base_rate
  end

  calculate_exchange(from, to_currency, rate)
end

#export_rates(format, file = nil, opts = {}) ⇒ Object

Raises:

  • (Money::Bank::UnknownRateFormat)


119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
# File 'lib/eu_central_bank.rb', line 119

def export_rates(format, file = nil, opts = {})
  raise Money::Bank::UnknownRateFormat unless
    RATE_FORMATS.include? format

  store.transaction true do
    s = case format
    when :json
      JSON.dump(rates)
    when :ruby
      Marshal.dump(rates)
    when :yaml
      YAML.dump(rates)
    end

    unless file.nil?
      File.open(file, "w") {|f| f.write(s) }
    end

    s
  end
end

#get_rate(from, to, date = nil) ⇒ Object



89
90
91
92
93
94
95
96
97
98
99
100
101
# File 'lib/eu_central_bank.rb', line 89

def get_rate(from, to, date = nil)
  return 1 if from == to

  check_currency_available(from)
  check_currency_available(to)

  if date.is_a?(Hash)
    # Backwards compatibility for the opts hash
    date = date[:date]
  end

  store.get_rate(::Money::Currency.wrap(from).iso_code, ::Money::Currency.wrap(to).iso_code, date)
end

#import_rates(format, s, opts = {}) ⇒ Object

Raises:

  • (Money::Bank::UnknownRateFormat)


141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
# File 'lib/eu_central_bank.rb', line 141

def import_rates(format, s, opts = {})
  raise Money::Bank::UnknownRateFormat unless
    RATE_FORMATS.include? format

  store.transaction true do
    data = case format
     when :json
       JSON.load(s)
     when :ruby
       Marshal.load(s)
     when :yaml
       YAML.load(s)
     end

    data.each do |key, rate|
      from, to = key.split(SERIALIZER_SEPARATOR)
      to, date = to.split(SERIALIZER_DATE_SEPARATOR)

      store.add_rate from, to, BigDecimal(rate, DECIMAL_PRECISION), date
    end
  end

  self
end

#ratesObject



111
112
113
114
115
116
117
# File 'lib/eu_central_bank.rb', line 111

def rates
  store.each_rate.each_with_object({}) do |(from,to,rate,date),hash|
    key = [from, to].join(SERIALIZER_SEPARATOR)
    key = [key, date.to_s].join(SERIALIZER_DATE_SEPARATOR) if date
    hash[key] = rate
  end
end

#save_historical_rates(cache, all = false) ⇒ Object



49
50
51
52
# File 'lib/eu_central_bank.rb', line 49

def save_historical_rates(cache, all=false)
  url = all ? ECB_ALL_HIST_URL : ECB_90_DAY_URL
  save_rates(cache, url)
end

#save_rates(cache, url = ECB_RATES_URL) ⇒ Object

Raises:



41
42
43
44
45
46
47
# File 'lib/eu_central_bank.rb', line 41

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

#save_rates_to_s(url = ECB_RATES_URL) ⇒ Object



58
59
60
# File 'lib/eu_central_bank.rb', line 58

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

#set_rate(from, to, rate, date = nil) ⇒ Object



103
104
105
106
107
108
109
# File 'lib/eu_central_bank.rb', line 103

def set_rate(from, to, rate, date = nil)
  if date.is_a?(Hash)
    # Backwards compatibility for the opts hash
    date = date[:date]
  end
  store.add_rate(::Money::Currency.wrap(from).iso_code, ::Money::Currency.wrap(to).iso_code, rate, date)
end

#update_historical_rates(cache = nil, all = false) ⇒ Object



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

def update_historical_rates(cache=nil, all=false)
  url = all ? ECB_ALL_HIST_URL : ECB_90_DAY_URL
  update_parsed_historical_rates(doc(cache, url))
end

#update_rates(cache = nil, url = ECB_RATES_URL) ⇒ Object



32
33
34
# File 'lib/eu_central_bank.rb', line 32

def update_rates(cache=nil, url=ECB_RATES_URL)
  update_parsed_rates(doc(cache, url))
end

#update_rates_from_s(content) ⇒ Object



54
55
56
# File 'lib/eu_central_bank.rb', line 54

def update_rates_from_s(content)
  update_parsed_rates(parse_rates(content))
end