lib/nmap/xml.rb in ruby-nmap-0.7.0 vs lib/nmap/xml.rb in ruby-nmap-0.8.0

- old
+ new

@@ -53,14 +53,23 @@ # If a block is given, it will be passed the new XML object. # # @yieldparam [XML] xml # The newly created XML object. # + # @since 0.8.0 + # + def self.parse(text,&block) + new(Nokogiri::XML(text), &block) + end + + # + # @deprecated Use {parse} instead. + # # @since 0.7.0 # def self.load(text,&block) - new(Nokogiri::XML(text), &block) + parse(text,&block) end # # Creates a new XML object from the file. # @@ -262,10 +271,69 @@ def hosts each_host.to_a end # + # Returns the first host. + # + # @return [Host] + # + # @since 0.8.0 + # + def host + each_host.first + end + + # + # Parses the hosts that were found to be down during the scan. + # + # @yield [host] + # Each host will be passed to a given block. + # + # @yieldparam [Host] host + # A down host in the scan. + # + # @return [XML, Enumerator] + # The XML parser. If no block was given, an enumerator object will + # be returned. + # + # @since 0.8.0 + # + def each_down_host + return enum_for(__method__) unless block_given? + + @doc.xpath("/nmaprun/host[status[@state='down']]").each do |host| + yield Host.new(host) + end + + return self + end + + # + # Parses the hosts found to be down during the scan. + # + # @return [Array<Host>] + # The down hosts in the scan. + # + # @since 0.8.0 + # + def down_hosts + each_down_host.to_a + end + + # + # Returns the first host found to be down during the scan. + # + # @return [Host] + # + # @since 0.8.0 + # + def down_host + each_down_host.first + end + + # # Parses the hosts that were found to be up during the scan. # # @yield [host] # Each host will be passed to a given block. # @@ -292,9 +360,20 @@ # @return [Array<Host>] # The hosts in the scan. # def up_hosts each_up_host.to_a + end + + # + # Returns the first host found to be up during the scan. + # + # @return [Host] + # + # @since 0.8.0 + # + def up_host + each_up_host.first end # # Parses the hosts that were found to be up during the scan. #