Class: Inspec::Resources::XmlConfig

Inherits:
JsonConfig show all
Defined in:
lib/inspec/resources/xml.rb

Instance Attribute Summary

Attributes inherited from JsonConfig

#params, #raw_content

Instance Method Summary collapse

Methods inherited from JsonConfig

#initialize, #method_missing, #resource_id, #to_s

Methods included from FileReader

#read_file_content

Methods included from ObjectTraverser

#extract_value

Constructor Details

This class inherits a constructor from Inspec::Resources::JsonConfig

Dynamic Method Handling

This class handles dynamic methods through the method_missing method in the class Inspec::Resources::JsonConfig

Instance Method Details

#parse(content) ⇒ Object



15
16
17
18
19
20
# File 'lib/inspec/resources/xml.rb', line 15

def parse(content)
  require "rexml/document" unless defined?(REXML::Document)
  REXML::Document.new(content)
rescue => e
  raise Inspec::Exceptions::ResourceFailed, "Unable to parse XML: #{e.message}"
end

#value(key) ⇒ Object



22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
# File 'lib/inspec/resources/xml.rb', line 22

def value(key)
  output = []
  REXML::XPath.each(@params, key.first.to_s) do |element|
    if element.is_a?(REXML::Attribute)
      output.push(element.to_s)
    elsif element.is_a?(REXML::Element)
      output.push(element.text)
    elsif element.is_a?(Integer) || element.is_a?(TrueClass) || element.is_a?(FalseClass) || element.is_a?(String)
      output.push(element)
    else
      raise Inspec::Exceptions::ResourceFailed, "Unknown XML object received (#{element.class}): #{element}"
    end
  end

  output
end