From a292d9e72268563a45bbb2c936e240139f7489a4 Mon Sep 17 00:00:00 2001 From: Florian Wininger Date: Tue, 5 Dec 2017 20:15:03 +0100 Subject: [PATCH 1/2] Improve coding style Signed-off-by: Florian Wininger --- .document | 3 --- .project | 11 ----------- .rubocop_todo.yml | 21 --------------------- Rakefile | 6 +++--- lib/ruby-nessus/Version1/host.rb | 4 ++-- lib/ruby-nessus/Version1/version1.rb | 6 +++--- lib/ruby-nessus/Version2/event.rb | 2 +- lib/ruby-nessus/Version2/host.rb | 4 ++-- lib/ruby-nessus/cli.rb | 12 ++++++------ 9 files changed, 17 insertions(+), 52 deletions(-) delete mode 100644 .document delete mode 100644 .project diff --git a/.document b/.document deleted file mode 100644 index 7f80cfc..0000000 --- a/.document +++ /dev/null @@ -1,3 +0,0 @@ -- -ChangeLog.md -LICENSE.txt diff --git a/.project b/.project deleted file mode 100644 index 7ca6678..0000000 --- a/.project +++ /dev/null @@ -1,11 +0,0 @@ - - - ruby-nessus - - - - - - - - diff --git a/.rubocop_todo.yml b/.rubocop_todo.yml index 6e244f0..a2847d3 100644 --- a/.rubocop_todo.yml +++ b/.rubocop_todo.yml @@ -6,11 +6,6 @@ # Note that changes in the inspected code, or installation of new # versions of RuboCop, may require this file to be generated again. -# Offense count: 4 -Lint/AmbiguousOperator: - Exclude: - - 'lib/ruby-nessus/cli.rb' - # Offense count: 1 Lint/DuplicateMethods: Exclude: @@ -21,22 +16,6 @@ Lint/RescueWithoutErrorClass: Exclude: - 'lib/ruby-nessus/cli.rb' -# Offense count: 2 -# Cop supports --auto-correct. -# Configuration parameters: IgnoreEmptyBlocks, AllowUnusedKeywordArguments. -Lint/UnusedBlockArgument: - Exclude: - - 'lib/ruby-nessus/cli.rb' - -# Offense count: 10 -Lint/UselessAssignment: - Exclude: - - 'Rakefile' - - 'lib/ruby-nessus/Version1/host.rb' - - 'lib/ruby-nessus/Version1/version1.rb' - - 'lib/ruby-nessus/Version2/event.rb' - - 'lib/ruby-nessus/Version2/host.rb' - # Offense count: 8 Metrics/AbcSize: Max: 70 diff --git a/Rakefile b/Rakefile index a2f0242..06cbb84 100644 --- a/Rakefile +++ b/Rakefile @@ -12,11 +12,11 @@ rescue LoadError => e end begin - gem 'rspec', '~> 2.4' + gem 'rspec', '~> 3.7' require 'rspec/core/rake_task' RSpec::Core::RakeTask.new -rescue LoadError => e +rescue LoadError task :spec do abort 'Please run `gem install rspec` to install RSpec.' end @@ -30,7 +30,7 @@ begin require 'yard' YARD::Rake::YardocTask.new -rescue LoadError => e +rescue LoadError task :yard do abort 'Please run `gem install yard` to install YARD.' end diff --git a/lib/ruby-nessus/Version1/host.rb b/lib/ruby-nessus/Version1/host.rb index 6026a67..2b6fd01 100644 --- a/lib/ruby-nessus/Version1/host.rb +++ b/lib/ruby-nessus/Version1/host.rb @@ -34,7 +34,7 @@ def scan_start_time if @host.at('startTime').inner_text.blank? false else - @host_scan_time = DateTime.strptime(@host.at('startTime').inner_text, fmt = '%a %b %d %H:%M:%S %Y') + @host_scan_time = DateTime.strptime(@host.at('startTime').inner_text, '%a %b %d %H:%M:%S %Y') end end @@ -47,7 +47,7 @@ def scan_stop_time if @host.at('stopTime').inner_text.blank? false else - @host_scan_time = DateTime.strptime(@host.at('stopTime').inner_text, fmt = '%a %b %d %H:%M:%S %Y') + @host_scan_time = DateTime.strptime(@host.at('stopTime').inner_text, '%a %b %d %H:%M:%S %Y') end end diff --git a/lib/ruby-nessus/Version1/version1.rb b/lib/ruby-nessus/Version1/version1.rb index 95197d8..562b54a 100644 --- a/lib/ruby-nessus/Version1/version1.rb +++ b/lib/ruby-nessus/Version1/version1.rb @@ -58,7 +58,7 @@ def title # def time datetime = @xml.xpath('//NessusClientData//Report//ReportName').inner_text.split(' - ').first - @report_time ||= DateTime.strptime(datetime, fmt = '%y/%m/%d %I:%M:%S %p') + @report_time ||= DateTime.strptime(datetime, '%y/%m/%d %I:%M:%S %p') end # @@ -71,7 +71,7 @@ def time # scan.start_time #=> 'Fri Nov 11 23:36:54 1985' # def start_time - @start_time = DateTime.strptime(@xml.xpath('//NessusClientData//Report//StartTime').inner_text, fmt = '%a %b %d %H:%M:%S %Y') + @start_time = DateTime.strptime(@xml.xpath('//NessusClientData//Report//StartTime').inner_text, '%a %b %d %H:%M:%S %Y') end # @@ -84,7 +84,7 @@ def start_time # scan.stop_time #=> 'Mon Nov 11 23:36:54 1985' # def stop_time - @stop_time = DateTime.strptime(@xml.xpath('//NessusClientData//Report//StopTime').inner_text, fmt = '%a %b %d %H:%M:%S %Y') + @stop_time = DateTime.strptime(@xml.xpath('//NessusClientData//Report//StopTime').inner_text, '%a %b %d %H:%M:%S %Y') end # diff --git a/lib/ruby-nessus/Version2/event.rb b/lib/ruby-nessus/Version2/event.rb index 963325e..9ac6d1f 100644 --- a/lib/ruby-nessus/Version2/event.rb +++ b/lib/ruby-nessus/Version2/event.rb @@ -230,7 +230,7 @@ def see_also # def patch_publication_date @patch_publication_date ||= if @event.at('patch_publication_date') - DateTime.strptime(@event.at('patch_publication_date').inner_text, fmt = '%Y/%m/%d') + DateTime.strptime(@event.at('patch_publication_date').inner_text, '%Y/%m/%d') end end diff --git a/lib/ruby-nessus/Version2/host.rb b/lib/ruby-nessus/Version2/host.rb index bdf6157..73d73dc 100644 --- a/lib/ruby-nessus/Version2/host.rb +++ b/lib/ruby-nessus/Version2/host.rb @@ -63,7 +63,7 @@ def ip # def start_time if (start_time = @host.at('tag[name=HOST_START]')) - DateTime.strptime(start_time.inner_text, fmt = '%a %b %d %H:%M:%S %Y') + DateTime.strptime(start_time.inner_text, '%a %b %d %H:%M:%S %Y') else false end @@ -80,7 +80,7 @@ def start_time # def stop_time if (stop_time = @host.at('tag[name=HOST_END]')) - DateTime.strptime(stop_time.inner_text, fmt = '%a %b %d %H:%M:%S %Y') + DateTime.strptime(stop_time.inner_text, '%a %b %d %H:%M:%S %Y') else false end diff --git a/lib/ruby-nessus/cli.rb b/lib/ruby-nessus/cli.rb index a5ec7ab..652cf36 100644 --- a/lib/ruby-nessus/cli.rb +++ b/lib/ruby-nessus/cli.rb @@ -95,16 +95,16 @@ def optparse(*args) @file = file end - opts.on('-h', '--help', 'This help summary page.') do |help| + opts.on('-h', '--help', 'This help summary page.') do |_help| Log.it opts Log.it - exit -1 + exit(-1) end - opts.on('-v', '--version', 'Recess Version.') do |version| + opts.on('-v', '--version', 'Recess Version.') do |_version| Log.it RubyNessus::VERSION Log.it - exit -1 + exit(-1) end begin @@ -113,13 +113,13 @@ def optparse(*args) if @file.nil? Log.it opts Log.it - exit -1 + exit(-1) end rescue => e Log.error e.message Log.it opts Log.it - exit -1 + exit(-1) end end end From 47d55ea2d7551f5e4d99443d9ba2e0b9f79837d1 Mon Sep 17 00:00:00 2001 From: Florian Wininger Date: Tue, 5 Dec 2017 20:17:13 +0100 Subject: [PATCH 2/2] Coding style improvements Signed-off-by: Florian Wininger --- .rubocop_todo.yml | 33 ---------------------------- examples/example.rb | 2 +- lib/ruby-nessus/Version1/host.rb | 8 +++---- lib/ruby-nessus/Version1/port.rb | 2 +- lib/ruby-nessus/Version1/version1.rb | 24 ++++++++++---------- lib/ruby-nessus/Version2/host.rb | 8 +++---- lib/ruby-nessus/Version2/version2.rb | 4 ++-- lib/ruby-nessus/cli.rb | 2 +- lib/ruby-nessus/parse.rb | 2 +- 9 files changed, 26 insertions(+), 59 deletions(-) diff --git a/.rubocop_todo.yml b/.rubocop_todo.yml index a2847d3..45cfafa 100644 --- a/.rubocop_todo.yml +++ b/.rubocop_todo.yml @@ -64,39 +64,6 @@ Naming/FileName: - 'lib/ruby-nessus/ruby-nessus.rb' - 'spec/ruby-nessus_spec.rb' -# Offense count: 7 -# Cop supports --auto-correct. -Performance/RedundantBlockCall: - Exclude: - - 'lib/ruby-nessus/Version1/host.rb' - - 'lib/ruby-nessus/Version1/version1.rb' - - 'lib/ruby-nessus/Version2/host.rb' - - 'lib/ruby-nessus/Version2/version2.rb' - - 'lib/ruby-nessus/parse.rb' - -# Offense count: 9 -# Cop supports --auto-correct. -Performance/StringReplacement: - Exclude: - - 'lib/ruby-nessus/Version1/host.rb' - - 'lib/ruby-nessus/Version1/version1.rb' - - 'lib/ruby-nessus/Version2/host.rb' - -# Offense count: 1 -# Cop supports --auto-correct. -# Configuration parameters: EnforcedStyle, SupportedStyles. -# SupportedStyles: braces, no_braces, context_dependent -Style/BracesAroundHashParameters: - Exclude: - - 'examples/example.rb' - -# Offense count: 2 -# Cop supports --auto-correct. -Style/ClassMethods: - Exclude: - - 'lib/ruby-nessus/Version1/port.rb' - - 'lib/ruby-nessus/cli.rb' - # Offense count: 8 Style/DateTime: Exclude: diff --git a/examples/example.rb b/examples/example.rb index 932a91d..d34f96a 100755 --- a/examples/example.rb +++ b/examples/example.rb @@ -40,6 +40,6 @@ def print_info(host) # From a string: puts '+ Using an XML string:' -Nessus::Parse.new(nil, { xml: File.read('example_v1.nessus') }) do |scan| +Nessus::Parse.new(nil, xml: File.read('example_v1.nessus')) do |scan| scan.each_host(&method(:print_info)) end diff --git a/lib/ruby-nessus/Version1/host.rb b/lib/ruby-nessus/Version1/host.rb index 2b6fd01..aab1fc0 100644 --- a/lib/ruby-nessus/Version1/host.rb +++ b/lib/ruby-nessus/Version1/host.rb @@ -237,7 +237,7 @@ def event_count # end def each_event(&block) @host.xpath('ReportItem').each do |event| - block.call(Event.new(event)) if block + yield(Event.new(event)) if block end end @@ -252,9 +252,9 @@ def events def get_runtime if scan_start_time && scan_stop_time - h = (Time.parse(scan_stop_time.to_s).strftime('%H').to_i - Time.parse(scan_start_time.to_s).strftime('%H').to_i).to_s.gsub('-', '') - m = (Time.parse(scan_stop_time.to_s).strftime('%M').to_i - Time.parse(scan_start_time.to_s).strftime('%M').to_i).to_s.gsub('-', '') - s = (Time.parse(scan_stop_time.to_s).strftime('%S').to_i - Time.parse(scan_start_time.to_s).strftime('%S').to_i).to_s.gsub('-', '') + h = (Time.parse(scan_stop_time.to_s).strftime('%H').to_i - Time.parse(scan_start_time.to_s).strftime('%H').to_i).to_s.delete('-') + m = (Time.parse(scan_stop_time.to_s).strftime('%M').to_i - Time.parse(scan_start_time.to_s).strftime('%M').to_i).to_s.delete('-') + s = (Time.parse(scan_stop_time.to_s).strftime('%S').to_i - Time.parse(scan_start_time.to_s).strftime('%S').to_i).to_s.delete('-') "#{h} hours #{m} minutes and #{s} seconds" else false diff --git a/lib/ruby-nessus/Version1/port.rb b/lib/ruby-nessus/Version1/port.rb index e6cc35e..c164e82 100644 --- a/lib/ruby-nessus/Version1/port.rb +++ b/lib/ruby-nessus/Version1/port.rb @@ -29,7 +29,7 @@ def initialize(service, number, protocol, raw_string) # New Port Object # @example # Port.parse(port) - def Port.parse(str) + def self.parse(str) @full_port = str components = str.match(/^([^\(]+)\((\d+)\/([^\)]+)\)/) diff --git a/lib/ruby-nessus/Version1/version1.rb b/lib/ruby-nessus/Version1/version1.rb index 562b54a..dc4f98d 100644 --- a/lib/ruby-nessus/Version1/version1.rb +++ b/lib/ruby-nessus/Version1/version1.rb @@ -97,9 +97,9 @@ def stop_time # scan.runtime #=> '2 hours 5 minutes and 16 seconds' # def runtime - h = (Time.parse(stop_time.to_s).strftime('%H').to_i - Time.parse(start_time.to_s).strftime('%H').to_i).to_s.gsub('-', '') - m = (Time.parse(stop_time.to_s).strftime('%M').to_i - Time.parse(start_time.to_s).strftime('%M').to_i).to_s.gsub('-', '') - s = (Time.parse(stop_time.to_s).strftime('%S').to_i - Time.parse(start_time.to_s).strftime('%S').to_i).to_s.gsub('-', '') + h = (Time.parse(stop_time.to_s).strftime('%H').to_i - Time.parse(start_time.to_s).strftime('%H').to_i).to_s.delete('-') + m = (Time.parse(stop_time.to_s).strftime('%M').to_i - Time.parse(start_time.to_s).strftime('%M').to_i).to_s.delete('-') + s = (Time.parse(stop_time.to_s).strftime('%S').to_i - Time.parse(start_time.to_s).strftime('%S').to_i).to_s.delete('-') "#{h} hours #{m} minutes and #{s} seconds" end @@ -201,7 +201,7 @@ def each_host(&block) hosts = [] @xml.xpath('//ReportHost').each do |host| hosts << host.at('HostName').inner_text if host.at('HostName').inner_text - block.call(Host.new(host)) if block + yield(Host.new(host)) if block end hosts end @@ -238,14 +238,14 @@ def host_count # scan.unique_ports #=> 234 # def unique_ports - unless @unique_ports - @unique_ports = [] - @xml.xpath('//ReportItem//port').each do |port| - @unique_ports << port.inner_text - end - @unique_ports.uniq! - @unique_ports.sort! + return if @unique_ports + + @unique_ports = [] + @xml.xpath('//ReportItem//port').each do |port| + @unique_ports << port.inner_text end + @unique_ports.uniq! + @unique_ports.sort! end # @@ -361,7 +361,7 @@ def find_by_hostname(hostname, &block) raise "Error: hostname can't be blank." if hostname.blank? @xml.xpath('//ReportHost[HostName]').each do |host| next unless host.inner_text.match(hostname) - block.call(Host.new(host)) if block + yield(Host.new(host)) if block end end diff --git a/lib/ruby-nessus/Version2/host.rb b/lib/ruby-nessus/Version2/host.rb index 73d73dc..d179f8b 100644 --- a/lib/ruby-nessus/Version2/host.rb +++ b/lib/ruby-nessus/Version2/host.rb @@ -343,7 +343,7 @@ def event_count # def each_event(&block) @host.xpath('ReportItem').each do |event| - block.call(Event.new(event)) if block + yield(Event.new(event)) if block end end @@ -532,9 +532,9 @@ def event_percentage_for(type, round_percentage = false) def get_runtime if stop_time && start_time - h = (Time.parse(stop_time.to_s).strftime('%H').to_i - Time.parse(start_time.to_s).strftime('%H').to_i).to_s.gsub('-', '') - m = (Time.parse(stop_time.to_s).strftime('%M').to_i - Time.parse(start_time.to_s).strftime('%M').to_i).to_s.gsub('-', '') - s = (Time.parse(stop_time.to_s).strftime('%S').to_i - Time.parse(start_time.to_s).strftime('%S').to_i).to_s.gsub('-', '') + h = (Time.parse(stop_time.to_s).strftime('%H').to_i - Time.parse(start_time.to_s).strftime('%H').to_i).to_s.delete('-') + m = (Time.parse(stop_time.to_s).strftime('%M').to_i - Time.parse(start_time.to_s).strftime('%M').to_i).to_s.delete('-') + s = (Time.parse(stop_time.to_s).strftime('%S').to_i - Time.parse(start_time.to_s).strftime('%S').to_i).to_s.delete('-') "#{h} hours #{m} minutes and #{s} seconds" else false diff --git a/lib/ruby-nessus/Version2/version2.rb b/lib/ruby-nessus/Version2/version2.rb index 8618eba..ce8d357 100644 --- a/lib/ruby-nessus/Version2/version2.rb +++ b/lib/ruby-nessus/Version2/version2.rb @@ -97,7 +97,7 @@ def each_host(&block) hosts = [] @xml.xpath('//ReportHost').each do |host| hosts << host['name'] if host['name'] - block.call(Host.new(host)) if block + yield(Host.new(host)) if block end hosts end @@ -328,7 +328,7 @@ def find_by_hostname(hostname, &block) raise "Error: hostname can't be blank." if hostname.blank? @xml.xpath('//ReportHost').each do |host| next unless host['name'].match(hostname) - block.call(Host.new(host)) if block + yield(Host.new(host)) if block end end diff --git a/lib/ruby-nessus/cli.rb b/lib/ruby-nessus/cli.rb index 652cf36..c33f027 100644 --- a/lib/ruby-nessus/cli.rb +++ b/lib/ruby-nessus/cli.rb @@ -13,7 +13,7 @@ def initialize @args = [] end - def CLI.run + def self.run new.run(*ARGV) end diff --git a/lib/ruby-nessus/parse.rb b/lib/ruby-nessus/parse.rb index 660d5de..d7bd748 100644 --- a/lib/ruby-nessus/parse.rb +++ b/lib/ruby-nessus/parse.rb @@ -23,7 +23,7 @@ def initialize(file = nil, options = {}, &block) raise 'Error: Supported .Nessus Version are 1 and 2.' end - block.call(@xml_parser) if block + yield(@xml_parser) if block end # Retrive scan from file