Skip to content

Instantly share code, notes, and snippets.

@technoweenie
Created November 10, 2008 21:13
Show Gist options
  • Save technoweenie/23630 to your computer and use it in GitHub Desktop.
Save technoweenie/23630 to your computer and use it in GitHub Desktop.
def self.encode(uri, character_class=nil, returning=String)
return nil if uri.nil?
if !uri.respond_to?(:to_str)
raise TypeError, "Can't convert #{uri.class} into String."
end
if ![String, ::Addressable::URI].include?(returning)
raise TypeError,
"Expected String or Addressable::URI, got #{returning.inspect}"
end
uri_object = uri.kind_of?(self) ? uri : self.parse(uri.to_str)
encoded_uri = Addressable::URI.new(
:scheme => self.encode_component(uri_object.scheme,
character_class || Addressable::URI::CharacterClasses::SCHEME),
:authority => self.encode_component(uri_object.authority,
character_class || Addressable::URI::CharacterClasses::AUTHORITY),
:path => self.encode_component(uri_object.path,
character_class || Addressable::URI::CharacterClasses::PATH),
:query => self.encode_component(uri_object.query,
character_class || Addressable::URI::CharacterClasses::QUERY),
:fragment => self.encode_component(uri_object.fragment,
character_class || Addressable::URI::CharacterClasses::FRAGMENT)
)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment