1

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)

sincera
  • 39
  • 1
  • 7
  • If looking for a way how to implement a url valid checker, take a look to [this](https://stackoverflow.com/a/5908162/5025116) answer. Any other question could be better adding the corresponding error stacktrace. – Sebastián Palma Aug 23 '18 at 23:19
  • What website would you like to check? http://www.reddit.com Traceback (most recent call last): 1: from prober.rb:33:in `
    ' prober.rb:14:in `main': wrong number of arguments (given 1, expected 0) (ArgumentError)
    – sincera Aug 23 '18 at 23:22
  • You can edit your question, adding the output you've commented. That and any other info. you can add to give us better understanding of your problem. – Sebastián Palma Aug 23 '18 at 23:25
  • For that specific problem, remove the space between `main` and the parenthesis. Ruby, due to the flexibility on accepting method calls without parenthesis is using the empty parenthesis as a method argument, hence your ArgumentError. – Sebastián Palma Aug 23 '18 at 23:44
  • I see the link, but that's checking a static url every time. I want it to check against a variable that the user defines, eg $userinput and the use of gets.chomp -- removing the space between main () gives me even more errors hehe – sincera Aug 23 '18 at 23:45
  • Note that checking `response.is_a?(Net::HTTPSuccess)` is more reliable than checking if the code is 200. It will cover all the valid 2xx response codes. You'll also have to [handle redirection](https://ruby-doc.org/stdlib-2.4.4/libdoc/net/http/rdoc/Net/HTTP.html#class-Net::HTTP-label-Following+Redirection). You may wish to use [the `http` gem](https://github.com/httprb/http) instead which can [handle redirection for you](https://github.com/httprb/http/wiki/Redirects). – Schwern Aug 24 '18 at 00:03
  • Oh excellent, that's exactly where I need to go next, because it's throwing a 301 at me – sincera Aug 24 '18 at 00:16
  • Guys thank you so much for this, I am just barely cutting my teeth and this helps tremendously. The HTTPSuccess tip is gold. I'm going to see about implementing the http gem instead for the redirection mentioned. – sincera Aug 24 '18 at 00:24

1 Answers1

0

For this ArgumentError problem:

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)

remove the space between main and the parenthesis. Ruby, due to the flexibility on accepting method calls without parenthesis is using the empty parenthesis as a method argument.

If you want to pass the user input to your main method, then you need to declare a parameter for it (better than using global variables).

You can use gets.chomp to store the user input, and when you call the main function, you pass that as argument.

So that can be like:

def main(user_input)
  while true
    uri = URI.parse(user_input)
    response = Net::HTTP.get_response(uri)
    ...
  end
end

puts "\n\nWhat website would you like to check?\n\n"
userinput = gets.chomp
main(userinput)

Notice the use of () in "void context" is interpreted as a nil object. Which is clearly valid for throwing an ArgumentError exception.

p ()
# nil
Sebastián Palma
  • 32,692
  • 6
  • 40
  • 59