Class: BankTools::Global::IBAN

Inherits:
Object
  • Object
show all
Defined in:
lib/banktools/global/iban.rb

Constant Summary collapse

E =
BankTools::Global::Errors

Instance Method Summary collapse

Constructor Details

#initialize(raw) ⇒ IBAN

Returns a new instance of IBAN.



9
10
11
# File 'lib/banktools/global/iban.rb', line 9

def initialize(raw)
  @pre_normalized = raw.to_s.gsub(/\s/, "").upcase
end

Instance Method Details

#errorsObject



17
18
19
20
21
22
23
24
25
26
27
28
29
30
# File 'lib/banktools/global/iban.rb', line 17

def errors
  rule = rules[country_code]
  return [ E::UNKNOWN_COUNTRY ] unless rule

  length = rule.fetch("length")
  return [ E::WRONG_LENGTH ] unless @pre_normalized.length == length

  re = rule.fetch("bban_pattern")
  return [ E::BAD_FORMAT ] unless bban.match?(re)

  return [ E::BAD_CHECKSUM ] unless good_checksum?

  []
end

#normalizeObject



32
33
34
# File 'lib/banktools/global/iban.rb', line 32

def normalize
  @pre_normalized.gsub(/.{4}/, '\0 ').strip
end

#valid?Boolean

Returns:

  • (Boolean)


13
14
15
# File 'lib/banktools/global/iban.rb', line 13

def valid?
  errors.empty?
end