Created
March 25, 2012 03:55
-
-
Save wyldrodney/2191274 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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