Class: Pingly

Inherits:
Object
  • Object
show all
Defined in:
lib/pingly.rb

Constant Summary collapse

VERSION =
'0.6.0'

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

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

#hostObject

Returns the value of attribute host.



6
7
8
# File 'lib/pingly.rb', line 6

def host
  @host
end

#timeoutObject

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_addressObject



35
36
37
# File 'lib/pingly.rb', line 35

def ip_address
  response_regex(/^PING #{Regexp.escape(host)} \((\d+\.\d+\.\d+\.\d+)\)/)
end

#packet_lossObject



31
32
33
# File 'lib/pingly.rb', line 31

def packet_loss
  response_regex(/(\d+\.\d+)\% packet loss$/).to_f
end

#packets_receivedObject



43
44
45
# File 'lib/pingly.rb', line 43

def packets_received
  response_regex(/, (\d+)(?: packets)? received/).to_i
end

#packets_sentObject



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

#responseObject



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

Returns:

  • (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