URI::Generic methods

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

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

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.