class Net::Ping::HTTP

The Ping::HTTP class encapsulates methods for HTTP pings.

Attributes

follow_redirect[RW]

By default an http ping will follow a redirect and give you the result of the final URI. If this value is set to false, then it will not follow a redirect and will return false immediately on a redirect.

follow_redirect?[RW]

By default an http ping will follow a redirect and give you the result of the final URI. If this value is set to false, then it will not follow a redirect and will return false immediately on a redirect.

user_agent[RW]

Sets the user agent used for the HTTP request to a custom one.

Public Instance Methods

ping(host = @host) click to toggle source

Looks for an HTTP response from the URI passed to the constructor. If the result is a kind of Net::HTTPSuccess then the ping was successful and true is returned. Otherwise, false is returned and the Net::Ping#exception method should contain a string indicating what went wrong.

If the #follow_redirect accessor is set to true (which it is by default) and a redirect occurs during the ping, then the Net::Ping#warning attribute is set to the redirect message, but the return result is still true. If it’s set to false then a redirect response is considered a failed ping.

If no file or path is specified in the URI, then ‘/’ is assumed.

# File lib/net/ping/http.rb, line 49
def ping(host = @host)
  super(host)
  bool = false
  uri = URI.parse(host)

  start_time = Time.now

  begin
    response = nil
    uri_path = uri.path.empty? ? '/' : uri.path
    headers = { }
    headers["User-Agent"] = user_agent unless user_agent.nil?
    Timeout.timeout(@timeout) do
      Net::HTTP.new(uri.host, @port).start do |http|
        response = http.request_get(uri_path, headers)
      end
    end
  rescue Exception => err
    @exception = err.message
  else
    if response.is_a?(Net::HTTPSuccess)
      bool = true
    else
      if @follow_redirect
        @warning = response.message
            
        while response.is_a?(Net::HTTPRedirection)
          redirect = URI.parse(response['location']) 
          redirect = uri + redirect if redirect.relative?                     
          response = Net::HTTP.get_response(redirect.host, redirect.path, @port)
        end
              
        if response.is_a?(Net::HTTPSuccess)
          bool = true
        else
          @warning = nil
          @exception = response.message
        end
      else
        @exception = response.message
      end
    end
  end

  # There is no duration if the ping failed
  @duration = Time.now - start_time if bool

  bool
end
Also aliased as: ping?, pingecho
ping?(host = @host) click to toggle source
Alias for: ping
pingecho(host = @host) click to toggle source
Alias for: ping

Public Class Methods

new(uri=nil, port=80, timeout=5) click to toggle source

Creates and returns a new Ping::HTTP object. Note that the default port for Ping::HTTP is 80.

# File lib/net/ping/http.rb, line 30
def initialize(uri=nil, port=80, timeout=5)
  @follow_redirect = true
  super(uri, port, timeout)
end