Sha256: efe7a0bb2fcb74f47f05a3ed448c4a43ad314e2272e24c7e3c491dd7d9c283de

Contents?: true

Size: 886 Bytes

Versions: 5

Compression:

Stored size: 886 Bytes

Contents

require_relative 'lengths'

module Iban
  class Validator
    def validate_iban(input)
      iban = sanitize_input(input)

      valid_length?(iban) && valid_checksum?(iban)
    end

    # public because it's used in `Ibanizator.calculate_iban`
    def sanitize_input(input)
      input.to_s.chomp.gsub(/\s+/,"")
    end

    private

    def valid_length?(iban)
      return false if iban.length <= 4 # two digits for the country code and two for the checksum
      country_code = iban[0..1].upcase.to_sym
      iban.length == LENGTHS[country_code]
    end

    def valid_checksum?(iban)
      number_representation = integerize(reorder(iban))
      number_representation % 97 == 1
    end

    def reorder(iban)
      "#{iban[4..-1]}#{iban[0..3]}"
    end

    def integerize(iban)
      iban.gsub(/[A-Z]/) do |match|
        match.ord - 'A'.ord + 10
      end.to_i
    end
  end
end

Version data entries

5 entries across 5 versions & 1 rubygems

Version Path
ibanizator-0.2.3 lib/iban/validator.rb
ibanizator-0.2.2 lib/iban/validator.rb
ibanizator-0.2.1 lib/iban/validator.rb
ibanizator-0.2.0 lib/iban/validator.rb
ibanizator-0.1.7 lib/iban/validator.rb