Class: BankValidator::Iban
- Inherits:
-
Object
- Object
- BankValidator::Iban
- Defined in:
- lib/bank_validator/iban.rb
Instance Attribute Summary collapse
-
#value ⇒ Object
Returns the value of attribute value.
Class Method Summary collapse
Instance Method Summary collapse
-
#initialize(iban) ⇒ Iban
constructor
A new instance of Iban.
- #valid? ⇒ Boolean
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
#value ⇒ Object
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
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
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
9 10 11 |
# File 'lib/bank_validator/iban.rb', line 9 def valid? BankValidator::Iban.valid_format?(value) && BankValidator::Iban.valid_checksum?(value) end |