Class: Ibandit::IBAN
- Inherits:
-
Object
- Object
- Ibandit::IBAN
- Defined in:
- lib/ibandit/iban.rb
Instance Attribute Summary collapse
-
#account_number ⇒ Object
readonly
Returns the value of attribute account_number.
-
#bank_code ⇒ Object
readonly
Returns the value of attribute bank_code.
-
#branch_code ⇒ Object
readonly
Returns the value of attribute branch_code.
-
#check_digits ⇒ Object
readonly
Returns the value of attribute check_digits.
-
#country_code ⇒ Object
readonly
Returns the value of attribute country_code.
-
#errors ⇒ Object
readonly
Returns the value of attribute errors.
-
#iban ⇒ Object
readonly
Returns the value of attribute iban.
Instance Method Summary collapse
- #bban ⇒ Object
-
#iban_national_id ⇒ Object
Component parts #.
-
#initialize(argument) ⇒ IBAN
constructor
A new instance of IBAN.
- #local_check_digits ⇒ Object
- #to_s(format = :compact) ⇒ Object
-
#valid? ⇒ Boolean
Validations #.
- #valid_account_number_format? ⇒ Boolean
- #valid_account_number_length? ⇒ Boolean
- #valid_bank_code_format? ⇒ Boolean
- #valid_bank_code_length? ⇒ Boolean
- #valid_branch_code_format? ⇒ Boolean
- #valid_branch_code_length? ⇒ Boolean
- #valid_characters? ⇒ Boolean
- #valid_check_digits? ⇒ Boolean
- #valid_country_code? ⇒ Boolean
- #valid_format? ⇒ Boolean
- #valid_length? ⇒ Boolean
- #valid_local_modulus_check? ⇒ Boolean
Constructor Details
#initialize(argument) ⇒ IBAN
Returns a new instance of IBAN.
8 9 10 11 12 13 14 15 16 17 18 19 |
# File 'lib/ibandit/iban.rb', line 8 def initialize(argument) if argument.is_a?(String) @iban = argument.to_s.gsub(/\s+/, '').upcase extract_local_details_from_iban! elsif argument.is_a?(Hash) build_iban_from_local_details(argument) else raise TypeError, 'Must pass an IBAN string or hash of local details' end @errors = {} end |
Instance Attribute Details
#account_number ⇒ Object (readonly)
Returns the value of attribute account_number.
5 6 7 |
# File 'lib/ibandit/iban.rb', line 5 def account_number @account_number end |
#bank_code ⇒ Object (readonly)
Returns the value of attribute bank_code.
5 6 7 |
# File 'lib/ibandit/iban.rb', line 5 def bank_code @bank_code end |
#branch_code ⇒ Object (readonly)
Returns the value of attribute branch_code.
5 6 7 |
# File 'lib/ibandit/iban.rb', line 5 def branch_code @branch_code end |
#check_digits ⇒ Object (readonly)
Returns the value of attribute check_digits.
5 6 7 |
# File 'lib/ibandit/iban.rb', line 5 def check_digits @check_digits end |
#country_code ⇒ Object (readonly)
Returns the value of attribute country_code.
5 6 7 |
# File 'lib/ibandit/iban.rb', line 5 def country_code @country_code end |
#errors ⇒ Object (readonly)
Returns the value of attribute errors.
5 6 7 |
# File 'lib/ibandit/iban.rb', line 5 def errors @errors end |
#iban ⇒ Object (readonly)
Returns the value of attribute iban.
5 6 7 |
# File 'lib/ibandit/iban.rb', line 5 def iban @iban end |
Instance Method Details
#bban ⇒ Object
50 51 52 |
# File 'lib/ibandit/iban.rb', line 50 def bban iban[4..-1] unless iban.nil? end |
#iban_national_id ⇒ Object
Component parts #
33 34 35 36 37 38 39 |
# File 'lib/ibandit/iban.rb', line 33 def iban_national_id return unless decomposable? national_id = bank_code.to_s national_id += branch_code.to_s national_id.slice(0, structure[:iban_national_id_length]) end |
#local_check_digits ⇒ Object
41 42 43 44 45 46 47 48 |
# File 'lib/ibandit/iban.rb', line 41 def local_check_digits return unless decomposable? && structure[:local_check_digit_position] iban.slice( structure[:local_check_digit_position] - 1, structure[:local_check_digit_length] ) end |
#to_s(format = :compact) ⇒ Object
21 22 23 24 25 26 27 |
# File 'lib/ibandit/iban.rb', line 21 def to_s(format = :compact) case format when :compact then iban.to_s when :formatted then formatted else raise ArgumentError, "invalid format '#{format}'" end end |
#valid? ⇒ Boolean
Validations #
58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 |
# File 'lib/ibandit/iban.rb', line 58 def valid? [ valid_country_code?, valid_characters?, valid_check_digits?, valid_length?, valid_bank_code_length?, valid_branch_code_length?, valid_account_number_length?, valid_format?, valid_bank_code_format?, valid_branch_code_format?, valid_account_number_format?, valid_local_modulus_check? ].all? end |
#valid_account_number_format? ⇒ Boolean
203 204 205 206 207 208 209 210 211 212 |
# File 'lib/ibandit/iban.rb', line 203 def valid_account_number_format? return unless valid_account_number_length? if account_number =~ Regexp.new(structure[:account_number_format]) true else @errors[:account_number] = 'is invalid' false end end |
#valid_account_number_length? ⇒ Boolean
142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 |
# File 'lib/ibandit/iban.rb', line 142 def valid_account_number_length? return unless valid_country_code? if account_number.nil? @errors[:account_number] = 'is required' return false end return true if account_number.length == structure[:account_number_length] @errors[:account_number] = 'is the wrong length (should be ' \ "#{structure[:account_number_length]} " \ 'characters)' false end |
#valid_bank_code_format? ⇒ Boolean
180 181 182 183 184 185 186 187 188 189 |
# File 'lib/ibandit/iban.rb', line 180 def valid_bank_code_format? return unless valid_bank_code_length? if bank_code =~ Regexp.new(structure[:bank_code_format]) true else @errors[:bank_code] = 'is invalid' false end end |
#valid_bank_code_length? ⇒ Boolean
112 113 114 115 116 117 118 119 120 121 122 123 124 125 |
# File 'lib/ibandit/iban.rb', line 112 def valid_bank_code_length? return unless valid_country_code? if bank_code.nil? || bank_code.length == 0 @errors[:bank_code] = 'is required' return false end return true if bank_code.length == structure[:bank_code_length] @errors[:bank_code] = 'is the wrong length (should be ' \ "#{structure[:bank_code_length]} characters)" false end |
#valid_branch_code_format? ⇒ Boolean
191 192 193 194 195 196 197 198 199 200 201 |
# File 'lib/ibandit/iban.rb', line 191 def valid_branch_code_format? return unless valid_branch_code_length? return true unless structure[:branch_code_format] if branch_code =~ Regexp.new(structure[:branch_code_format]) true else @errors[:branch_code] = 'is invalid' false end end |
#valid_branch_code_length? ⇒ Boolean
127 128 129 130 131 132 133 134 135 136 137 138 139 140 |
# File 'lib/ibandit/iban.rb', line 127 def valid_branch_code_length? return unless valid_country_code? return true if branch_code.to_s.length == structure[:branch_code_length] if structure[:branch_code_length] == 0 @errors[:branch_code] = "is not used in #{country_code}" elsif branch_code.nil? || branch_code.length == 0 @errors[:branch_code] = 'is required' else @errors[:branch_code] = 'is the wrong length (should be ' \ "#{structure[:branch_code_length]} characters)" end false end |
#valid_characters? ⇒ Boolean
158 159 160 161 162 163 164 165 166 167 |
# File 'lib/ibandit/iban.rb', line 158 def valid_characters? return if iban.nil? if iban.scan(/[^A-Z0-9]/).any? @errors[:characters] = 'Non-alphanumeric characters found: ' \ "#{iban.scan(/[^A-Z\d]/).join(' ')}" false else true end end |
#valid_check_digits? ⇒ Boolean
85 86 87 88 89 90 91 92 93 94 95 96 97 |
# File 'lib/ibandit/iban.rb', line 85 def valid_check_digits? return unless decomposable? && valid_characters? expected_check_digits = CheckDigit.iban(country_code, bban) if check_digits == expected_check_digits true else @errors[:check_digits] = 'Check digits failed modulus check. ' \ "Expected '#{expected_check_digits}', " \ "received '#{check_digits}'" false end end |
#valid_country_code? ⇒ Boolean
75 76 77 78 79 80 81 82 83 |
# File 'lib/ibandit/iban.rb', line 75 def valid_country_code? if Ibandit.structures.key?(country_code) true else @errors[:country_code] = "'#{country_code}' is not a valid " \ 'ISO 3166-1 IBAN country code' false end end |
#valid_format? ⇒ Boolean
169 170 171 172 173 174 175 176 177 178 |
# File 'lib/ibandit/iban.rb', line 169 def valid_format? return unless valid_country_code? if bban =~ Regexp.new(structure[:bban_format]) true else @errors[:format] = "Unexpected format for a #{country_code} IBAN." false end end |
#valid_length? ⇒ Boolean
99 100 101 102 103 104 105 106 107 108 109 110 |
# File 'lib/ibandit/iban.rb', line 99 def valid_length? return unless valid_country_code? && !iban.nil? if iban.length == structure[:total_length] true else @errors[:length] = "Length doesn't match SWIFT specification " \ "(expected #{structure[:total_length]} " \ "characters, received #{iban.size})" false end end |
#valid_local_modulus_check? ⇒ Boolean
214 215 216 217 218 219 |
# File 'lib/ibandit/iban.rb', line 214 def valid_local_modulus_check? return unless valid_format? return true unless Ibandit.modulus_checker valid_modulus_check_bank_code? && valid_modulus_check_account_number? end |