Sha256: 67868b1ca889b893c9f7a82969c7b78270b9ff9bddf03dd707ef744254930e86

Contents?: true

Size: 1.71 KB

Versions: 1

Compression:

Stored size: 1.71 KB

Contents

module VCardio
  class VCard
    def initialize(version = '3.0', properties = [], &block)
      @properties = properties
      @version    = version

      builder(&block) if block_given?
    end

    attr_reader :properties, :version

    def get(property_name)
      @properties.select { |p| p.name == property_name.to_s.upcase }
    end

    def separator
      if spec == :rfc2426
        "\n"
      elsif spec == :rfc6350
        "\r\n"
      end
    end

    def spec
      if @version == '3.0'
        :rfc2426
      elsif @version == '4.0'
        :rfc6350
      end
    end

    def to_abnf
      abnf = []
      abnf << 'BEGIN:VCARD'
      abnf << "VERSION:#{version}"

      @properties.each do |property|
        abnf << "#{Manilla.fold(property.to_abnf(spec), 75, "\r\n\s")}"
      end

      abnf << 'END:VCARD'

      abnf.join(separator)
    end
    alias_method :to_s, :to_abnf

    def to_file(path)
      file = File.new(path, 'w')

      file.write(to_abnf)
      file.flush
      file.close

      file
    end

    def ==(other)
      other.is_a?(VCardio::VCard) &&
        other.properties == properties &&
        other.version    == version
    end

    def method_missing(method_name, *args, &block)
      return super if respond_to?(method_name)
      get(method_name)
    end

    def self.parse(file)
      if file.is_a?(String)
        VCardio::Parser::DocumentParser.call(file)
      elsif file.is_a?(File)
        VCardio::Parser::DocumentParser.call(file.read)
      else
        fail(VCardio::Error, 'Could not parse. Expecting string or file.')
      end
    end

    private

    def builder(&block)
      builder = VCardio::Builder.call(&block)

      @properties.concat(builder.properties)
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
vcardio-0.1.1 lib/vcardio/vcard.rb