Class: Nordea::ExchangeRates

Inherits:
Object
  • Object
show all
Defined in:
lib/nordea/exchange_rates.rb

Overview

Fetch and update Excahnge rates from Nordea Bank.

Parses the custom dataformat used by Nordea into more useful Ruby types and objects.

Constant Summary collapse

DATA_URI =

URI to the machine readable data file.

URI::HTTP.build({
  :host => "openpages.nordea.com",
  :path => "/fi/lists/currency/elelctronicExchangeFI.dat"
})
FLOAT_KEYS =

Hash keys for key-value pairs that should be converted into floats.

[
  :middle_rate_for_commercial_transactions,
  :buying_rate_for_commercial_transactions,
  :selling_rate_for_commercial_transactions,
  :buying_rate_for_cash,
  :selling_rate_for_cash
]

Instance Method Summary collapse

Constructor Details

#initializeExchangeRates

Initialize a new Nordea::ExchangeRates object.



29
30
# File 'lib/nordea/exchange_rates.rb', line 29

def initialize
end

Instance Method Details

#currencies(force = false) ⇒ Hash

List all currencies as a hash. Uses the currencies’ ISO codes as keys.

Examples:

e = Nordea::ExchangeRates.new #=> #<Nordea::ExchangeRates:0x00000102102888>
e.currencies["USD"] #=> {
#                                      :file_id => "VK01",
#                                    :record_id => "001",
#                               :quotation_time => 2011-06-17 15:50:01 +0300,
#                                    :rate_type => :list,
#                            :currency_iso_code => "USD",
#                    :counter_currency_iso_code => "EUR",
#      :middle_rate_for_commercial_transactions => 1.427,
#      :buying_rate_for_commercial_transactions => 1.442,
#     :selling_rate_for_commercial_transactions => 1.412,
#                         :buying_rate_for_cash => 1.459,
#                        :selling_rate_for_cash => 1.395,
#                          :direction_of_change => "-",
#                      :currency_convertability => true,
#                                    :euro_area => false,
#                           :euro_adoption_date => nil,
#                              :currency_expiry => false,
#                                     :reserved => ""
# }

Parameters:

  • force (Boolean) (defaults to: false)

    force update

Returns:

  • (Hash)

    a hash off all list currency rates and their properties



85
86
87
88
89
90
91
92
93
# File 'lib/nordea/exchange_rates.rb', line 85

def currencies(force = false)
  hash = {}

  records_array(force).each do |record|
    hash[record[:currency_iso_code]] = record
  end

  hash
end

#headers(force = false) ⇒ Hash

The batch headers

Parameters:

  • force (Boolean) (defaults to: false)

    force update

Returns:

  • (Hash)

    key-value pairs



36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
# File 'lib/nordea/exchange_rates.rb', line 36

def headers(force = false)
  header_line = lines(force).first

  unpacked = header_line.unpack("A4A3A14A120A9")

  headers = {
    :file_id       => unpacked[0], # File ID   = VK01
    :record_id     => unpacked[1], # Record ID = 000
    :change_time   => unpacked[2], # Exchange rate change time
    :notifications => unpacked[3], # Notifications
    :reserved      => unpacked[4]  # Reserved
  }

  headers.each do |key, value|
    headers[key] = value.force_encoding("ascii") if value.respond_to?(:force_encoding)
  end

  headers[:change_time] = Nordea.parse_time(headers[:change_time])
  headers[:notifications].strip! if headers[:notifications].respond_to? :strip!
  headers[:reserved].strip! if headers[:reserved].respond_to? :strip!
  headers
end