URI::Generic methods

13 views
Skip to first unread message

Love U Ruby

unread,
May 25, 2013, 7:58:06 AM5/25/13
to rubyonra...@googlegroups.com
URL normalization :- http://en.wikipedia.org/wiki/URL_normalization - I
just read this source.

I found a method uri#normalization :
http://www.ruby-doc.org/stdlib-2.0/libdoc/uri/rdoc/URI/Generic.html#method-i-normalize-21

But not able to figure out how this method works:

require 'uri'

uri = URI.parse("http://www.example.com/../a/b/../c/./d.html")
p uri.normalize.to_s
#=> "http://www.example.com/../a/b/../c/./d.html"
But I expect " http://www.example.com/a/c/d.html"

Nothing any thing change encountered. So any one out there have any good
example to understand this method?

Thanks

--
Posted via http://www.ruby-forum.com/.

Matt Jones

unread,
May 26, 2013, 9:20:18 AM5/26/13
to rubyonra...@googlegroups.com
Quickest way is to read the source (available from the 'click to toggle source' link you get when hovering over a method):

def normalize!
  if path && path == ''
    set_path('/')
  end
  if scheme && scheme != scheme.downcase
    set_scheme(self.scheme.downcase)
  end
  if host && host != host.downcase
    set_host(self.host.downcase)
  end
end

looks like the idea is to handle the following oddly-formatted sorts of URIs:

http://example.com (no trailing /)
HttP://example.com/ (protocol capitalized oddly)
http://EXAMPLE.com/ (host capitalized)

If your goal is to normalize out those ..s in your path, File.expand_path may be more what you're looking for:

File.expand_path('/no/such/path/../srsly')  # => returns "/no/such/srsly"

But really, including .. in a URI isn't entirely valid - not all structures exposed via URI are filesystems, after all...

--Matt Jones 

Love U Ruby

unread,
May 26, 2013, 1:32:21 PM5/26/13
to rubyonra...@googlegroups.com
Matt Jones wrote in post #1110185:

> Quickest way is to read the source (available from the 'click to toggle
> source' link you get when hovering over a method):

> def normalize!
> if path && path == ''
> set_path('/')
> end
> if scheme && scheme != scheme.downcase
> set_scheme(self.scheme.downcase)
> end
> if host && host != host.downcase
> set_host(self.host.downcase)
> end
> end

> looks like the idea is to handle the following oddly-formatted sorts of
> URIs:
>
> http://example.com (no trailing /)
> HttP://example.com/ (protocol capitalized oddly)
> http://EXAMPLE.com/ (host capitalized)

Thanks @matt! I am happy to see this.
Reply all
Reply to author
Forward
0 new messages