Class: Ibandit::Sweden::Validator

Inherits:
Object
  • Object
show all
Defined in:
lib/ibandit/sweden/validator.rb

Class Method Summary collapse

Class Method Details

.account_number_length_valid_for_bank_code?(bank_code: nil, account_number: nil) ⇒ Boolean

Returns:

  • (Boolean)


54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
# File 'lib/ibandit/sweden/validator.rb', line 54

def self.(bank_code: nil,
                                                    account_number: nil)
  bank_code_possible = (
    bank_code: bank_code,
    account_number: ,
  )
  return unless bank_code_possible

  Sweden::BankLookup.for_bank_code(bank_code).any? do |bank|
    length = bank.fetch(:serial_number_length)
    length += bank[:clearing_code_length] if bank[:include_clearing_code]

     = .gsub(/\A0+/, "")
    if bank[:zerofill_serial_number] && !bank[:include_clearing_code]
       =
        .
          rjust(bank.fetch(:serial_number_length), "0")
    end

    .length == length
  end
end

.bank_code_exists?(bank_code) ⇒ Boolean

SWIFT detail validators #

Returns:

  • (Boolean)


40
41
42
# File 'lib/ibandit/sweden/validator.rb', line 40

def self.bank_code_exists?(bank_code)
  Sweden::BankLookup.for_bank_code(bank_code).any?
end

.bank_code_exists_for_clearing_code?(clearing_code) ⇒ Boolean

Local detail validators #

Returns:

  • (Boolean)


10
11
12
# File 'lib/ibandit/sweden/validator.rb', line 10

def self.bank_code_exists_for_clearing_code?(clearing_code)
  !Sweden::BankLookup.for_clearing_code(clearing_code).nil?
end

.bank_code_possible_for_account_number?(bank_code: nil, account_number: nil) ⇒ Boolean

Returns:

  • (Boolean)


44
45
46
47
48
49
50
51
52
# File 'lib/ibandit/sweden/validator.rb', line 44

def self.(bank_code: nil,
                                                account_number: nil)
  return unless bank_code_exists?(bank_code)

  clearing_code = .gsub(/\A0+/, "").slice(0, 4).to_i
  Sweden::BankLookup.for_bank_code(bank_code).any? do |bank|
    !bank[:include_clearing_code] || bank[:range].include?(clearing_code)
  end
end

.valid_clearing_code_length?(clearing_code) ⇒ Boolean

Returns:

  • (Boolean)


14
15
16
17
18
19
# File 'lib/ibandit/sweden/validator.rb', line 14

def self.valid_clearing_code_length?(clearing_code)
  return unless bank_code_exists_for_clearing_code?(clearing_code)

  bank_info = Sweden::BankLookup.for_clearing_code(clearing_code)
  bank_info.fetch(:clearing_code_length) == clearing_code.to_s.length
end

.valid_serial_number_length?(clearing_code: nil, serial_number: nil) ⇒ Boolean

Returns:

  • (Boolean)


21
22
23
24
25
26
27
28
29
30
31
32
33
34
# File 'lib/ibandit/sweden/validator.rb', line 21

def self.valid_serial_number_length?(clearing_code: nil,
                                     serial_number: nil)
  return false unless serial_number
  return unless bank_code_exists_for_clearing_code?(clearing_code)

  bank_info = Sweden::BankLookup.for_clearing_code(clearing_code)
  serial_number_length = bank_info.fetch(:serial_number_length)

  if bank_info.fetch(:zerofill_serial_number)
    serial_number = serial_number.rjust(serial_number_length, "0")
  end

  serial_number_length == serial_number.to_s.length
end