Skip to content

Instantly share code, notes, and snippets.

@rmw
Last active December 22, 2015 16:59
Show Gist options
  • Save rmw/6503146 to your computer and use it in GitHub Desktop.
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
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