From: Hunt Jon Date: 2009-09-28T13:45:40+09:00 Subject: Re: Getting a valid URL from a command line On Mon, Sep 28, 2009 at 1:11 PM, Rob Biedenharn wrote: > On Sep 27, 2009, at 11:48 PM, Hunt Jon wrote: > >> Hi - I'm working on the script below, which attempts at getting >> a user input and validate that the input is formed like a URL. >> And if the user fails to input, it should ask again. >> >> require 'uri' >> puts "Type a URL" >> begin >>  url = gets.chomp >>  URI.parse(url) # should raise if a variable 'url' is malformed. >> rescue URI::InvalidURIError >>  puts "That is not a valid URL. Try again." >>  retry >> end >> >> I expect that if I run "URI.parse()" it should raise an error, but >> it doesn't happen. >> >> Can anybody help me on this one? >> >> Jon > > require 'uri' > print "Type a URL: " > begin >  url = gets.chomp >  puts "You said: #{url.inspect}" >  uri = URI.parse(url) # should raise if a variable 'url' is malformed. >  puts uri.inspect > rescue URI::InvalidURIError >  puts "That is not a valid URL. Try again." >  retry > end > > Try getting a little bit more information out (and post what input you are > trying that you expect to be malformed). > > Note that some URI's are HTTP and some might be Generic.  There are a lot > more types of URI that just those that start with http://.  Have you ever > seen a jdbc resource string? > > -Rob I expect a user to input a HTTP or HTTPS URL. e.g., http://abcdef.gov Maybe using URI seems *too* generic after the research as 'uri' means different protocols, not just http/https. I'll look into it. Perhaps using Regexp match would be better. Jon