Last active
December 22, 2015 16:59
-
-
Save rmw/6503146 to your computer and use it in GitHub Desktop.
Struct to parse url and query in the way you want it ... just full path(host + path) and query as hash with indifferent access
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
URL = Struct.new(:url_string, :full_path, :query, :parsed_url) do | |
def initialize(*args) | |
super | |
self.parsed_url = URI.parse(url_string) | |
self.full_path = self.parsed_url.host + self.parsed_url.path | |
self.query = CGI.parse(self.parsed_url.query).with_indifferent_access | |
end | |
end | |
u = Url.new("http://www.example.com/hello?id=12&stuff=more-stuff") | |
u.full_path == "www.example.com/hello" | |
u.query == { id: 12, stuff: 'more-stuff' } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment