Class: Ibanvalidator::IBAN

Inherits:
Object
  • Object
show all
Defined in:
lib/ibanvalidator/iban.rb

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(code) ⇒ IBAN

attr_accessor :code, :bank, :country, :location, :branch



7
8
9
# File 'lib/ibanvalidator/iban.rb', line 7

def initialize( code )
  @code = IBAN.canonicalize_code(code)
end

Class Method Details

.canonicalize_code(code) ⇒ Object



35
36
37
# File 'lib/ibanvalidator/iban.rb', line 35

def self.canonicalize_code( code )
  code.to_s.strip.gsub(/\s+/, '').upcase
end

.default_rulesObject

Load and cache the default rules from rules.yml



40
41
42
# File 'lib/ibanvalidator/iban.rb', line 40

def self.default_rules
  Ibanvalidator.default_rules 
end

.from_local(country_code, data) ⇒ Object



44
45
46
# File 'lib/ibanvalidator/iban.rb', line 44

def self.from_local(country_code, data)
  Conversion.local2iban country_code, data
end

.valid?(code, rules = nil) ⇒ Boolean

Returns:

  • (Boolean)


31
32
33
# File 'lib/ibanvalidator/iban.rb', line 31

def self.valid?( code, rules = nil )
  new(code).validation_errors(rules).empty?
end

Instance Method Details

#bbanObject



26
27
28
# File 'lib/ibanvalidator/iban.rb', line 26

def bban
  @code[4..-1]
end

#check_digitsObject



22
23
24
# File 'lib/ibanvalidator/iban.rb', line 22

def check_digits
  @code[2..3]
end

#codeObject

The code in canonical form, suitable for storing in a database or sending over the wire



14
15
16
# File 'lib/ibanvalidator/iban.rb', line 14

def code
  @code
end

#country_codeObject



18
19
20
# File 'lib/ibanvalidator/iban.rb', line 18

def country_code
  @code[0..1]
end

#numerifyObject



89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
# File 'lib/ibanvalidator/iban.rb', line 89

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

#prettifyObject

The IBAN code in a human-readable format



58
59
60
# File 'lib/ibanvalidator/iban.rb', line 58

def prettify
  @code.gsub(/(.{4})/, '\1 ').strip
end

#to_localObject



49
50
51
# File 'lib/ibanvalidator/iban.rb', line 49

def to_local
  Conversion.iban2local country_code, bban
end

#to_sObject



53
54
55
# File 'lib/ibanvalidator/iban.rb', line 53

def to_s
  "#<#{self.class}: #{prettify}>"
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).

Returns:

  • (Boolean)


84
85
86
87
# File 'lib/ibanvalidator/iban.rb', line 84

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



62
63
64
65
66
67
68
69
70
# File 'lib/ibanvalidator/iban.rb', line 62

def validation_errors( rules = nil )
  errors = []
  return [:too_short] if @code.size < 5
  return [:too_long] if @code.size > 34
  return [:bad_chars] unless @code =~ /^[A-Z0-9]+$/
  errors += validation_errors_against_rules( rules || Ibanvalidator.default_rules )
  errors << :bad_check_digits unless valid_check_digits?
  errors
end

#validation_errors_against_rules(rules) ⇒ Object



73
74
75
76
77
78
79
# File 'lib/ibanvalidator/iban.rb', line 73

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