Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Ruby URI - How to get entire path after URL

Tags:

uri

parsing

ruby

How do you get the full path of the URL given the following

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

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

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

like image 950
user2974739 Avatar asked Aug 30 '15 04:08

user2974739


2 Answers

The method you are looking for is request_uri

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

You can use any method you'd like to remove the leading / if needed.

Edit: To get the part after the # sign, use fragment:

[uri.request_uri, uri.fragment].join("#")
=> "/posts?id=30&limit=5#time=1305298413"
like image 157
fdisk Avatar answered Oct 06 '22 00:10

fdisk


You can ask the URI object for its path, query, and fragment like this:

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

or (a little more consice, but less explicit):

"#{uri.request_uri}##{uri.fragment}"
# => "/posts?id=30&limit=5#time=1305298413"
like image 43
tessi Avatar answered Oct 06 '22 01:10

tessi