class IBAN

Constants

VERSION

Public Class Methods

new(code) click to toggle source
# File lib/iban.rb, line 14
def initialize(code)
  @code = code.to_s.strip.gsub(/\s+/, '').upcase
end
specifications() click to toggle source
# File lib/iban.rb, line 6
def self.specifications
  @@specs ||= YAML.load_file(File.expand_path("iban/specs.yml", File.dirname(__FILE__)))
end
valid?(code) click to toggle source
# File lib/iban.rb, line 10
def self.valid?(code)
  new(code).valid?
end

Public Instance Methods

bban() click to toggle source
# File lib/iban.rb, line 26
def bban
  @code[4..-1]
end
check_digits() click to toggle source
# File lib/iban.rb, line 22
def check_digits
  @code[2..3]
end
country_code() click to toggle source
# File lib/iban.rb, line 18
def country_code
  @code[0..1]
end
to_i() click to toggle source
# File lib/iban.rb, line 30
def to_i
  "#{bban}#{country_code}#{check_digits}".each_byte.map do |byte|
    case byte
    when 48..57 then byte - 48 # 0..9
    when 65..90 then byte - 55 # A..Z
    else raise RuntimeError.new("Unexpected byte '#{byte}' in IBAN code '#{@code}'")
    end
  end.join.to_i
end
to_s(formatted=false) click to toggle source
# File lib/iban.rb, line 40
def to_s(formatted=false)
  formatted ? @code.gsub(/(.{4})/, '\1 ').strip : @code
end
valid?() click to toggle source
# File lib/iban.rb, line 44
def valid?
  valid_check_digits? && valid_length? && valid_bban?
end
valid_bban?() click to toggle source
# File lib/iban.rb, line 56
def valid_bban?
  !!bban_data
end
valid_check_digits?() click to toggle source
# File lib/iban.rb, line 48
def valid_check_digits?
  to_i % 97 == 1
end
valid_length?() click to toggle source
# File lib/iban.rb, line 52
def valid_length?
  !!specification && specification['length'] == @code.length
end

Private Instance Methods

bban_data() click to toggle source
# File lib/iban.rb, line 66
def bban_data
  @bban_data ||= Regexp.new("^#{specification['regexp']}$").match(bban) if specification
end
method_missing(method_name, *args) click to toggle source
Calls superclass method
# File lib/iban.rb, line 70
def method_missing(method_name, *args)
  respond_to?(method_name) ? bban_data[method_name] : super
end
respond_to_missing?(method_name, include_private=false) click to toggle source
Calls superclass method
# File lib/iban.rb, line 74
def respond_to_missing?(method_name, include_private=false)
  (bban_data && bban_data.names.include?(method_name.to_s)) || super
end
specification() click to toggle source
# File lib/iban.rb, line 62
def specification
  @specification ||= self.class.specifications[country_code.downcase]
end