Class: BankAccountTools::IBAN
- Inherits:
-
Object
- Object
- BankAccountTools::IBAN
- Extended by:
- Forwardable
- Defined in:
- lib/bank_account_tools/iban.rb
Overview
International Bank Account Number
Class Method Summary collapse
Instance Method Summary collapse
- #bban ⇒ Object
- #check_digits ⇒ Object
- #country_code ⇒ Object
-
#initialize(code) ⇒ IBAN
constructor
A new instance of IBAN.
- #to_i ⇒ Object
- #to_s(formatted = false) ⇒ Object
- #valid? ⇒ Boolean
- #valid_check_digits? ⇒ Boolean
Constructor Details
#initialize(code) ⇒ IBAN
Returns a new instance of IBAN.
15 16 17 |
# File 'lib/bank_account_tools/iban.rb', line 15 def initialize(code) @code = code.to_s.gsub(/\s+/, '').upcase end |
Class Method Details
.valid?(code) ⇒ Boolean
11 12 13 |
# File 'lib/bank_account_tools/iban.rb', line 11 def self.valid?(code) new(code).valid? end |
Instance Method Details
#bban ⇒ Object
27 28 29 |
# File 'lib/bank_account_tools/iban.rb', line 27 def bban @bban ||= BBAN.new(country_code, @code[4..-1]) end |
#check_digits ⇒ Object
23 24 25 |
# File 'lib/bank_account_tools/iban.rb', line 23 def check_digits @code[2..3] end |
#country_code ⇒ Object
19 20 21 |
# File 'lib/bank_account_tools/iban.rb', line 19 def country_code @code[0..1] end |
#to_i ⇒ Object
39 40 41 |
# File 'lib/bank_account_tools/iban.rb', line 39 def to_i "#{bban}#{country_code}#{check_digits}".each_char.map { |chr| chr.to_i(36) }.join.to_i end |
#to_s(formatted = false) ⇒ Object
43 44 45 |
# File 'lib/bank_account_tools/iban.rb', line 43 def to_s(formatted=false) formatted ? @code.gsub(/(.{4})/, '\1 ').strip : @code end |
#valid? ⇒ Boolean
31 32 33 |
# File 'lib/bank_account_tools/iban.rb', line 31 def valid? valid_check_digits? && bban.valid? end |
#valid_check_digits? ⇒ Boolean
35 36 37 |
# File 'lib/bank_account_tools/iban.rb', line 35 def valid_check_digits? to_i % 97 == 1 end |