Skip to content

Instantly share code, notes, and snippets.

@wyldrodney
Created March 25, 2012 03:55
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save wyldrodney/2191274 to your computer and use it in GitHub Desktop.
Save wyldrodney/2191274 to your computer and use it in GitHub Desktop.
require 'net/http'
require 'net/https'
require 'net/ping/tcp'
class Page
def initialize(address)
@address = address
get_status
end
def alive?
@alive
end
def status_code
@code
end
def status_string
@msg
end
private
def get_status
@uri = URI(@address)
if @uri
http = Net::HTTP.new(@uri.host, @uri.port)
http.use_ssl = true if @uri.scheme == 'https'
begin
response = http.get(@uri.path)
set_answer(response)
rescue
ping(@uri.host)
end
end
end
def ping(address)
Net::Ping::TCP.service_check = true
set_answer(Net::Ping::TCP.new(address).ping?)
end
def set_answer(response)
case
when response.kind_of?(TrueClass)
@alive = true
@code = nil
@msg = nil
when response.kind_of?(FalseClass)
@alive = false
@code = nil
@msg = nil
else
@alive = true
@code = response.code
@msg = response.message
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment