Class: Ronin::Web::CLI::Commands::Xml Private

Inherits:
Ronin::Web::CLI::Command show all
Defined in:
lib/ronin/web/cli/commands/xml.rb

Overview

This class is part of a private API. You should avoid using this class if possible, as it may be removed or be changed in the future.

Performs XPath queries on a URL or XML file.

Usage

ronin-web xml [options] {URL | FILE} [XPATH]

Options

-X, --xpath XPATH                XPath query
-t, --text                       Prints the inner-text
-F, --first                      Only print the first match
-h, --help                       Print help information

Arguments

URL | FILE                       The URL or FILE to search
XPATH                            The XPath query

Since:

  • 2.0.0

Direct Known Subclasses

Html

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#queryString? (readonly)

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

The XPath expression.

Returns:

  • (String, nil)

Since:

  • 2.0.0



85
86
87
# File 'lib/ronin/web/cli/commands/xml.rb', line 85

def query
  @query
end

Instance Method Details

#parse(html) ⇒ Nokogiri::XML::Document

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Parses the HTML source code.

Parameters:

  • html (String)

    The raw unparsed XML.

Returns:

  • (Nokogiri::XML::Document)

    The parsed XML document.

Since:

  • 2.0.0



141
142
143
# File 'lib/ronin/web/cli/commands/xml.rb', line 141

def parse(html)
  Nokogiri::XML(html)
end

#read(source) ⇒ File, String

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Reads a URI or file.

Parameters:

  • source (String)

    The URI or file path.

Returns:

  • (File, String)

    The contents of the URI or file.

Since:

  • 2.0.0



123
124
125
126
127
128
129
130
# File 'lib/ronin/web/cli/commands/xml.rb', line 123

def read(source)
  if source.start_with?('https://') ||
     source.start_with?('http://')
    Support::Network::HTTP.get_body(source)
  else
    File.new(source)
  end
end

#run(source, query = @query) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Runs the ronin-web xml command.

Parameters:

  • source (String)

    The URL or FILE argument.

  • query (String, nil) (defaults to: @query)

    The optional XPath argument.

Since:

  • 2.0.0



96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
# File 'lib/ronin/web/cli/commands/xml.rb', line 96

def run(source,query=@query)
  unless query
    print_error "must specify --xpath or an XPath argument"
    exit(-1)
  end

  doc   = parse(read(source))
  nodes = if options[:first] then doc.at(query)
          else                    doc.search(query)
          end

  if options[:text]
    puts nodes.inner_text
  else
    puts nodes
  end
end