Class: Nordea::Account

Inherits:
Resource show all
Defined in:
lib/nordea/resources/account.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Resource

#request_xml

Constructor Details

#initialize(fields = {}, command_params = {}, session = nil) {|_self| ... } ⇒ Account

Returns a new instance of Account.

Yields:

  • (_self)

Yield Parameters:



3
4
5
6
7
8
# File 'lib/nordea/resources/account.rb', line 3

def initialize(fields = {}, command_params = {}, session = nil)
  @name, @balance, @currency, @index = fields[:name], fields[:balance], fields[:currency], fields[:index]
  @command_params = command_params
  @session = session if session
  yield self if block_given?
end

Instance Attribute Details

#balanceObject

Returns the value of attribute balance.



10
11
12
# File 'lib/nordea/resources/account.rb', line 10

def balance
  @balance
end

#currencyObject

Returns the value of attribute currency.



10
11
12
# File 'lib/nordea/resources/account.rb', line 10

def currency
  @currency
end

#indexObject

Returns the value of attribute index.



10
11
12
# File 'lib/nordea/resources/account.rb', line 10

def index
  @index
end

#nameObject

Returns the value of attribute name.



10
11
12
# File 'lib/nordea/resources/account.rb', line 10

def name
  @name
end

#sessionObject

Returns the value of attribute session.



10
11
12
# File 'lib/nordea/resources/account.rb', line 10

def session
  @session
end

Class Method Details

.new_from_xml(xml_node, session) ⇒ Object



97
98
99
# File 'lib/nordea/resources/account.rb', line 97

def self.new_from_xml(xml_node, session)
  new({}, {}, session) { |acc| acc.reload_from_xml(xml_node) }
end

Instance Method Details

#account_type_nameObject



12
# File 'lib/nordea/resources/account.rb', line 12

def ; 'konton' end

#balance_to_s(decimals = 2) ⇒ Object



41
42
43
# File 'lib/nordea/resources/account.rb', line 41

def balance_to_s(decimals = 2)
  %Q{%.#{decimals}f} % balance
end

#deposit(amount, currency = 'SEK', options = {}) ⇒ Object

Raises:

  • (ArgumentError)


88
89
90
91
92
93
94
95
# File 'lib/nordea/resources/account.rb', line 88

def deposit(amount, currency = 'SEK', options = {})
  options.symbolize_keys!
  from = options.delete(:withdraw_from)
  
  raise ArgumentError unless from.is_a?(Nordea::Account)
  
  from.withdraw(amount, currency, options.merge(:deposit_to => self))
end

#reloadObject



45
46
47
48
# File 'lib/nordea/resources/account.rb', line 45

def reload
  @transactions = nil
  session.accounts(true)
end

#reload_from_xml(xml_node) ⇒ Object



50
51
52
53
54
# File 'lib/nordea/resources/account.rb', line 50

def reload_from_xml(xml_node)
  xml_node = Hpricot(xml_node) unless xml_node.class.to_s =~ /^Hpricot::/
  xml_node = xml_node.at("anchor") unless xml_node.name == 'anchor'
  initialize(self.class._setup_fields(xml_node), self.class._setup_command_params(xml_node))
end

#to_command_paramsObject



14
15
16
# File 'lib/nordea/resources/account.rb', line 14

def to_command_params
  @command_params
end

#to_sObject



37
38
39
# File 'lib/nordea/resources/account.rb', line 37

def to_s
  "#{name}: #{balance_to_s} #{currency}"
end

#transactions(reload = false) ⇒ Object



18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
# File 'lib/nordea/resources/account.rb', line 18

def transactions(reload = false)
  @transactions = nil if reload
  @transactions ||= begin
    # for some reason it needs this otherwise it would use the old transaction list
    session.request(Nordea::Commands::RESOURCE)

    nodes = session.request(Commands::TRANSACTIONS, to_command_params).parse_xml.search('go[@href="#trans"]')
    idx = nodes.length
    
    nodes.inject([]) do |all, node|
      trans = Transaction.new_from_xml(node, self)
      # since transactions don't have a time (just date) but are listed in chronological order
      # we add one second to each item in the collection so they can be sorted by date
      trans.date = Time.parse(trans.date.to_s) + (idx -= 1)
      all << trans
    end
  end
end

#withdraw(amount, currency = 'SEK', options = {}) ⇒ Object

Raises:

  • (ArgumentError)


56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
# File 'lib/nordea/resources/account.rb', line 56

def withdraw(amount, currency = 'SEK', options = {})
  options.symbolize_keys!
  to = options[:deposit_to]

  raise ArgumentError, "options[:deposit_to] must be a Nordea::Account" unless to.is_a?(Nordea::Account)

  currency, amount  = currency.to_s, amount.to_s.sub(".", ",")
   = [index, currency, name].join(":")
     = [to.index, to.currency, to.name].join(":")

  params = { :from_account_info => ,
             :to_account_info   => ,
             :amount            => amount,
             :currency_code     => currency }
  
  session.request(Nordea::Commands::TRANSFER_TO_OWN_ACCOUNT_PHASE_1, params)
  session.request(Nordea::Commands::TRANSFER_TO_OWN_ACCOUNT_PHASE_2, params)
  session.request(Nordea::Commands::TRANSFER_TO_OWN_ACCOUNT_PHASE_3, {
    :currency_code             => currency,
    :from_account_number       => index,
    :from_account_name         => name,
    :to_account_number         => to.index,
    :to_account_name           => to.name,
    :amount                    => amount,
    :exchange_rate             => "0",
    :from_currency_code        => currency,
    :to_currency_code          => currency
  })

  reload
end