Class: Ibanvalidator::IBAN
- Inherits:
-
Object
- Object
- Ibanvalidator::IBAN
- Defined in:
- lib/ibanvalidator/iban.rb
Instance Attribute Summary collapse
-
#errors(rules = nil) ⇒ Object
Returns the value of attribute errors.
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
quick_check Ibanvalidator::IBAN.valid?(“DE89370400440532013000”).
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.
- #sepa_scheme? ⇒ Boolean
-
#to_local(leading_zero = false) ⇒ Object
mit ignore_zero fasle werdnen die führenden nullen mit ausgegeben.
- #to_s ⇒ Object
-
#valid?(rules = nil) ⇒ Boolean
instanz.
-
#valid_check_digits? ⇒ Boolean
Pruefdsummen siehe de.wikipedia.org/wiki/IBAN#Validierung_der_Pr.C3.BCfsum Nun wird der Rest berechnet, der sich beim ganzzahligen Teilen der Zahl durch 97 ergibt (Modulo 97).
- #validation_errors(rules = nil) ⇒ Object
- #validation_errors_against_rules(rules) ⇒ Object
Constructor Details
#initialize(code) ⇒ IBAN
Returns a new instance of IBAN.
12 13 14 |
# File 'lib/ibanvalidator/iban.rb', line 12 def initialize( code ) @code = IBAN.canonicalize_code(code) end |
Instance Attribute Details
#errors(rules = nil) ⇒ Object
Returns the value of attribute errors.
5 6 7 |
# File 'lib/ibanvalidator/iban.rb', line 5 def errors @errors end |
Class Method Details
.canonicalize_code(code) ⇒ Object
50 51 52 |
# File 'lib/ibanvalidator/iban.rb', line 50 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
55 56 57 |
# File 'lib/ibanvalidator/iban.rb', line 55 def self.default_rules Ibanvalidator.default_rules end |
.from_local(country_code, data) ⇒ Object
59 60 61 |
# File 'lib/ibanvalidator/iban.rb', line 59 def self.from_local(country_code, data) Conversion.local2iban country_code, data end |
.valid?(code, rules = nil) ⇒ Boolean
quick_check Ibanvalidator::IBAN.valid?(“DE89370400440532013000”)
8 9 10 |
# File 'lib/ibanvalidator/iban.rb', line 8 def self.valid?( code, rules = nil ) new(code).valid?(rules) end |
Instance Method Details
#bban ⇒ Object
40 41 42 |
# File 'lib/ibanvalidator/iban.rb', line 40 def bban @code[4..-1] end |
#check_digits ⇒ Object
36 37 38 |
# File 'lib/ibanvalidator/iban.rb', line 36 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
28 29 30 |
# File 'lib/ibanvalidator/iban.rb', line 28 def code @code end |
#country_code ⇒ Object
32 33 34 |
# File 'lib/ibanvalidator/iban.rb', line 32 def country_code @code[0..1] end |
#numerify ⇒ Object
103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 |
# File 'lib/ibanvalidator/iban.rb', line 103 def numerify #Diese setzt sich aus #BBAN (in Deutschland z. B. 18 Stellen) + Länderkürzel kodiert + Prüfsumme zusammen. #Dabei werden die beiden Buchstaben des Länderkürzels sowie weitere etwa in der Kontonummer enthaltene Buchstaben durch ihre Position im lateinischen Alphabet + 9 ersetzt #(A = 10, B = 11, …, Z = 35). 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
73 74 75 |
# File 'lib/ibanvalidator/iban.rb', line 73 def prettify @code.gsub(/(.{4})/, '\1 ').strip end |
#sepa_scheme? ⇒ Boolean
45 46 47 |
# File 'lib/ibanvalidator/iban.rb', line 45 def sepa_scheme? Ibanvalidator.sepa_countries.include? country_code end |
#to_local(leading_zero = false) ⇒ Object
mit ignore_zero fasle werdnen die führenden nullen mit ausgegeben
64 65 66 |
# File 'lib/ibanvalidator/iban.rb', line 64 def to_local(leading_zero=false) Conversion.iban2local country_code, bban, leading_zero end |
#to_s ⇒ Object
68 69 70 |
# File 'lib/ibanvalidator/iban.rb', line 68 def to_s "#<#{self.class}: #{prettify}>" end |
#valid?(rules = nil) ⇒ Boolean
instanz
21 22 23 |
# File 'lib/ibanvalidator/iban.rb', line 21 def valid?(rules = nil) errors(rules).empty? end |
#valid_check_digits? ⇒ Boolean
Pruefdsummen siehe de.wikipedia.org/wiki/IBAN#Validierung_der_Pr.C3.BCfsum Nun wird der Rest berechnet, der sich beim ganzzahligen Teilen der Zahl durch 97 ergibt (Modulo 97).
98 99 100 101 |
# File 'lib/ibanvalidator/iban.rb', line 98 def valid_check_digits? ##Das Ergebnis muss 1 sein, ansonsten ist die IBAN falsch. numerify.to_i % 97 == 1 end |
#validation_errors(rules = nil) ⇒ Object
77 78 79 80 81 82 83 84 85 |
# File 'lib/ibanvalidator/iban.rb', line 77 def validation_errors( rules = nil ) errors = [] return [:iban_too_short] if @code.size < 5 return [:iban_too_long] if @code.size > 34 return [:iban_bad_chars] unless @code =~ /^[A-Z0-9]+$/ errors += validation_errors_against_rules( rules || Ibanvalidator.default_rules ) errors << :iban_bad_check_digits unless valid_check_digits? errors end |
#validation_errors_against_rules(rules) ⇒ Object
88 89 90 91 92 93 94 |
# File 'lib/ibanvalidator/iban.rb', line 88 def validation_errors_against_rules( rules ) errors = [] return [:iban_unknown_country_code] if rules[country_code].nil? errors << :iban_bad_length if rules[country_code]["length"] != @code.size errors << :iban_bad_format unless bban =~ rules[country_code]["bban_pattern"] errors end |