Sha256: 41a793f97325b0578236dad228662574e5721bed7dfbcb8a6d8025b6f04a7db2

Contents?: true

Size: 712 Bytes

Versions: 3

Compression:

Stored size: 712 Bytes

Contents

module Virtuaaliviivakoodi
  class ReferenceNormalizer
    REFERENCE_INTERNATIONAL_PATTERN = /^RF[0-9]{3,23}$/
    REFERENCE_NATIONAL_PATTERN = /^[0-9]{4,20}$/

    def self.call(reference)
      reference_without_whitespace = reference.to_s.gsub(/\s/, "")
      case reference_without_whitespace
      when REFERENCE_INTERNATIONAL_PATTERN
        # Pad everything after checksum
        reference_without_whitespace[2..3] + Virtuaaliviivakoodi.pad(reference_without_whitespace[4..-1], 21)
      when REFERENCE_NATIONAL_PATTERN
        # Pad everything
        Virtuaaliviivakoodi.pad(reference_without_whitespace, 20)
      else
        raise ArgumentError, "Reference is not valid"
      end
    end
  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
virtuaaliviivakoodi-1.0.0 lib/virtuaaliviivakoodi/reference_normalizer.rb
virtuaaliviivakoodi-0.1.1 lib/virtuaaliviivakoodi/reference_normalizer.rb
virtuaaliviivakoodi-0.1.0 lib/virtuaaliviivakoodi/reference_normalizer.rb