Class: Nordea::Bank

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

Overview

Bank implementation for use with the Money gem.

Examples:

nordea_bank = Nordea::Bank.new
Money.default_bank =  nordea_bank
nordea_bank.exchange(100, "EUR", "USD")
Money.us_dollar(100).exchange_to("ZAR")
nordea_bank.exchange_with(Money.new(100, "CAD"), "USD")

Instance Method Summary collapse

Constructor Details

#initializeBank

Returns a new instance of Bank.



14
15
16
17
# File 'lib/nordea/bank.rb', line 14

def initialize
  super
  update_rates
end

Instance Method Details

#currencies(force = false) ⇒ Hash

Get the currency data from Nordea

Parameters:

  • force (Boolean) (defaults to: false)

    force an update of the currency data

Returns:

  • (Hash)

    Data for all the currencies and rates from Nordea



77
78
79
# File 'lib/nordea/bank.rb', line 77

def currencies(force = false)
  exchange_rates.currencies(force)
end

#exchange(cents, from_currency = "EUR", to_currency = "EUR") ⇒ Money

Exchange from one currency to another

Examples:

nordea_bank = Nordea::Bank.new
nordea_bank.exchange(100, "EUR", "USD")

Parameters:

  • cents (Integer)

    the amount for the conversion in cents, or equivalent

  • from_currency (String) (defaults to: "EUR")

    the source currency

  • to_currency (String) (defaults to: "EUR")

    the target currency

Returns:

  • (Money)

    the result of the conversion



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

def exchange(cents, from_currency = "EUR", to_currency = "EUR")
  exchange_with(Money.new(cents, from_currency), to_currency)
end

#exchange_ratesExchangeRates

Initialize or use an existing Nordea::ExchangeRates object

Returns:



69
70
71
# File 'lib/nordea/bank.rb', line 69

def exchange_rates
  @exchange_rates ||= Nordea::ExchangeRates.new
end

#exchange_with(from, to_currency) ⇒ Money

Exchanges the given Money object to a new Money object in to_currency.

Examples:

nordea_bank = Nordea::Bank.new
nordea_bank.exchange_with(Money.new(100, "CAD"), "USD")

Parameters:

  • from (Money)

    the Money object from which to convert

  • to_currency (String)

    the ISO code for the target currency

Returns:

  • (Money)

    the new Money object in the target currency



56
57
58
59
60
61
62
63
64
# File 'lib/nordea/bank.rb', line 56

def exchange_with(from, to_currency)
  rate = get_rate(from.currency, to_currency)
  unless rate
    from_base_rate = get_rate("EUR", from.currency)
    to_base_rate = get_rate("EUR", to_currency)
    rate = to_base_rate / from_base_rate
  end
  Money.new((from.cents * rate).round, to_currency)
end

#known_currenciesArray<String>

List currencies found in the Money gem and the Nordea currency data

Returns:

  • (Array<String>)

    ISO currency codes



91
92
93
# File 'lib/nordea/bank.rb', line 91

def known_currencies
  @known_currencies = money_currencies & exchange_rates.currencies.keys
end

#money_currenciesArray<String>

List all currencies known to the money gem

Returns:

  • (Array<String>)

    ISO currency codes



84
85
86
# File 'lib/nordea/bank.rb', line 84

def money_currencies
  Money::Currency.table.keys.map { |c| c.to_s.upcase }
end

#update_ratesHash

Get updated rates from the Nordea server

Forces an update of the exchange rates

Returns:

  • (Hash)

    rates available



24
25
26
27
28
29
30
# File 'lib/nordea/bank.rb', line 24

def update_rates
  currencies(true).each_pair do |currency, data|
    rate = data[:middle_rate_for_commercial_transactions]
    add_rate("EUR", currency, rate) if known_currencies.include?(currency)
  end
  rates
end