Class: Net::Ping
- Inherits:
-
Object
- Object
- Net::Ping
- Defined in:
- lib/net/ping/ping.rb
Overview
The Ping class serves as an abstract base class for all other Ping class types. You should not instantiate this class directly.
Direct Known Subclasses
External, Ping::HTTP, Ping::ICMP, Ping::TCP, Ping::UDP, Ping::WMI
Defined Under Namespace
Classes: External, HTTP, ICMP, TCP, UDP, WMI
Constant Summary collapse
- VERSION =
The version of the net-ping library.
'1.7.6'
Instance Attribute Summary collapse
-
#duration ⇒ Object
readonly
The number of seconds (returned as a Float) that it took to ping the host.
-
#exception ⇒ Object
readonly
If a ping fails, this value is set to the error that occurred which caused it to fail.
-
#host ⇒ Object
The host to ping.
-
#port ⇒ Object
The port to ping.
-
#timeout ⇒ Object
The maximum time a ping attempt is made.
-
#warning ⇒ Object
readonly
This value is set if a ping succeeds, but some other condition arose during the ping attempt which merits warning, e.g a redirect in the case of Ping::HTTP#ping.
Instance Method Summary collapse
-
#initialize(host = nil, port = nil, timeout = 5) {|_self| ... } ⇒ Ping
constructor
The default constructor for the Net::Ping class.
-
#ping(host = @host) ⇒ Object
(also: #pingecho)
The default interface for the Net::Ping#ping method.
- #ping6(host = @host) ⇒ Object
- #ping?(host = @host) ⇒ Boolean
Constructor Details
#initialize(host = nil, port = nil, timeout = 5) {|_self| ... } ⇒ Ping
The default constructor for the Net::Ping class. Accepts an optional host
, port
and timeout
. The port defaults to your echo port, or 7 if that happens to be undefined. The default timeout is 5 seconds.
The host, although optional in the constructor, must be specified at some point before the Net::Ping#ping method is called, or else an ArgumentError will be raised.
Yields self
in block context.
This class is not meant to be instantiated directly. It is strictly meant as an interface for subclasses.
57 58 59 60 61 62 63 64 65 66 |
# File 'lib/net/ping/ping.rb', line 57 def initialize(host=nil, port=nil, timeout=5) @host = host @port = port || Socket.getservbyname('echo') || 7 @timeout = timeout @exception = nil @warning = nil @duration = nil yield self if block_given? end |
Instance Attribute Details
#duration ⇒ Object (readonly)
The number of seconds (returned as a Float) that it took to ping the host. This is not a precise value, but rather a good estimate since there is a small amount of internal calculation that is added to the overall time.
42 43 44 |
# File 'lib/net/ping/ping.rb', line 42 def duration @duration end |
#exception ⇒ Object (readonly)
If a ping fails, this value is set to the error that occurred which caused it to fail.
29 30 31 |
# File 'lib/net/ping/ping.rb', line 29 def exception @exception end |
#host ⇒ Object
The host to ping. In the case of Ping::HTTP, this is the URI.
16 17 18 |
# File 'lib/net/ping/ping.rb', line 16 def host @host end |
#port ⇒ Object
The port to ping. This is set to the echo port (7) by default. The Ping::HTTP class defaults to port 80.
21 22 23 |
# File 'lib/net/ping/ping.rb', line 21 def port @port end |
#timeout ⇒ Object
The maximum time a ping attempt is made.
24 25 26 |
# File 'lib/net/ping/ping.rb', line 24 def timeout @timeout end |
#warning ⇒ Object (readonly)
This value is set if a ping succeeds, but some other condition arose during the ping attempt which merits warning, e.g a redirect in the case of Ping::HTTP#ping.
35 36 37 |
# File 'lib/net/ping/ping.rb', line 35 def warning @warning end |
Instance Method Details
#ping(host = @host) ⇒ Object Also known as: pingecho
The default interface for the Net::Ping#ping method. Each subclass should call super() before continuing with their own implementation in order to ensure that the @exception and @warning instance variables are reset.
If host
is nil here, then it will use the host specified in the constructor. If the host
is nil and there was no host specified in the constructor then an ArgumentError is raised. – The @duration should be set in the subclass’ ping method.
79 80 81 82 83 84 |
# File 'lib/net/ping/ping.rb', line 79 def ping(host = @host) raise ArgumentError, 'no host specified' unless host @exception = nil @warning = nil @duration = nil end |
#ping6(host = @host) ⇒ Object
86 87 88 89 90 91 |
# File 'lib/net/ping/ping.rb', line 86 def ping6(host = @host) raise ArgumentError, 'no host specified' unless host @exception = nil @warning = nil @duration = nil end |
#ping?(host = @host) ⇒ Boolean
93 94 95 |
# File 'lib/net/ping/ping.rb', line 93 def ping?(host = @host) !!ping(host) end |