I'm making a simple way to check is a site is up or not, this is my Ping model that holds a few adresses I want to check
require 'net/http'
def self.check
pings = Ping.all
pings.each do |p|
http = Net::HTTP.new(p.address,80)
response = http.request_get('/')
if response.message == ( 'OK' or 'Found')
puts 'up!!'
end
end
end
I'm just checking if the response message is "OK" or "Found" but my or statement only checks for "OK".
Also is this a good way to check?
response.message == ( 'OK' or 'Found')
isn't going to do what you think.
> ( 'OK' or 'Found')
=> "OK"
This is why it's only checking for "OK". IMHO you shouldn't check the message as that could vary. Check the response code for anything in the 200 or 300 range.
As long as you aren't checking a lot of sites the above is fine. If you were, it might begin to take awhile as they are sequential.
You might also want to add a timeout so that if it's trying to ping and fails for a 20 seconds consider the site down.
Also I know for a fact that some websites will return 400 errors for unrecognized user agents. So what you see via your browser might not be at all what your script is going too.