Class: Nexpose::Node
- Inherits:
-
Object
- Object
- Nexpose::Node
- Defined in:
- lib/nexpose/node.rb
Overview
This class represents each of the /NexposeReport/nodes/node elements in the Nexpose Full XML document.
It provides a convenient way to access the information scattered all over the XML in attributes and nested tags.
Instead of providing separate methods for each supported property we rely on Ruby’s #method_missing to do most of the work.
Instance Method Summary collapse
-
#endpoints ⇒ Object
Each of the endpoints associated with this node.
-
#initialize(xml_node) ⇒ Node
constructor
Accepts an XML node from Nokogiri::XML.
-
#method_missing(method, *args) ⇒ Object
This method is invoked by Ruby when a method that is not defined in this instance is called.
-
#respond_to?(method, include_private = false) ⇒ Boolean
This allows external callers (and specs) to check for implemented properties.
-
#supported_tags ⇒ Object
List of supported tags.
-
#tests(*args) ⇒ Object
Convert each ./test/test entry into a simple hash.
Constructor Details
#initialize(xml_node) ⇒ Node
Accepts an XML node from Nokogiri::XML.
12 13 14 |
# File 'lib/nexpose/node.rb', line 12 def initialize(xml_node) @xml = xml_node end |
Dynamic Method Handling
This class handles dynamic methods through the method_missing method
#method_missing(method, *args) ⇒ Object
This method is invoked by Ruby when a method that is not defined in this instance is called.
In our case we inspect the @method@ parameter and try to find the attribute, simple descendent or collection that it maps to in the XML tree.
59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 |
# File 'lib/nexpose/node.rb', line 59 def method_missing(method, *args) # We could remove this check and return nil for any non-recognized tag. # The problem would be that it would make tricky to debug problems with # typos. For instance: <>.potr would return nil instead of raising an # exception unless .include?(method) super return end # First we try the attributes. In Ruby we use snake_case, but in XML # hyphenated-case is used for some attributes translations_table = { device_id: 'device-id', hardware_address: 'hardware-address', risk_score: 'risk-score', site_name: 'site-name' } method_name = translations_table.fetch(method, method.to_s) return @xml.attributes[method_name].value if @xml.attributes.key?(method_name) # Then we try simple children tags: description, solution # Finally the enumerations: names if method_name == 'names' @xml.xpath('./names/name').collect(&:text) elsif ['fingerprints', 'software'].include?(method_name) xpath_selector = { 'fingerprints' => './fingerprints/os', 'software' => './software/fingerprint' }[method_name] xml_os = @xml.at_xpath(xpath_selector) return '' if xml_os.nil? xml_os.attributes['product'].value else # nothing found, the tag is valid but not present in this ReportItem return nil end end |
Instance Method Details
#endpoints ⇒ Object
Each of the endpoints associated with this node. Returns an array of Nexpose::Endpoint objects
42 43 44 |
# File 'lib/nexpose/node.rb', line 42 def endpoints @xml.xpath('./endpoints/endpoint').collect { |xml_endpoint| Endpoint.new(xml_endpoint) } end |
#respond_to?(method, include_private = false) ⇒ Boolean
This allows external callers (and specs) to check for implemented properties
48 49 50 51 |
# File 'lib/nexpose/node.rb', line 48 def respond_to?(method, include_private = false) return true if .include?(method.to_sym) super end |
#supported_tags ⇒ Object
List of supported tags. They can be attributes, simple descendans or collections (e.g. <references/>, <tags/>)
18 19 20 21 22 23 24 25 26 27 28 29 30 31 |
# File 'lib/nexpose/node.rb', line 18 def [ # attributes :address, :device_id, :hardware_address, :risk_score, :site_name, :status, # simple tags # multiple tags :fingerprints, :names, # compounded tags :endpoints, :software, :tests ] end |