Sha256: 52bb1756a01fa4019013feeb849c3f92a68e06fa1f132e3c9d451631b9caf1fd

Contents?: true

Size: 1.4 KB

Versions: 5

Compression:

Stored size: 1.4 KB

Contents

# frozen_string_literal: true

module CXML
  class Document < DocumentNode
    attr_writer :dtd

    accessible_attributes %i[
      payload_id
      timestamp
      version
      xml_lang
    ]
    accessible_nodes %i[
      header
      message
      request
      response
    ]

    def initialize_timestamp(value)
      @timestamp = Time.parse(value)
    end

    def timestamp
      @timestamp ||= Time.now.utc
    end

    def version
      @version ||= '1.2.037'
    end

    def dtd
      @dtd ||= 'cXML'
    end

    # Check if document is request
    # @return [Boolean]
    def request?
      !request.nil?
    end

    # Check if document is a response
    # @return [Boolean]
    def response?
      !response.nil?
    end

    # Check if document is a message
    # @return [Boolean]
    def message?
      !message.nil?
    end

    def from_xml(xml_string)
      initialize(Parser.new(data: xml_string).parse)
      self
    end

    def to_xml(doc = ox_doc)
      doc << to_element
      Ox.dump doc
    end

    def node_name
      'cXML'
    end

    def dtd_url
      "http://xml.cxml.org/schemas/cXML/#{version}/#{dtd}.dtd"
    end

    private

    def ox_doc
      doc = Ox::Document.new
      instruct = Ox::Instruct.new(:xml)
      instruct[:version] = '1.0'
      instruct[:encoding] = 'UTF-8'
      doc << instruct
      doc << Ox::DocType.new("cXML SYSTEM \"#{dtd_url}\"")
      doc
    end
  end
end

Version data entries

5 entries across 5 versions & 1 rubygems

Version Path
cxml-ruby-0.8.2 lib/cxml/document.rb
cxml-ruby-0.8.1 lib/cxml/document.rb
cxml-ruby-0.8.0 lib/cxml/document.rb
cxml-ruby-0.7.0 lib/cxml/document.rb
cxml-ruby-0.6.1 lib/cxml/document.rb