Class: FastestServer::Ping

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

Direct Known Subclasses

LinuxPing, WindowsPing

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(server) ⇒ Ping

Returns a new instance of Ping.



27
28
29
# File 'lib/fastest_server/ping.rb', line 27

def initialize(server)
  @server = server
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(name, *args) ⇒ Object



55
56
57
58
59
60
61
# File 'lib/fastest_server/ping.rb', line 55

def method_missing(name, *args)
  if name =~ /regex_/
    self.class.const_get(name.upcase)
  else
    super
  end
end

Class Method Details

.get_countObject



4
5
6
# File 'lib/fastest_server/ping.rb', line 4

def get_count
  @count ||= 4
end

.perform(server) ⇒ Object



12
13
14
# File 'lib/fastest_server/ping.rb', line 12

def perform(server)
  get_pinger.new(server).perform
end

.set_count(count) ⇒ Object



8
9
10
# File 'lib/fastest_server/ping.rb', line 8

def set_count(count)
  @count = [[count, 1].max, 100].min
end

Instance Method Details

#execute_ping_commandObject

Raises:

  • (NotImplementedError)


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

def execute_ping_command
  raise NotImplementedError, "Subclass must implement execute_ping_command method."
end

#parsed(**arguments) ⇒ Object

From MacOX ping manual

EXIT STATUS

The `ping` utility exits with one of the following values:
0         At least one response was heard from the specified host

2         The transmission was successful but no responses were received.  

any other value
          An error occurred.  These values are defined in <sysexists.h>


74
75
76
77
# File 'lib/fastest_server/ping.rb', line 74

def parsed(**arguments)
  {ip: @server, server: @server, site: @server, status: 0,
   loss: 100, max: 0.0, min: 0.0, avg: 0.0, stddev: 0.0}.merge(arguments)
end

#performObject

ping and parse the result



36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
# File 'lib/fastest_server/ping.rb', line 36

def perform
  result, status = execute_ping_command

  # we only process `successful` ping.
  return parsed(status: status) unless status == 0

  _, site, ip, _ = result.match(regex_ping)[0].split(" ")
  ip.gsub!(regex_filter, "")
  loss = result.match(regex_loss) ? $&.to_f : 100

  if stat = result.match(regex_stat)
    parsed(ip: ip, site: site, loss: loss,
          max: stat["max"].to_f, min: stat["min"].to_f,
          avg: stat["avg"].to_f, stddev: stat["stddev"]&.to_f || 0.0)
  else
    parsed(ip: ip, site: site, loss: loss, status: status)
  end
end