Class: CXML::Document

Inherits:
Object
  • Object
show all
Defined in:
lib/cxml/document.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(data = {}) ⇒ Document

Returns a new instance of Document.



11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
# File 'lib/cxml/document.rb', line 11

def initialize(data={})
  if data.kind_of?(Hash) && !data.empty?
    @version = data['version']
    @payload_id = data['payloadID']

    if data['timestamp']
      @timestamp = Time.parse(data['timestamp'])
    end

    if data['Header']
      @header = CXML::Header.new(data['Header'])
    end

    if data['Request']
      @request = CXML::Request.new(data['Request'])
    end

    if data['Response']
      @response = CXML::Response.new(data['Response'])
    end
  end
end

Instance Attribute Details

#headerObject

Returns the value of attribute header.



7
8
9
# File 'lib/cxml/document.rb', line 7

def header
  @header
end

#payload_idObject

Returns the value of attribute payload_id.



4
5
6
# File 'lib/cxml/document.rb', line 4

def payload_id
  @payload_id
end

#requestObject

Returns the value of attribute request.



8
9
10
# File 'lib/cxml/document.rb', line 8

def request
  @request
end

#responseObject

Returns the value of attribute response.



9
10
11
# File 'lib/cxml/document.rb', line 9

def response
  @response
end

#timestampObject

Returns the value of attribute timestamp.



5
6
7
# File 'lib/cxml/document.rb', line 5

def timestamp
  @timestamp
end

#versionObject

Returns the value of attribute version.



3
4
5
# File 'lib/cxml/document.rb', line 3

def version
  @version
end

Instance Method Details

#renderObject



52
53
54
55
56
57
58
59
60
# File 'lib/cxml/document.rb', line 52

def render
  node = CXML.builder
  node.cXML('version' => version, 'payloadID' => payload_id, 'timestamp' => timestamp.iso8601) do |doc|
    doc.Header { |n| @header.render(n) } if @header
    @request.render(node) if @request
    @response.render(node) if @response
  end
  node
end

#request?Boolean

Check if document is request

Returns:

  • (Boolean)


42
43
44
# File 'lib/cxml/document.rb', line 42

def request?
  !request.nil?
end

#response?Boolean

Check if document is a response

Returns:

  • (Boolean)


48
49
50
# File 'lib/cxml/document.rb', line 48

def response?
  !response.nil?
end

#setupObject



34
35
36
37
38
# File 'lib/cxml/document.rb', line 34

def setup
  @version    = CXML::Protocol.version
  @timestamp  = Time.now.utc
  @payload_id = "#{@timestamp.to_i}.process.#{Process.pid}@domain.com"
end