Class: NbrbCurrency
- Inherits:
-
Money::Bank::VariableExchange
- Object
- Money::Bank::VariableExchange
- NbrbCurrency
- Defined in:
- lib/nbrb_currency.rb
Constant Summary collapse
- NBRB_RATES_URL =
'http://nbrb.by/Services/XmlExRates.aspx'
- CURRENCIES =
%w(AUD BGN UAH DKK USD EUR PLN JPY IRR ISK CAD CNY KWD LVL LTL MDL NOK RUB SGD KGS KZT TRY GBP CZK SEK CHF)
Instance Method Summary collapse
- #exchange(cents, from_currency, to_currency) ⇒ Object
- #exchange_with(from, to_currency) ⇒ Object
- #save_rates(cache) ⇒ Object
- #update_rates(cache = nil) ⇒ Object
Instance Method Details
#exchange(cents, from_currency, to_currency) ⇒ Object
32 33 34 |
# File 'lib/nbrb_currency.rb', line 32 def exchange(cents, from_currency, to_currency) exchange_with(Money.new(cents, from_currency), to_currency) end |
#exchange_with(from, to_currency) ⇒ Object
36 37 38 39 40 41 42 43 44 45 |
# File 'lib/nbrb_currency.rb', line 36 def exchange_with(from, to_currency) rate = get_rate(from.currency, to_currency) unless rate from_base_rate = get_rate(from.currency, "BYR") to_base_rate = get_rate(to_currency, "BYR") rate = (BigDecimal.new(from_base_rate, 8) / BigDecimal.new(to_base_rate, 8)).to_f raise "Rate #{from.currency} - #{to_currency} unknown!" unless rate end Money.new(((Money::Currency.wrap(to_currency).subunit_to_unit.to_f / from.currency.subunit_to_unit.to_f) * from.cents * rate).round, to_currency) end |
#save_rates(cache) ⇒ Object
24 25 26 27 28 29 30 |
# File 'lib/nbrb_currency.rb', line 24 def save_rates(cache) raise InvalidCache if !cache File.open(cache, "w") do |file| io = open(NBRB_RATES_URL) ; io.each_line {|line| file.puts line} end end |
#update_rates(cache = nil) ⇒ Object
13 14 15 16 17 18 19 20 21 22 |
# File 'lib/nbrb_currency.rb', line 13 def update_rates(cache=nil) exchange_rates(cache).each do |exchange_rate| rate = exchange_rate.xpath("Rate").text currency = exchange_rate.xpath("CharCode").text scale = exchange_rate.xpath("Scale").text next if currency == "XDR" add_rate(currency, "BYR", (BigDecimal.new(rate) / BigDecimal.new(scale)).to_f) end add_rate("BYR", "BYR", 1) end |