Class: Boxcars::XNode

Inherits:
Object
  • Object
show all
Defined in:
lib/boxcars/x_node.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(node) ⇒ XNode

Returns a new instance of XNode.



7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
# File 'lib/boxcars/x_node.rb', line 7

def initialize(node)
  @node = node
  @valid_names = []
  @children = {}
  @attributes = node.attributes.values.to_h { |a| [a.name.to_sym, a.value] }

  node.children.each do |child|
    next if child.text?

    child_node = XNode.new(child)
    if @children[child.name].nil?
      @valid_names << child.name.to_sym
      @children[child.name] = child_node
    elsif @children[child.name].is_a?(Array)
      @children[child.name] << child_node
    else
      @children[child.name] = [@children[child.name], child_node]
    end
  end
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(name, *args) ⇒ Object



70
71
72
73
74
# File 'lib/boxcars/x_node.rb', line 70

def method_missing(name, *args)
  return @children[name.to_s] if @children.key?(name.to_s)

  super
end

Instance Attribute Details

#attributes ⇒ Object

Returns the value of attribute attributes.



5
6
7
# File 'lib/boxcars/x_node.rb', line 5

def attributes
  @attributes
end

#children ⇒ Object

Returns the value of attribute children.



5
6
7
# File 'lib/boxcars/x_node.rb', line 5

def children
  @children
end

#node ⇒ Object

Returns the value of attribute node.



5
6
7
# File 'lib/boxcars/x_node.rb', line 5

def node
  @node
end

Class Method Details

.from_xml(xml) ⇒ Object



28
29
30
31
32
33
34
35
36
37
38
39
40
# File 'lib/boxcars/x_node.rb', line 28

def self.from_xml(xml)
  Boxcars::OptionalDependency.require!("nokogiri", feature: "XML trains and XML parsing helpers")
  xml = xml[xml.index("<")..-1] unless xml.start_with?("<")
  xml = xml[0..xml.rindex(">")] unless xml.end_with?(">")
  doc = Nokogiri::XML.parse(xml)
  if doc.errors.any?
    Boxcars.debug("XML: #{xml}", :yellow)
    # rubocop:disable-next Lint/Debugger
    debugger if ENV.fetch("DEBUG_XML", false)
    raise XmlError, "XML is not valid: #{doc.errors.map { |e| "#{e.line}:#{e.column} #{e.message}" }}"
  end
  XNode.new(doc.root)
end

Instance Method Details

#[](key) ⇒ Object



66
67
68
# File 'lib/boxcars/x_node.rb', line 66

def [](key)
  @children[key.to_s]
end

#respond_to_missing?(method_name, include_private = false) ⇒ Boolean

Returns:

  • (Boolean)


76
77
78
# File 'lib/boxcars/x_node.rb', line 76

def respond_to_missing?(method_name, include_private = false)
  @valid_names.include?(method_name.to_sym) || super
end

#stext ⇒ Object



62
63
64
# File 'lib/boxcars/x_node.rb', line 62

def stext
  @stext ||= text.gsub(/[[:space:]]+/, " ").strip # remove extra spaces
end

#text ⇒ Object



46
47
48
# File 'lib/boxcars/x_node.rb', line 46

def text
  @node.text
end

#xml ⇒ Object



42
43
44
# File 'lib/boxcars/x_node.rb', line 42

def xml
  @node.to_xml
end

#xpath(path) ⇒ Object



50
51
52
# File 'lib/boxcars/x_node.rb', line 50

def xpath(path)
  @node.xpath(path)
end

#xtext(path) ⇒ Object



54
55
56
57
58
59
60
# File 'lib/boxcars/x_node.rb', line 54

def xtext(path)
  # rubocop:disable-next Style/SafeNavigationChainLength
  rv = xpath(path)&.text&.gsub(/[[:space:]]+/, " ")&.strip
  return nil if rv.empty?

  rv
end