Skip to content

Instantly share code, notes, and snippets.

@mislav
Created February 17, 2011 22:51
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mislav/832913 to your computer and use it in GitHub Desktop.
Save mislav/832913 to your computer and use it in GitHub Desktop.
Fixes comparing instances of Addressable::URI
require 'addressable/uri'
# feature-detect the bug
unless Addressable::URI.parse('/?a=1&b=2') === '/?b=2&a=1'
# fix `normalized_query` by sorting query key-value pairs
# (rejected: https://github.com/sporkmonger/addressable/issues/28)
class Addressable::URI
class << self
alias normalize_component_without_query_fix normalize_component
def normalize_component(component, character_class = CharacterClasses::RESERVED + CharacterClasses::UNRESERVED)
normalized = normalize_component_without_query_fix(component, character_class)
if character_class == Addressable::URI::CharacterClasses::QUERY
pairs = normalized.split('&').sort_by { |pair| pair[0, pair.index('=') || pair.length] }
normalized = pairs.join('&')
end
normalized
end
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment