Ruby URI - How to get the full path after a URL

How do you get the full path to the URL below

uri = URI("http://foo.com/posts?id=30&limit=5#time=1305298413")

I just want to posts?id=30&limit=5#time=1305298413

I tried uri.path and that returns / posts and ui.query returns 'id = 30 & limit = 5'

+4
source share
3 answers

You can request an object URIfor it path, queryand fragmentas follows:

"#{uri.path}?#{uri.query}##{uri.fragment}"
# => "/posts?id=30&limit=5#time=1305298413"

or (a bit more consignment, but less explicit):

"#{uri.request_uri}##{uri.fragment}"
# => "/posts?id=30&limit=5#time=1305298413"
+4
source

The method you are looking for request_uri

uri.request_uri
=> "/posts?id=30&limit=5"

You can use any method that you would like to remove if necessary /.

Edit: To get the item after the mark #, use fragment:

[uri.request_uri, uri.fragment].join("#")
=> "/posts?id=30&limit=5#time=1305298413"
+2
File.basename("http://foo.com/posts?id=30&limit=5#time=1305298413")
# => "posts?id=30&limit=5#time=1305298413"
+1

All Articles