Class: Nordea::Session

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(pnr, pin, &block) ⇒ Session

Returns a new instance of Session.



6
7
8
9
10
11
12
13
14
# File 'lib/nordea/session.rb', line 6

def initialize(pnr, pin, &block)
  @pnr, @pin = pnr, pin
  @num_requests = 0
  if block_given?
   
   yield self
   logout
  end
end

Instance Attribute Details

#num_requestsObject (readonly)

Returns the value of attribute num_requests.



4
5
6
# File 'lib/nordea/session.rb', line 4

def num_requests
  @num_requests
end

#pinObject

Returns the value of attribute pin.



3
4
5
# File 'lib/nordea/session.rb', line 3

def pin
  @pin
end

#pnrObject

Returns the value of attribute pnr.



3
4
5
# File 'lib/nordea/session.rb', line 3

def pnr
  @pnr
end

#tokenObject

Returns the value of attribute token.



3
4
5
# File 'lib/nordea/session.rb', line 3

def token
  @token
end

Instance Method Details

#accounts(reload = false) ⇒ Object



47
48
49
50
51
52
53
54
# File 'lib/nordea/session.rb', line 47

def accounts(reload = false)
  if reload && @accounts
    reloaded_xml_nodes = reload_resources(Account)
    @accounts.each_with_index { |, idx| .reload_from_xml(reloaded_xml_nodes[idx]) }
  else
    @accounts ||= setup_resources(Account)
  end
end

#loginObject Also known as: login!



16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
# File 'lib/nordea/session.rb', line 16

def 
  doc = request(Commands::LOGIN_PHASE_1, { "kundnr" => pnr, "pinkod" => pin, "sid" => "0" }).parse_xml
  begin 
    @token = (doc/'*[@name=sid]').attr("value")
    contract = (doc/'postfield[@name=contract]').attr("value")
  rescue Exception => e
    if node = doc.at("card[@title='Fel']")
      error_message = node.at("p").inner_text.strip
    else
      error_message = ""
    end
    raise Nordea::InvalidLogin, error_message
  end
  request Commands::LOGIN_PHASE_2, "bank" => "mobile_light", "no_prev" => "1", "contract" => contract
end

#logoutObject Also known as: logout!



34
35
36
# File 'lib/nordea/session.rb', line 34

def logout
  request Commands::LOGOUT
end

#request(command, extra_params = {}) ⇒ Object



40
41
42
43
44
45
# File 'lib/nordea/session.rb', line 40

def request(command, extra_params = {})
  extra_params.symbolize_keys!
  extra_params.merge!(:sid => token) unless extra_params.has_key?(:sid)
  @num_requests += 1
  Nordea::Request.new(command, extra_params)
end