Class: Bank::IBAN

Inherits:
Object
  • Object
show all
Extended by:
Forwardable
Defined in:
lib/bank/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.



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

def initialize(code)
  @code = code.to_s.gsub(/\s+/, '').upcase
end

Class Method Details

.valid?(code) ⇒ Boolean

Returns:

  • (Boolean)


10
11
12
# File 'lib/bank/iban.rb', line 10

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

Instance Method Details

#bbanObject



26
27
28
# File 'lib/bank/iban.rb', line 26

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

#check_digitsObject



22
23
24
# File 'lib/bank/iban.rb', line 22

def check_digits
  @code[2..3]
end

#country_codeObject



18
19
20
# File 'lib/bank/iban.rb', line 18

def country_code
  @code[0..1]
end

#to_iObject



38
39
40
# File 'lib/bank/iban.rb', line 38

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



42
43
44
# File 'lib/bank/iban.rb', line 42

def to_s(formatted=false)
  formatted ? @code.gsub(/(.{4})/, '\1 ').strip : @code
end

#valid?Boolean

Returns:

  • (Boolean)


30
31
32
# File 'lib/bank/iban.rb', line 30

def valid?
  valid_check_digits? && bban.valid?
end

#valid_check_digits?Boolean

Returns:

  • (Boolean)


34
35
36
# File 'lib/bank/iban.rb', line 34

def valid_check_digits?
  to_i % 97 == 1
end