Class: BankValidator::Iban

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

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(iban) ⇒ Iban

Returns a new instance of Iban.



5
6
7
# File 'lib/bank_validator/iban.rb', line 5

def initialize(iban)
  @value = iban
end

Instance Attribute Details

#valueObject

Returns the value of attribute value.



3
4
5
# File 'lib/bank_validator/iban.rb', line 3

def value
  @value
end

Class Method Details

.valid_checksum?(iban) ⇒ Boolean

Returns:

  • (Boolean)


18
19
20
21
22
23
24
25
26
27
# File 'lib/bank_validator/iban.rb', line 18

def valid_checksum?(iban)
  # Move first four characters to end of string
  dummy_iban = iban[4..-1] + iban[0..3]

  # Substitute all letters with integers
  checksum = dummy_iban.chars.map { |char| ((char =~ /[a-zA-Z]/).present?) ? (char.downcase.ord - 87).to_s : char }.join

  # Check if division by 97 yields a remainder of 1, in which case it could be a valid IBAN
  (checksum.to_i % 97) == 1
end

.valid_format?(iban) ⇒ Boolean

Returns:

  • (Boolean)


14
15
16
# File 'lib/bank_validator/iban.rb', line 14

def valid_format?(iban)
  iban =~ /[A-Z]{2}[a-zA-Z0-9]{14,}/ ? true : false
end

Instance Method Details

#valid?Boolean

Returns:

  • (Boolean)


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

def valid?
  BankValidator::Iban.valid_format?(value) && BankValidator::Iban.valid_checksum?(value)
end