Sha256: f4cdb17f0c1ad86243b28a58119d3a41ec8ab5eb72e0419ff4f46c12e70842de

Contents?: true

Size: 811 Bytes

Versions: 2

Compression:

Stored size: 811 Bytes

Contents

class Ibanizator
  class Iban
    class Validator
      attr_reader :iban

      def initialize(iban)
        @iban = iban.to_s
      end

      def validate
        valid_length? && valid_checksum?
      end

      private

      def valid_length?
        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 == COUNTRY_CODES[country_code]
      end

      def valid_checksum?
        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]/) { |match| match.ord - 'A'.ord + 10 }.to_i
      end
    end
  end # Iban
end # Ibanizator

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
dkd-ibanizator-0.10.0 lib/ibanizator/iban/validator.rb
dkd-ibanizator-0.9.0 lib/ibanizator/iban/validator.rb