Class: BankAccountTools::IBAN

Inherits:
Object
  • Object
show all
Extended by:
Forwardable
Defined in:
lib/bank_account_tools/iban.rb

Overview

International Bank Account Number

Class Method Summary collapse

Instance Method Summary collapse

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

Returns:

  • (Boolean)


11
12
13
# File 'lib/bank_account_tools/iban.rb', line 11

def self.valid?(code)
  new(code).valid?
end

Instance Method Details

#bbanObject



27
28
29
# File 'lib/bank_account_tools/iban.rb', line 27

def bban
  @bban ||= BBAN.new(country_code, @code[4..-1])
end

#check_digitsObject



23
24
25
# File 'lib/bank_account_tools/iban.rb', line 23

def check_digits
  @code[2..3]
end

#country_codeObject



19
20
21
# File 'lib/bank_account_tools/iban.rb', line 19

def country_code
  @code[0..1]
end

#to_iObject



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

Returns:

  • (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

Returns:

  • (Boolean)


35
36
37
# File 'lib/bank_account_tools/iban.rb', line 35

def valid_check_digits?
  to_i % 97 == 1
end