lib/nmap/xml.rb in ruby-nmap-0.4.0 vs lib/nmap/xml.rb in ruby-nmap-0.4.1
- old
+ new
@@ -2,11 +2,10 @@
require 'nmap/scan_task'
require 'nmap/scan'
require 'nmap/host'
require 'nokogiri'
-require 'enumerator'
module Nmap
class XML
include Enumerable
@@ -24,15 +23,15 @@
# If a block is given, it will be passed the new XML object.
#
# @yieldparam [XML] xml
# The newly created XML object.
#
- def initialize(path,&block)
+ def initialize(path)
@path = File.expand_path(path)
@doc = Nokogiri::XML(File.new(@path))
- block.call(self) if block
+ yield self if block_given?
end
#
# Parses the scanner information.
#
@@ -128,16 +127,19 @@
# Each host will be passed to a given block.
#
# @yieldparam [Host] host
# A host in the scan.
#
- # @return [XML]
- # The XML object.
+ # @return [XML, Enumerator]
+ # The XML object. If no block was given, an enumerator object will
+ # be returned.
#
def each_host(&block)
+ return enum_for(:each_host) unless block
+
@doc.xpath('/nmaprun/host').each do |host|
- block.call(Host.new(host)) if block
+ Host.new(host,&block)
end
return self
end
@@ -146,11 +148,11 @@
#
# @return [Array<Host>]
# The hosts in the scan.
#
def hosts
- Enumerator.new(self,:each_host).to_a
+ enum_for(:each_host).to_a
end
#
# Parses the hosts that were found to be up during the scan.
#
@@ -158,14 +160,17 @@
# Each host will be passed to a given block.
#
# @yieldparam [Host] host
# A host in the scan.
#
- # @return [XML]
- # The XML parser.
+ # @return [XML, Enumerator]
+ # The XML parser. If no block was given, an enumerator object will
+ # be returned.
#
def each_up_host(&block)
+ return enum_for(:each_up_host) unless block
+
@doc.xpath("/nmaprun/host[status[@state='up']]").each do |host|
Host.new(host,&block)
end
return self
@@ -176,19 +181,19 @@
#
# @return [Array<Host>]
# The hosts in the scan.
#
def up_hosts
- Enumerator.new(self,:each_up_host).to_a
+ enum_for(:each_up_host).to_a
end
#
# Parses the hosts that were found to be up during the scan.
#
- # @see each_up_hosts
+ # @see each_up_host
#
def each(&block)
- each_up_hosts(&block)
+ each_up_host(&block)
end
#
# Converts the XML parser to a String.
#