class Ibanify::IBAN

Public Class Methods

new(number) click to toggle source
# File lib/ibanify.rb, line 7
def initialize(number)
  @number = IBAN.normalize(number)
end
normalize(number) click to toggle source
# File lib/ibanify.rb, line 56
def self.normalize(number)
  number.strip.gsub(/\s+/, '').upcase
end
valid?(number) click to toggle source
# File lib/ibanify.rb, line 11
def self.valid?(number)
  new(number).validation_error.nil?
end

Public Instance Methods

bban() click to toggle source
# File lib/ibanify.rb, line 39
def bban
  @number[4..-1]
end
countries() click to toggle source
# File lib/ibanify.rb, line 43
def countries
  load_countries_from_string(File.read(File.dirname(__FILE__) + '/ibanify/countries.yml'))
end
country_code() click to toggle source
# File lib/ibanify.rb, line 35
def country_code
  @number[0..1]
end
load_countries_from_string(string) click to toggle source
# File lib/ibanify.rb, line 47
def load_countries_from_string(string)
  hash = YAML.load(string)
  hash.each do |country_code, rules|
    rules['bban'] = Regexp.new('^' + rules['bban'] + '$')
  end

  Countries.new(hash)
end
number() click to toggle source
# File lib/ibanify.rb, line 31
def number
  @number
end
valid_check_digits?() click to toggle source
# File lib/ibanify.rb, line 22
def valid_check_digits?
  iban = @number[4..-1] + @number[0, 4]
  iban.gsub!(/./) do |c|
    c.to_i(36)
  end
 
  iban.to_i % 97 == 1
end
validation_error() click to toggle source
# File lib/ibanify.rb, line 15
def validation_error
  return :invalid_country_code unless countries[country_code]
  return :invalid_length       unless countries[country_code]['length'] == @number.size
  return :invalid_bban         unless bban =~ countries[country_code]['bban']
  return :invalid_check_digits unless valid_check_digits?
end