Class: AqBanking::Commander

Inherits:
Object
  • Object
show all
Defined in:
lib/aq_banking/commander.rb

Defined Under Namespace

Classes: Result, SystemCmdError

Constant Summary collapse

DEFAULT_TIMEOUT =
120

Instance Method Summary collapse

Instance Method Details

#account_valid?(bank_code:, account_number:) ⇒ Boolean

Returns:

  • (Boolean)


72
73
74
75
# File 'lib/aq_banking/commander.rb', line 72

def (bank_code:, account_number:)
  result = run "ktoblzcheck #{bank_code} #{}"
  result.success?
end

#add_user(server_url:, hbci_version:, bank_code:, user_id:, user_name: '') ⇒ Object



107
108
109
110
111
112
113
114
115
116
117
118
# File 'lib/aq_banking/commander.rb', line 107

def add_user(server_url:, hbci_version:, bank_code:, user_id:, user_name: '')
  logger.debug 'adding account %p' % {server_url: server_url, hbci_version: hbci_version, bank_code: bank_code, user_id: user_id, user_name: user_name}

  cmd = ""
  cmd << "#{aq_hbci} adduser -t pintan --context=1"
  cmd << " -b #{bank_code} -u #{user_id}"
  cmd << %Q( -N "#{user_name}")
  cmd << %Q( --hbciversion=#{hbci_version})
  cmd << %Q( -s "#{server_url}")

  run_or_raise(cmd, 'could not add account').success?
end

#call_getsysid(pin_file:, user_id:, bank_code:) ⇒ Object



120
121
122
123
124
125
126
# File 'lib/aq_banking/commander.rb', line 120

def call_getsysid(pin_file:, user_id:, bank_code:)
  logger.debug 'calling getsysid with %p' % {pin_file: pin_file, user_id: user_id, bank_code: bank_code}

  cmd = "#{aq_hbci} -P #{pin_file} getsysid -u #{user_id} -b #{bank_code}"

  run_or_raise(cmd, 'Could not get sysid. This might be because the bank_code, userId or password is wrong, or internet banking is not active. Check if you can log into your internet banking w/ these credentials.')
end

#remove_user(bank_code:, user_id:) ⇒ Boolean

removes user at bank with bank-code from aqhbci-tool.

Parameters:

  • bank_code (String)
  • user_id (String)

Returns:

  • (Boolean)


90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
# File 'lib/aq_banking/commander.rb', line 90

def remove_user(bank_code:, user_id:)
  logger.debug 'removing user %p' % {bank_code: bank_code, user_id: user_id}

  # we must do this due to frickin aqhbci: the option --with-acccounts removes one account and if there are more
  # an error is raised. on the other hand, aqhbci-tool4 listaccounts only shows bank_code and account number, but not
  # user id. Refactorings to improve this are very welcome
  count = 0
  max_trials = 10
  loop do
    count = count + 1
    result = run "aqhbci-tool4 deluser --with-accounts -u %p -b %p" % [user_id, bank_code]
    break if result.success? or result.stderr.include? 'ERROR: No matching users'

    raise 'could not delete user' if count >= max_trials
  end
end

#send_request(bank_code, account_number, pin_file, from: nil, to: nil) ⇒ String

Parameters:

  • bank_code (String)
  • account_number (String)
  • pin_file (String)

    path to pin

  • from (Date, String) (defaults to: nil)
  • to (Date, String) (defaults to: nil)

Returns:

  • (String)

Raises:



21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
# File 'lib/aq_banking/commander.rb', line 21

def send_request(bank_code, , pin_file, from: nil, to: nil)
  logger.debug 'sending request %p' % {bank_code: bank_code, account_number: , pin_file: pin_file, from: from, to: to}

  from = Date.parse(from) if from.is_a? String
  to = Date.parse(to) if to.is_a? String

  cmd = "#{aq_cli} -P #{pin_file} request -b #{bank_code} -a #{}"
  cmd << " --fromdate=#{from.strftime("%Y%m%d")}" if from
  cmd << " --todate=#{to.strftime("%Y%m%d")}" if to
  cmd << " --transactions --balance"

  result = run_or_raise cmd, 'request failed'

  result.stdout
end

#send_request!(server_url:, hbci_version: '220', bank_code:, user_id:, password:, account_number:, user_name: '', from: nil, to: nil) ⇒ String

sends a request, but creates the account and fetches the sysid if neccessary

Returns:

  • (String)

Raises:



50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
# File 'lib/aq_banking/commander.rb', line 50

def send_request!(server_url:, hbci_version: '220', bank_code:, user_id:, password:, account_number:, user_name: '', from: nil, to: nil)
  raise ArgumentError.new("unknown hbci_version: \"#{hbci_version}\"") unless HBCI_VERSIONS.include? hbci_version
  raise ArgumentError.new("server url must be present") unless server_url.present?

  logger.debug 'calling Commander#with_pin with args %p %p %p' % [bank_code, user_id, password]
  with_pin bank_code, user_id, password do |pin_file_path|
    # ensure that no old password is in cache
    remove_user bank_code: bank_code, user_id: user_id

    add_user(server_url: server_url, hbci_version: hbci_version,
             bank_code: bank_code, user_id: user_id, user_name: user_name)

    call_getsysid pin_file: pin_file_path, user_id: user_id, bank_code: bank_code

    send_request(bank_code, , pin_file_path, from: from, to: to)
  end
ensure
  # clean up after ourselves
  remove_user bank_code: bank_code, user_id: user_id
end

#user_exists?(bank_code:, user_id:) ⇒ Boolean

Parameters:

  • bank_code (String)
  • user_id (String)

Returns:

  • (Boolean)


80
81
82
83
# File 'lib/aq_banking/commander.rb', line 80

def user_exists?(bank_code:, user_id:)
  result = run "aqhbci-tool4 listusers"
  result.stdout.include?("Bank: de/#{bank_code} User Id: #{user_id}")
end

#with_pin(bank_code, user_id, password, &block) ⇒ Object



37
38
39
40
41
42
43
44
# File 'lib/aq_banking/commander.rb', line 37

def with_pin bank_code, user_id, password, &block
  pin_file = build_pin_file bank_code: bank_code, user_id: user_id, password: password

  logger.debug 'calling with_pin block with file: %p' % [pin_file]
  block.call(pin_file.path) if block
ensure
  pin_file.close!
end