Class: EuCentralBank
- Inherits:
-
Money::Bank::VariableExchange
- Object
- Money::Bank::VariableExchange
- EuCentralBank
- Defined in:
- lib/eu_central_bank.rb
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
Instance Attribute Summary collapse
-
#historical_last_updated ⇒ Object
Returns the value of attribute historical_last_updated.
-
#historical_rates_updated_at ⇒ Object
Returns the value of attribute historical_rates_updated_at.
-
#last_updated ⇒ Object
Returns the value of attribute last_updated.
-
#rates_updated_at ⇒ Object
Returns the value of attribute rates_updated_at.
Instance Method Summary collapse
- #check_currency_available(currency) ⇒ Object
- #exchange(cents, from_currency, to_currency, date = nil) ⇒ Object
- #exchange_with(from, to_currency, date = nil) ⇒ Object
- #export_rates(format, file = nil, opts = {}) ⇒ Object
- #get_rate(from, to, date = nil) ⇒ Object
- #import_rates(format, s, opts = {}) ⇒ Object
-
#initialize(st = Money::RatesStore::StoreWithHistoricalDataSupport.new, &block) ⇒ EuCentralBank
constructor
A new instance of EuCentralBank.
- #rates ⇒ Object
- #save_rates(cache, url = ECB_RATES_URL) ⇒ Object
- #save_rates_to_s(url = ECB_RATES_URL) ⇒ Object
- #set_rate(from, to, rate, date = nil) ⇒ Object
- #update_historical_rates(cache = nil) ⇒ Object
- #update_rates(cache = nil, url = ECB_RATES_URL) ⇒ Object
- #update_rates_from_s(content) ⇒ Object
Constructor Details
#initialize(st = Money::RatesStore::StoreWithHistoricalDataSupport.new, &block) ⇒ EuCentralBank
Returns a new instance of EuCentralBank.
23 24 25 26 |
# File 'lib/eu_central_bank.rb', line 23 def initialize(st = Money::RatesStore::StoreWithHistoricalDataSupport.new, &block) super @currency_string = nil end |
Instance Attribute Details
#historical_last_updated ⇒ Object
Returns the value of attribute historical_last_updated.
14 15 16 |
# File 'lib/eu_central_bank.rb', line 14 def historical_last_updated @historical_last_updated end |
#historical_rates_updated_at ⇒ Object
Returns the value of attribute historical_rates_updated_at.
15 16 17 |
# File 'lib/eu_central_bank.rb', line 15 def historical_rates_updated_at @historical_rates_updated_at end |
#last_updated ⇒ Object
Returns the value of attribute last_updated.
12 13 14 |
# File 'lib/eu_central_bank.rb', line 12 def last_updated @last_updated end |
#rates_updated_at ⇒ Object
Returns the value of attribute rates_updated_at.
13 14 15 |
# File 'lib/eu_central_bank.rb', line 13 def rates_updated_at @rates_updated_at end |
Instance Method Details
#check_currency_available(currency) ⇒ Object
156 157 158 159 160 161 |
# File 'lib/eu_central_bank.rb', line 156 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
52 53 54 |
# File 'lib/eu_central_bank.rb', line 52 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
56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 |
# File 'lib/eu_central_bank.rb', line 56 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 = "No conversion rate known for '#{from.currency.iso_code}' -> '#{to_currency}'" << " on #{date.to_s}" if date raise Money::Bank::UnknownRate, end rate = to_base_rate / from_base_rate end calculate_exchange(from, to_currency, rate) end |
#export_rates(format, file = nil, opts = {}) ⇒ Object
109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 |
# File 'lib/eu_central_bank.rb', line 109 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
79 80 81 82 83 84 85 86 87 88 89 90 91 |
# File 'lib/eu_central_bank.rb', line 79 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
131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 |
# File 'lib/eu_central_bank.rb', line 131 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 |
#rates ⇒ Object
101 102 103 104 105 106 107 |
# File 'lib/eu_central_bank.rb', line 101 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_rates(cache, url = ECB_RATES_URL) ⇒ Object
36 37 38 39 40 41 42 |
# File 'lib/eu_central_bank.rb', line 36 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
48 49 50 |
# File 'lib/eu_central_bank.rb', line 48 def save_rates_to_s(url=ECB_RATES_URL) open_url(url).read end |
#set_rate(from, to, rate, date = nil) ⇒ Object
93 94 95 96 97 98 99 |
# File 'lib/eu_central_bank.rb', line 93 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) ⇒ Object
32 33 34 |
# File 'lib/eu_central_bank.rb', line 32 def update_historical_rates(cache=nil) update_parsed_historical_rates(doc(cache, ECB_90_DAY_URL)) end |
#update_rates(cache = nil, url = ECB_RATES_URL) ⇒ Object
28 29 30 |
# File 'lib/eu_central_bank.rb', line 28 def update_rates(cache=nil, url=ECB_RATES_URL) update_parsed_rates(doc(cache, url)) end |
#update_rates_from_s(content) ⇒ Object
44 45 46 |
# File 'lib/eu_central_bank.rb', line 44 def update_rates_from_s(content) update_parsed_rates(doc_from_s(content)) end |