pingly 0.3.0 → 0.4.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (2) hide show
  1. data/lib/pingly.rb +30 -9
  2. metadata +1 -1
data/lib/pingly.rb CHANGED
@@ -1,16 +1,16 @@
1
+ require 'open3'
2
+
1
3
  class Pingly
2
- VERSION = '0.3.0'
4
+ VERSION = '0.4.0'
3
5
 
4
- attr_accessor :host, :timeout, :raw_response
6
+ attr_accessor :host, :timeout
5
7
 
6
8
  def self.ping_loop(host, timeout = 5)
7
9
  while true do
8
10
  p = new(host)
9
11
  p.ping!
10
12
 
11
- if p.packet_loss > 25
12
- yield if block_given?
13
- end
13
+ yield if block_given? && !p.successful?
14
14
 
15
15
  puts p.response
16
16
  end
@@ -25,7 +25,7 @@ class Pingly
25
25
  end
26
26
 
27
27
  def ping!
28
- self.raw_response = `#{build_ping_string}`
28
+ perform_ping
29
29
  end
30
30
 
31
31
  def packet_loss
@@ -45,14 +45,35 @@ class Pingly
45
45
  end
46
46
 
47
47
  def response
48
- "#{Time.now.strftime('%Y-%m-%d %H:%M:%S')} - #{host}(#{ip_address}) - Sent: #{packets_sent} Received: #{packets_received} Loss: #{packet_loss}%"
48
+ "#{Time.now.strftime('%Y-%m-%d %H:%M:%S')} - " + (successful? ? successful_response : failed_response)
49
+ end
50
+
51
+ def successful?
52
+ ping! unless ping_performed
53
+
54
+ response_status.success? && packet_loss < 25
49
55
  end
50
56
 
51
57
  private
52
58
 
59
+ attr_accessor :ping_performed, :response_stdout, :response_stderr, :response_status
60
+
61
+ def successful_response
62
+ "#{host}(#{ip_address}) - Sent: #{packets_sent} Received: #{packets_received} Loss: #{packet_loss}%"
63
+ end
64
+
65
+ def failed_response
66
+ response_stderr.to_s
67
+ end
68
+
69
+ def perform_ping
70
+ self.response_stdout, self.response_stderr, self.response_status = Open3.capture3(build_ping_string)
71
+ self.ping_performed = true
72
+ end
73
+
53
74
  def response_regex(regex)
54
- ping! unless raw_response
55
- raw_response =~ regex
75
+ ping! unless ping_performed
76
+ response_stdout =~ regex
56
77
  $1
57
78
  end
58
79
 
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: pingly
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.3.0
4
+ version: 0.4.0
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors: