Sha256: 6dd3ab0af3d128c4528d37361f178ce2e6ef42858949316c0b0cdfbdc9800aa9

Contents?: true

Size: 825 Bytes

Versions: 22

Compression:

Stored size: 825 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 == LENGTHS[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]/) do |match|
          match.ord - 'A'.ord + 10
        end.to_i
      end
    end
  end # Iban
end # Ibanizator

Version data entries

22 entries across 22 versions & 1 rubygems

Version Path
ibanizator-0.4.15 lib/ibanizator/iban/validator.rb
ibanizator-0.4.14 lib/ibanizator/iban/validator.rb
ibanizator-0.4.13 lib/ibanizator/iban/validator.rb
ibanizator-0.4.12 lib/ibanizator/iban/validator.rb
ibanizator-0.4.11 lib/ibanizator/iban/validator.rb
ibanizator-0.4.10 lib/ibanizator/iban/validator.rb
ibanizator-0.4.9 lib/ibanizator/iban/validator.rb
ibanizator-0.4.8 lib/ibanizator/iban/validator.rb
ibanizator-0.4.7 lib/ibanizator/iban/validator.rb
ibanizator-0.4.6 lib/ibanizator/iban/validator.rb
ibanizator-0.4.5 lib/ibanizator/iban/validator.rb
ibanizator-0.4.4 lib/ibanizator/iban/validator.rb
ibanizator-0.4.3 lib/ibanizator/iban/validator.rb
ibanizator-0.4.2 lib/ibanizator/iban/validator.rb
ibanizator-0.4.1 lib/ibanizator/iban/validator.rb
ibanizator-0.4.0 lib/ibanizator/iban/validator.rb
ibanizator-0.3.5 lib/ibanizator/iban/validator.rb
ibanizator-0.3.4 lib/ibanizator/iban/validator.rb
ibanizator-0.3.3 lib/ibanizator/iban/validator.rb
ibanizator-0.3.2 lib/ibanizator/iban/validator.rb