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) ⇒ Account

Returns a new instance of Account.



3
4
5
6
7
# 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
end

Instance Attribute Details

#balanceObject

Returns the value of attribute balance.



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

def balance
  @balance
end

#currencyObject

Returns the value of attribute currency.



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

def currency
  @currency
end

#indexObject

Returns the value of attribute index.



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

def index
  @index
end

#nameObject

Returns the value of attribute name.



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

def name
  @name
end

#sessionObject

Returns the value of attribute session.



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

def session
  @session
end

Class Method Details

.new_from_xml(xml_node, session) ⇒ Object



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

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

Instance Method Details

#account_type_nameObject



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

def ; 'konton' end

#balance_to_s(decimals = 2) ⇒ Object



32
33
34
# File 'lib/nordea/resources/account.rb', line 32

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

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

Raises:

  • (ArgumentError)


74
75
76
77
78
79
80
81
# File 'lib/nordea/resources/account.rb', line 74

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

#to_command_paramsObject



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

def to_command_params
  @command_params
end

#to_sObject



28
29
30
# File 'lib/nordea/resources/account.rb', line 28

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

#transactions(reload = false) ⇒ Object

TODO move shared transaction fetching to Nordea::Resource and call super



18
19
20
21
22
23
24
25
26
# File 'lib/nordea/resources/account.rb', line 18

def transactions(reload = false)
  @transactions = nil if reload
  @transactions ||= begin
    doc = session.request(Commands::TRANSACTIONS, to_command_params).parse_xml
    doc.search('go[@href="#trans"]').inject([]) do |all, node|
      all << Transaction.new_from_xml(node)
    end
  end
end

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

Raises:

  • (ArgumentError)


42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
# File 'lib/nordea/resources/account.rb', line 42

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
  })

  session.accounts(true)
end