Class: Prawn::SwissQRBill::IBAN
- Inherits:
-
Object
- Object
- Prawn::SwissQRBill::IBAN
- 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
-
#code ⇒ Object
readonly
Returns the value of attribute code.
Instance Method Summary collapse
- #bban ⇒ Object
- #check_digits ⇒ Object
- #country_code ⇒ Object
-
#initialize(code) ⇒ IBAN
constructor
A new instance of IBAN.
- #prettify ⇒ Object
-
#to_i ⇒ Object
Convert Alpha-Numeric IBAN to numeric values (incl. rearrangement).
-
#valid? ⇒ Boolean
valid IBAN: CH77 8080 8004 1110 4136 9.
- #valid_check_digits? ⇒ Boolean
- #valid_country? ⇒ Boolean
-
#valid_length? ⇒ Boolean
NOTE: Only the length for Switzerland is implemented because it is Swiss QR-bill specific.
- #valid_swiss_length? ⇒ Boolean
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
#code ⇒ Object (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
#bban ⇒ Object
28 29 30 |
# File 'lib/prawn/swiss_qr_bill/iban.rb', line 28 def bban @code[4..-1] end |
#check_digits ⇒ Object
24 25 26 |
# File 'lib/prawn/swiss_qr_bill/iban.rb', line 24 def check_digits @code[2..3] end |
#country_code ⇒ Object
20 21 22 |
# File 'lib/prawn/swiss_qr_bill/iban.rb', line 20 def country_code @code[0..1] end |
#prettify ⇒ Object
40 41 42 |
# File 'lib/prawn/swiss_qr_bill/iban.rb', line 40 def prettify @code.gsub(/(.{4})/, '\1 ').strip end |
#to_i ⇒ Object
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
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
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
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
58 59 60 |
# File 'lib/prawn/swiss_qr_bill/iban.rb', line 58 def valid_length? valid_swiss_length? end |
#valid_swiss_length? ⇒ Boolean
62 63 64 |
# File 'lib/prawn/swiss_qr_bill/iban.rb', line 62 def valid_swiss_length? @code.length == 21 end |