From: Rob Biedenharn Date: 2009-09-28T14:01:56+09:00 Subject: Re: Getting a valid URL from a command line On Sep 28, 2009, at 12:45 AM, Hunt Jon wrote: > On Mon, Sep 28, 2009 at 1:11 PM, Rob Biedenharn > wrote: >> On Sep 27, 2009, at 11:48 PM, Hunt Jon wrote: >>> >>> 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 You can see what the scheme is determined to be: irb> require 'uri' => true irb> u=URI.parse('http://example.com/') => # irb> u.scheme => "http" irb> x=URI.parse('example.com') => # irb> x.scheme => nil You probably don't want to jump down the Regexp rabbit-hole if you know that you want a valid URI. Let the library do the heavy lifting. -Rob Rob Biedenharn http://agileconsultingllc.com Rob@AgileConsultingLLC.com