Class: Nikto::Item

Inherits:
Object
  • Object
show all
Defined in:
lib/nikto/item.rb

Overview

This class represents each of the <item> elements in the Nikto 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.

Constant Summary collapse

TAGS_WITH_CSV_CONTENT =
%i[references].freeze

Instance Method Summary collapse

Constructor Details

#initialize(xml_node) ⇒ Item

Accepts an XML node from Nokogiri::XML.



14
15
16
# File 'lib/nikto/item.rb', line 14

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.



43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
# File 'lib/nikto/item.rb', line 43

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 supported_tags.include?(method)
    super
    return
  end
  # We need the translations_table because 'method' is a reserved word
  translations_table = {
    request_method: 'method'
  }
  method_name = translations_table.fetch(method, method.to_s)

  # First we try the attributes
  return @xml.attributes[method_name].value if @xml.attributes.key?(method_name)

  # Then we try simple children tags
  tag = @xml.xpath("./#{method_name}").first

  if tag && tag.text.present?
    text = tag.text
    TAGS_WITH_CSV_CONTENT.include?(method) ? cleanup_csv(text) : text
  end
end

Instance Method Details

#respond_to?(method, include_private = false) ⇒ Boolean

This allows external callers (and specs) to check for implemented properties

Returns:

  • (Boolean)


32
33
34
35
# File 'lib/nikto/item.rb', line 32

def respond_to?(method, include_private=false)
  return true if supported_tags.include?(method.to_sym)
  super
end

#supported_tagsObject

List of supported tags. They can be attributes, simple descendants or collections (e.g. <references/>, <tags/>)



20
21
22
23
24
25
26
27
28
# File 'lib/nikto/item.rb', line 20

def supported_tags
  [
    # attributes
    :id, :osvdbid, :osvdblink, :request_method,

    # simple tags
    :description, :iplink, :namelink, :uri, :references
  ]
end