Sha256: 99d3a94491d3d71ef888db3b33a24d56460d4b973afc2226ce327f2cd5cdb03f

Contents?: true

Size: 1.05 KB

Versions: 2

Compression:

Stored size: 1.05 KB

Contents

module VCardio
  module Validator
    #
    # @private
    #
    class DocumentValidator
      def self.call(document)
        lines = document.reject(&:empty?)

        begin_vcard = false
        end_vcard = false
        fn = false
        version = false

        lines.each_with_index do |line, i|
          if i == 0 && /\ABEGIN:VCARD\z/.match(line)
            begin_vcard = true
          elsif (i == lines.size - 1) && /\AEND:VCARD\z/.match(line)
            end_vcard = true
          elsif /\AFN:/.match(line)
            fn = true
          elsif i == 1 && /\AVERSION:(3.0|4.0)\z/.match(line)
            version = true
          end
        end

        fail(VCardio::Error, 'Invalid vCard: First line must be BEGIN:VCARD') unless begin_vcard
        fail(VCardio::Error, 'Invalid vCard: Last line must be END:VCARD') unless end_vcard
        fail(VCardio::Error, 'Invalid vCard: Second line must be VERSION with value 3.0 or 4.0') unless version
        fail(VCardio::Error, 'Invalid vCard: Must contain the FN property') unless fn
      end
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
vcardio-0.1.1 lib/vcardio/validator/document_validator.rb
vcardio-0.1.0 lib/vcardio/validator/document_validator.rb