Class: IBANTools::IBAN
- Inherits:
-
Object
- Object
- IBANTools::IBAN
- Defined in:
- lib/iban-tools/iban.rb
Class Method Summary collapse
- .canonicalize_code(code) ⇒ Object
-
.default_rules ⇒ Object
Load and cache the default rules from rules.yml.
- .from_local(country_code, data) ⇒ Object
- .valid?(code, rules = nil) ⇒ Boolean
Instance Method Summary collapse
- #bban ⇒ Object
- #check_digits ⇒ Object
-
#code ⇒ Object
The code in canonical form, suitable for storing in a database or sending over the wire.
- #country_code ⇒ Object
-
#initialize(code) ⇒ IBAN
constructor
A new instance of IBAN.
- #numerify ⇒ Object
-
#prettify ⇒ Object
The IBAN code in a human-readable format.
- #to_local ⇒ Object
- #to_s ⇒ Object
- #valid_check_digits? ⇒ Boolean
- #validation_errors(rules = nil) ⇒ Object
- #validation_errors_against_rules(rules) ⇒ Object
Constructor Details
#initialize(code) ⇒ IBAN
23 24 25 |
# File 'lib/iban-tools/iban.rb', line 23 def initialize( code ) @code = IBAN.canonicalize_code(code) end |
Class Method Details
.canonicalize_code(code) ⇒ Object
10 11 12 |
# File 'lib/iban-tools/iban.rb', line 10 def self.canonicalize_code( code ) code.to_s.strip.gsub(/\s+/, '').upcase end |
.default_rules ⇒ Object
Load and cache the default rules from rules.yml
15 16 17 |
# File 'lib/iban-tools/iban.rb', line 15 def self.default_rules @default_rules ||= IBANRules.defaults end |
.from_local(country_code, data) ⇒ Object
19 20 21 |
# File 'lib/iban-tools/iban.rb', line 19 def self.from_local(country_code, data) Conversion.local2iban country_code, data end |
.valid?(code, rules = nil) ⇒ Boolean
6 7 8 |
# File 'lib/iban-tools/iban.rb', line 6 def self.valid?( code, rules = nil ) new(code).validation_errors(rules).empty? end |
Instance Method Details
#bban ⇒ Object
63 64 65 |
# File 'lib/iban-tools/iban.rb', line 63 def bban @code[4..-1] end |
#check_digits ⇒ Object
59 60 61 |
# File 'lib/iban-tools/iban.rb', line 59 def check_digits @code[2..3] end |
#code ⇒ Object
The code in canonical form, suitable for storing in a database or sending over the wire
51 52 53 |
# File 'lib/iban-tools/iban.rb', line 51 def code @code end |
#country_code ⇒ Object
55 56 57 |
# File 'lib/iban-tools/iban.rb', line 55 def country_code @code[0..1] end |
#numerify ⇒ Object
71 72 73 74 75 76 77 78 79 80 81 82 83 84 |
# File 'lib/iban-tools/iban.rb', line 71 def numerify numerified = "" (@code[4..-1] + @code[0..3]).each_byte do |byte| numerified += case byte # 0..9 when 48..57 then byte.chr # 'A'..'Z' when 65..90 then (byte - 55).to_s # 55 = 'A'.ord + 10 else raise RuntimeError.new("Unexpected byte '#{byte}' in IBAN code '#{prettify}'") end end numerified end |
#prettify ⇒ Object
The IBAN code in a human-readable format
91 92 93 |
# File 'lib/iban-tools/iban.rb', line 91 def prettify @code.gsub(/(.{4})/, '\1 ').strip end |
#to_local ⇒ Object
27 28 29 |
# File 'lib/iban-tools/iban.rb', line 27 def to_local Conversion.iban2local country_code, bban end |
#to_s ⇒ Object
86 87 88 |
# File 'lib/iban-tools/iban.rb', line 86 def to_s "#<#{self.class}: #{prettify}>" end |
#valid_check_digits? ⇒ Boolean
67 68 69 |
# File 'lib/iban-tools/iban.rb', line 67 def valid_check_digits? numerify.to_i % 97 == 1 end |
#validation_errors(rules = nil) ⇒ Object
31 32 33 34 35 36 37 38 |
# File 'lib/iban-tools/iban.rb', line 31 def validation_errors( rules = nil ) errors = [] return [:too_short] if @code.size < 5 return [:bad_chars] unless @code =~ /^[A-Z0-9]+$/ errors += validation_errors_against_rules( rules || IBAN.default_rules ) errors << :bad_check_digits unless valid_check_digits? errors end |
#validation_errors_against_rules(rules) ⇒ Object
40 41 42 43 44 45 46 |
# File 'lib/iban-tools/iban.rb', line 40 def validation_errors_against_rules( rules ) errors = [] return [:unknown_country_code] if rules[country_code].nil? errors << :bad_length if rules[country_code]["length"] != @code.size errors << :bad_format unless bban =~ rules[country_code]["bban_pattern"] errors end |