Class: Ronin::Web::CLI::Commands::Diff Private

Inherits:
Ronin::Web::CLI::Command show all
Includes:
CommandKit::Colors
Defined in:
lib/ronin/web/cli/commands/diff.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.

Diffs two web pages.

Usage

ronin-web diff [options] {URL | FILE} {URL | FILE}

Arguments

URL | FILE                       The original URL or file
URL | FILE                       The modified URL or file

Options

-h, --help                       Print help information
-f, --format                     Pass the format of the URL or files. Supported formats are html and xml. (Default: html)

Since:

  • 1.0.0

Instance Method Summary collapse

Instance Method Details

#parse_doc(page) ⇒ Nokogiri::HTML::Document, 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.

Loads the given html or xml sources

Parameters:

  • page (String)

    The URL or file path of the original page.

Returns:

  • (Nokogiri::HTML::Document, Nokogiri::XML::Document)

    html or xml document depends upon --format option

Since:

  • 1.0.0



145
146
147
148
149
150
151
152
153
154
# File 'lib/ronin/web/cli/commands/diff.rb', line 145

def parse_doc(page)
  case options[:format]
  when :html
    Nokogiri::HTML(read(page))
  when :xml
    Nokogiri::XML(read(page))
  else
    raise(NotImplementedError,"unsupported format: #{options[:format].inspect}")
  end
end

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.

Prints a change to the document.

Parameters:

  • change ("+", "-")

    The type of change.

    • + - indicates an added node.
    • - - indicates a removed node.
  • node (Nokogiri::HTML::Node, Nokogiri::HTML::Node)

    The node that was changed.

Since:

  • 1.0.0



105
106
107
108
109
110
111
112
113
114
115
116
# File 'lib/ronin/web/cli/commands/diff.rb', line 105

def print_change(change,node)
  color = case change
          when '+' then colors.method(:green)
          when '-' then colors.method(:red)
          end

  content = node.to_s

  content.each_line(chomp: true) do |line|
    puts color.call("#{change} #{line}")
  end
end

#read(source) ⇒ String, File

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 web page.

Parameters:

  • source (String)

    The URL or file path of the web page.

Returns:

  • (String, File)

    The contents of the web page.

Since:

  • 1.0.0



127
128
129
130
131
132
133
134
# File 'lib/ronin/web/cli/commands/diff.rb', line 127

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(page1, page2) ⇒ 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 diff command.

Parameters:

  • page1 (String)

    The URL or file path of the original page.

  • page2 (String)

    The URL or file path of the modified page.

Since:

  • 1.0.0



82
83
84
85
86
87
88
89
90
91
# File 'lib/ronin/web/cli/commands/diff.rb', line 82

def run(page1,page2)
  doc1 = parse_doc(page1)
  doc2 = parse_doc(page2)

  doc1.diff(doc2) do |change,node|
    unless change == ' ' # ignroe unchanged nodes
      print_change(change,node)
    end
  end
end