Class: Prawn::SwissQRBill::IBAN

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

Overview

Check validity of IBAN.

NOTE: *For Switzerland only*

Simple implementation according to en.wikipedia.org/wiki/International_Bank_Account_Number#Algorithms

TODO: validate QR-iban

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(code) ⇒ IBAN

Returns a new instance of IBAN.



16
17
18
# File 'lib/prawn/swiss_qr_bill/iban.rb', line 16

def initialize(code)
  @code = standardize(code)
end

Instance Attribute Details

#codeObject (readonly)

Returns the value of attribute code.



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

def code
  @code
end

Instance Method Details

#bbanObject



28
29
30
# File 'lib/prawn/swiss_qr_bill/iban.rb', line 28

def bban
  @code[4..-1]
end

#check_digitsObject



24
25
26
# File 'lib/prawn/swiss_qr_bill/iban.rb', line 24

def check_digits
  @code[2..3]
end

#country_codeObject



20
21
22
# File 'lib/prawn/swiss_qr_bill/iban.rb', line 20

def country_code
  @code[0..1]
end

#prettifyObject



40
41
42
# File 'lib/prawn/swiss_qr_bill/iban.rb', line 40

def prettify
  @code.gsub(/(.{4})/, '\1 ').strip
end

#to_iObject

Convert Alpha-Numeric IBAN to numeric values (incl. rearrangement)

Reference: en.wikipedia.org/wiki/International_Bank_Account_Number#Validating_the_IBAN



36
37
38
# File 'lib/prawn/swiss_qr_bill/iban.rb', line 36

def to_i
  "#{bban}#{country_code}#{check_digits}".gsub(/[A-Z]/) { |c| c.ord - 55 }.to_i
end

#valid?Boolean

valid IBAN: CH77 8080 8004 1110 4136 9

valid QR-IBAN: CH08 3080 8004 1110 4136 9

Returns:

  • (Boolean)


49
50
51
# File 'lib/prawn/swiss_qr_bill/iban.rb', line 49

def valid?
  valid_check_digits? && valid_swiss_length? && valid_country?
end

#valid_check_digits?Boolean

Returns:

  • (Boolean)


53
54
55
# File 'lib/prawn/swiss_qr_bill/iban.rb', line 53

def valid_check_digits?
  to_i % 97 == 1
end

#valid_country?Boolean

Returns:

  • (Boolean)


66
67
68
# File 'lib/prawn/swiss_qr_bill/iban.rb', line 66

def valid_country?
  country_code == 'CH'
end

#valid_length?Boolean

NOTE: Only the length for Switzerland is implemented because it is Swiss QR-bill specific

Returns:

  • (Boolean)


58
59
60
# File 'lib/prawn/swiss_qr_bill/iban.rb', line 58

def valid_length?
  valid_swiss_length?
end

#valid_swiss_length?Boolean

Returns:

  • (Boolean)


62
63
64
# File 'lib/prawn/swiss_qr_bill/iban.rb', line 62

def valid_swiss_length?
  @code.length == 21
end