Class: CXML::Parser

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(data:) ⇒ Parser

Returns a new instance of Parser.



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

def initialize(data:)
  @data = data
end

Instance Attribute Details

#dataObject

Returns the value of attribute data.



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

def data
  @data
end

#parsed_dataObject

Returns the value of attribute parsed_data.



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

def parsed_data
  @parsed_data
end

Instance Method Details

#documentObject



21
22
23
24
25
26
27
28
29
# File 'lib/cxml/parser.rb', line 21

def document
  doc = Ox.load(data)
  @doc_type_string = doc.nodes.detect do |node|
    node.value&.match?(/^cXML /)
  end&.value
  doc.nodes.detect do |node|
    node.value&.match?(/^cxml$/i)
  end || doc
end

#dtdObject



35
36
37
# File 'lib/cxml/parser.rb', line 35

def dtd
  dtd_url&.match(%r{cXML/.*/(.*)\.dtd})&.to_a&.last
end

#dtd_urlObject



39
40
41
42
43
# File 'lib/cxml/parser.rb', line 39

def dtd_url
  return nil unless @doc_type_string

  @doc_type_string.match(/http.*\.dtd/)&.to_a&.first
end

#parseObject



12
13
14
15
16
17
18
19
# File 'lib/cxml/parser.rb', line 12

def parse
  @parsed_data = node_to_hash document
  if dtd_url
    @parsed_data[:version] = version
    @parsed_data[:dtd] = dtd
  end
  @parsed_data
end

#versionObject



31
32
33
# File 'lib/cxml/parser.rb', line 31

def version
  dtd_url&.match(%r{cXML/(.*)/.*\.dtd})&.to_a&.last
end