# Getting a valid URL from a command line

**URL:** <https://www.ruby-forum.com/t/getting-a-valid-url-from-a-command-line/176256>\
**Category:** Ruby\
**Created:** [September 28, 2009, 5:48am UTC](https://www.ruby-forum.com/t/getting-a-valid-url-from-a-command-line/176256 "2009-09-28T05:48:54Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![Hunt\_J](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/h/b9e5f3/32.png) [@Hunt\_J](https://www.ruby-forum.com/u/Hunt_J)\
**Post date:** [September 28, 2009, 5:48am UTC](https://www.ruby-forum.com/t/getting-a-valid-url-from-a-command-line/176256/1 "2009-09-28T05:48:54Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![Hunt\_J](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/h/b9e5f3/32.png) [@Hunt\_J](https://www.ruby-forum.com/u/Hunt_J)\
**Post date:** [September 28, 2009, 6:11am UTC](https://www.ruby-forum.com/t/getting-a-valid-url-from-a-command-line/176256/2 "2009-09-28T06:11:44Z")

</div>

On Sep 27, 2009, at 11:48 PM, Hunt J. wrote:

> 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

Rob B. [http://agileconsultingllc.com](http://agileconsultingllc.com)  
removed\_email\_address@domain.invalid

---

<div class="post-metadata">

**Author:** ![Hunt\_J](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/h/b9e5f3/32.png) [@Hunt\_J](https://www.ruby-forum.com/u/Hunt_J)\
**Post date:** [September 28, 2009, 6:46am UTC](https://www.ruby-forum.com/t/getting-a-valid-url-from-a-command-line/176256/3 "2009-09-28T06:46:16Z")

</div>

On Mon, Sep 28, 2009 at 1:11 PM, Rob B.  
[removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> > URI.parse(url) # should raise if a variable ‘url’ is malformed.  
> > Jon  
> > 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](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

---

<div class="post-metadata">

**Author:** ![Hunt\_J](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/h/b9e5f3/32.png) [@Hunt\_J](https://www.ruby-forum.com/u/Hunt_J)\
**Post date:** [September 28, 2009, 7:02am UTC](https://www.ruby-forum.com/t/getting-a-valid-url-from-a-command-line/176256/4 "2009-09-28T07:02:40Z")

</div>

On Sep 28, 2009, at 12:45 AM, Hunt J. wrote:

> > retry  
> > seen a jdbc resource string?
> > 
> > -Rob
> 
> I expect a user to input a HTTP or HTTPS URL. e.g., [http://abcdef.gov](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/](http://example.com/)’)  
=\> #\<URI::HTTP:0x395b34 URL:[http://example.com/](http://example.com/)\>  
irb\> u.scheme  
=\> “http”  
irb\> x=URI.parse(‘[example.com](http://example.com)’)  
=\> #\<URI::Generic:0x392f24 URL: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 B. [http://agileconsultingllc.com](http://agileconsultingllc.com)  
removed\_email\_address@domain.invalid

---

<div class="post-metadata">

**Author:** ![Hunt\_J](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/h/b9e5f3/32.png) [@Hunt\_J](https://www.ruby-forum.com/u/Hunt_J)\
**Post date:** [September 28, 2009, 3:40pm UTC](https://www.ruby-forum.com/t/getting-a-valid-url-from-a-command-line/176256/5 "2009-09-28T15:40:41Z")

</div>

On Sunday 27 September 2009 11:45:40 pm Hunt J. wrote:

> I expect a user to input a HTTP or HTTPS URL. e.g., [http://abcdef.gov](http://abcdef.gov)  
> Maybe using URI seems _too_ generic after the research as ‘uri’ means  
> different protocols, not just http/https.

Well, a URI isn’t even required to work. Just a clarification:

A URL is meant to actually refer to a resource. For example,

[http://ruby-lang.org/](http://ruby-lang.org/)

actually refers to a working website, and is thus a URL – thus, the  
protocol  
must be something that actually exists, and as a practical matter,  
you’ll want  
it to be something you (or your browser) know how to handle.

A URI only needs to be globally unique. For example:

[http://www.w3.org/1999/xhtml](http://www.w3.org/1999/xhtml)

It doesn’t matter AT ALL whether this points to a working resource. The  
Web  
will continue to work, even if [w3.org](http://w3.org) completely implodes. As a matter  
of  
courtesy, the W3C has actually made this a valid URL, which points to a  
description of what that namespace is, and the specifications that use  
it –  
but when your browser sees that URI at the top of a web page:

It doesn’t actually talk to [w3.org](http://w3.org) at all. It just knows internally that  
this  
namespaces is where HTML elements go in an XHTML document.

On a completely unrelated note, if you know how XML namespaces work,  
technically, the following would probably work, on browsers that  
understand  
XHTML:

\<foobar:html xmlns:foobar=‘[XHTML namespace](http://www.w3.org/1999/xhtml)’\>  
foobar:head  
…  
\</foobar:head\>  
foobar:body  
…  
\</foobar:body\>  
\</foobar:html\>

I suspect that the spec explicitly disallows this, at least in the  
“transitional” mode, because it’s not backwards compatible with HTML  
4.0. But  
the point is, internally, the browser is looking for an html element  
associated with that URI – which is why it’s not a valid xhtml document  
if  
you don’t include that xmlns in some form.
