lib/nmap/os.rb in ruby-nmap-0.4.0 vs lib/nmap/os.rb in ruby-nmap-0.4.1
- old
+ new
@@ -1,10 +1,8 @@
require 'nmap/os_class'
require 'nmap/os_match'
-require 'enumerator'
-
module Nmap
class OS
include Enumerable
@@ -18,14 +16,14 @@
# If a block is given, it will passed the newly created OS object.
#
# @yieldparam [OS] os
# The newly created OS object.
#
- def initialize(node,&block)
+ def initialize(node)
@node = node
- block.call(self) if block
+ yield self if block_given?
end
#
# Parses the OS class information.
#
@@ -33,23 +31,26 @@
# Passes each OS class to the given block.
#
# @yieldparam [OSClass] class
# The OS class information.
#
- # @return [OS]
- # The OS information.
+ # @return [OS, Enumerator]
+ # The OS information. If no block was given, an enumerator object
+ # will be returned.
#
- def each_class(&block)
+ def each_class
+ return enum_for(:each_class) unless block_given?
+
@node.xpath("osclass").map do |osclass|
os_class = OSClass.new(
osclass['type'].to_sym,
osclass['vendor'],
osclass['osfamily'].to_sym,
osclass['accuracy'].to_i
)
- block.call(os_class) if block
+ yield os_class
end
return self
end
@@ -58,11 +59,11 @@
#
# @return [Array<OSClass>]
# The OS class information.
#
def classes
- Enumerator.new(self,:each_class).to_a
+ enum_for(:each_class).to_a
end
#
# Parses the OS match information.
#
@@ -70,21 +71,24 @@
# Passes each OS match to the given block.
#
# @yieldparam [OSMatch] class
# The OS match information.
#
- # @return [OS]
- # The OS information.
+ # @return [OS, Enumerator]
+ # The OS information. If no block was given, an enumerator object
+ # will be returned.
#
- def each_match(&block)
+ def each_match
+ return enum_for(:each_match) unless block_given?
+
@node.xpath("osmatch").map do |osclass|
os_match = OSMatch.new(
osclass['name'],
osclass['accuracy'].to_i
)
- block.call(os_match) if block
+ yield os_match
end
return self
end
@@ -93,30 +97,32 @@
#
# @return [Array<OSMatch>]
# The OS match information.
#
def matches
- Enumerator.new(self,:each_match).to_a
+ enum_for(:each_match).to_a
end
#
# Parses the ports used for guessing the OS.
#
# @return [Array<Integer>]
# The ports used.
#
def ports_used
- @node.xpath("portused/@portid").map { |port| port.inner_text.to_i }
+ @ports_used ||= @node.xpath("portused/@portid").map do |port|
+ port.inner_text.to_i
+ end
end
#
# Parses the OS fingerprint used by Nmap.
#
# @return [String]
# The OS fingerprint.
#
def fingerprint
- @node.at("osfingerprint/@fingerprint").inner_text
+ @fingerprint ||= @node.at("osfingerprint/@fingerprint").inner_text
end
#
# Parses the OS match information.
#