What I'm trying to do is ask a user for a URL, then query that URL to see if it's up, and respond with "Response OK!" as the title describes.
require 'net/http'
require 'url'
Here I'm asking a user for a url and attempting to define that string as a variable:
puts "\n\nWhat website would you like to check?\n\n"
userinput = gets.chomp
Here I'm checking for an HTTP response with that variable $userinput
def main
while true
uri = URI.parse($userinput)
response = Net::HTTP::get_response(uri)
if response.code == "200"
puts "Response OK!"
else
puts "Received #{response.code} code. Probing again in 15s..."
end
sleep(15)
end
end
# Exit on CTRL-C SIGINT
Signal.trap("INT") {
puts "\nUser exited."
exit
}
main ()
Here is the code in action. I don't know how else to paste this:
What website would you like to check?
http://www.reddit.com
Traceback (most recent call last):
1: from prober.rb:33:in `<main>'
prober.rb:14:in `main': wrong number of arguments (given 1, expected 0) (ArgumentError)
Removing the space between main () results in this:
Traceback (most recent call last):
5: from prober.rb:33:in `<main>'
4: from prober.rb:16:in `main'
3: from /usr/local/Cellar/ruby/2.5.1/lib/ruby/2.5.0/uri/common.rb:237:in `parse'
2: from /usr/local/Cellar/ruby/2.5.1/lib/ruby/2.5.0/uri/rfc3986_parser.rb:73:in `parse'
1: from /usr/local/Cellar/ruby/2.5.1/lib/ruby/2.5.0/uri/rfc3986_parser.rb:15:in `split'
/usr/local/Cellar/ruby/2.5.1/lib/ruby/2.5.0/uri/rfc3986_parser.rb:18:in `rescue in split': bad URI(is not URI?): (URI::InvalidURIError)