Class: Pingly
- Inherits:
-
Object
- Object
- Pingly
- Defined in:
- lib/pingly.rb
Constant Summary collapse
- VERSION =
'0.6.0'
Instance Attribute Summary collapse
-
#host ⇒ Object
Returns the value of attribute host.
-
#timeout ⇒ Object
Returns the value of attribute timeout.
Class Method Summary collapse
Instance Method Summary collapse
-
#initialize(host, timeout = 5) ⇒ Pingly
constructor
A new instance of Pingly.
- #ip_address ⇒ Object
- #packet_loss ⇒ Object
- #packets_received ⇒ Object
- #packets_sent ⇒ Object
- #ping! ⇒ Object
- #response ⇒ Object
- #successful? ⇒ Boolean
Constructor Details
#initialize(host, timeout = 5) ⇒ Pingly
Returns a new instance of Pingly.
23 24 25 |
# File 'lib/pingly.rb', line 23 def initialize(host, timeout = 5) self.host, self.timeout = host, timeout end |
Instance Attribute Details
#host ⇒ Object
Returns the value of attribute host.
6 7 8 |
# File 'lib/pingly.rb', line 6 def host @host end |
#timeout ⇒ Object
Returns the value of attribute timeout.
6 7 8 |
# File 'lib/pingly.rb', line 6 def timeout @timeout end |
Class Method Details
.ping!(host, timeout = 5) ⇒ Object
19 20 21 |
# File 'lib/pingly.rb', line 19 def self.ping!(host, timeout = 5) new(host,timeout).ping! end |
.ping_loop(host, timeout = 5) ⇒ Object
8 9 10 11 12 13 14 15 16 17 |
# File 'lib/pingly.rb', line 8 def self.ping_loop(host, timeout = 5) while true do p = new(host) p.ping! yield if block_given? && !p.successful? puts p.response end end |
Instance Method Details
#ip_address ⇒ Object
35 36 37 |
# File 'lib/pingly.rb', line 35 def ip_address response_regex(/^PING #{Regexp.escape(host)} \((\d+\.\d+\.\d+\.\d+)\)/) end |
#packet_loss ⇒ Object
31 32 33 |
# File 'lib/pingly.rb', line 31 def packet_loss response_regex(/(\d+\.\d+)\% packet loss$/).to_f end |
#packets_received ⇒ Object
43 44 45 |
# File 'lib/pingly.rb', line 43 def packets_received response_regex(/, (\d+)(?: packets)? received/).to_i end |
#packets_sent ⇒ Object
39 40 41 |
# File 'lib/pingly.rb', line 39 def packets_sent response_regex(/^(\d+) packets transmitted/).to_i end |
#ping! ⇒ Object
27 28 29 |
# File 'lib/pingly.rb', line 27 def ping! perform_ping end |
#response ⇒ Object
47 48 49 |
# File 'lib/pingly.rb', line 47 def response "#{Time.now.strftime('%Y-%m-%d %H:%M:%S')} - " + (successful? ? successful_response : failed_response) end |
#successful? ⇒ Boolean
51 52 53 54 55 |
# File 'lib/pingly.rb', line 51 def successful? ping! unless ping_performed response_status.success? && packet_loss < 25 end |