class FastestServer::Ping

Public Class Methods

get_count() click to toggle source
# File lib/fastest_server/ping.rb, line 4
def get_count
  @count ||= 4
end
new(server) click to toggle source
# File lib/fastest_server/ping.rb, line 27
def initialize(server)
  @server = server
end
perform(server) click to toggle source
# File lib/fastest_server/ping.rb, line 12
def perform(server)
  get_pinger.new(server).perform
end
set_count(count) click to toggle source
# File lib/fastest_server/ping.rb, line 8
def set_count(count)
  @count = [[count, 1].max, 100].min
end

Private Class Methods

get_pinger() click to toggle source
# File lib/fastest_server/ping.rb, line 18
def get_pinger
  if (/cygwin|mswin|mingw|bccwin|wince|emx/ =~ RUBY_PLATFORM)
    WindowsPing
  else
    LinuxPing
  end
end

Public Instance Methods

execute_ping_command() click to toggle source
# File lib/fastest_server/ping.rb, line 31
def execute_ping_command
  raise NotImplementedError, "Subclass must implement execute_ping_command method."
end
method_missing(name, *args) click to toggle source
Calls superclass method
# 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
parsed(**arguments) click to toggle source

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>
# 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
perform() click to toggle source

ping and parse the result

# 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