Module: BrazilianUtils::IBANUtils
- Defined in:
- lib/brazilian-utils/iban-utils.rb
Overview
Utilities for the Brazilian IBAN: BR + 2 ISO 7064 MOD 97-10 check
digits + an 8-digit bank ISPB + a 5-digit branch + a 10-digit account +
a 1-letter account type + a 1-character owner indicator (29 characters
total).
Constant Summary collapse
- LENGTH =
29
Class Method Summary collapse
-
.format(value) ⇒ String
Formats an IBAN in the ISO 13616 print grouping: blocks of 4 characters separated by spaces, upper-cased.
-
.get_info(value) ⇒ Hash?
Parses a Brazilian IBAN into its fields.
-
.is_valid(value) ⇒ Boolean
(also: valid?)
Validates a Brazilian IBAN; any other country is invalid.
-
.parse(value) ⇒ String
Removes IBAN formatting, keeps letters and digits upper-cased, capped to 29 characters.
Class Method Details
.format(value) ⇒ String
Formats an IBAN in the ISO 13616 print grouping: blocks of 4 characters separated by spaces, upper-cased. Does not validate (use is_valid).
33 34 35 36 37 38 |
# File 'lib/brazilian-utils/iban-utils.rb', line 33 def self.format(value) cleaned = parse(value) return '' if cleaned.empty? apply_mask(cleaned, 4) end |
.get_info(value) ⇒ Hash?
Parses a Brazilian IBAN into its fields.
105 106 107 108 109 110 111 112 113 114 115 116 117 118 |
# File 'lib/brazilian-utils/iban-utils.rb', line 105 def self.get_info(value) return nil unless is_valid(value) compact = normalize(value) { countryCode: compact[0, 2], checkDigits: compact[2, 2], bankIspb: compact[4, 8], branch: compact[12, 5], account: compact[17, 10], accountType: compact[27, 1], owner: compact[28, 1] } end |
.is_valid(value) ⇒ Boolean Also known as: valid?
Validates a Brazilian IBAN; any other country is invalid.
83 84 85 86 87 88 89 90 91 92 93 94 95 |
# File 'lib/brazilian-utils/iban-utils.rb', line 83 def self.is_valid(value) compact = normalize(value) return false unless compact return false unless compact.start_with?('BR') return false unless compact[2, 2].match?(/\A\d{2}\z/) return false unless compact[4, 8].match?(/\A\d{8}\z/) return false unless compact[12, 5].match?(/\A\d{5}\z/) return false unless compact[17, 10].match?(/\A\d{10}\z/) return false unless compact[27, 1].match?(/\A[A-Z]\z/) return false unless compact[28, 1].match?(/\A[1-9A-Z]\z/) checksum_valid?(compact) end |
.parse(value) ⇒ String
Removes IBAN formatting, keeps letters and digits upper-cased, capped to 29 characters.
21 22 23 24 25 |
# File 'lib/brazilian-utils/iban-utils.rb', line 21 def self.parse(value) return '' unless value.is_a?(String) || value.is_a?(Integer) value.to_s.gsub(/[^a-zA-Z0-9]/, '').upcase[0, LENGTH] end |