def self.encode(uri, return_type=String)
return nil if uri.nil?
begin
uri = uri.to_str
rescue NoMethodError, TypeError
raise TypeError, "Can't convert #{uri.class} into String."
end if !uri.is_a? String
if ![String, ::Addressable::URI].include?(return_type)
raise TypeError,
"Expected Class (String or Addressable::URI), " +
"got #{return_type.inspect}"
end
uri_object = uri.kind_of?(self) ? uri : self.parse(uri)
encoded_uri = Addressable::URI.new(
:scheme => self.encode_component(uri_object.scheme,
Addressable::URI::CharacterClasses::SCHEME),
:authority => self.encode_component(uri_object.authority,
Addressable::URI::CharacterClasses::AUTHORITY),
:path => self.encode_component(uri_object.path,
Addressable::URI::CharacterClasses::PATH),
:query => self.encode_component(uri_object.query,
Addressable::URI::CharacterClasses::QUERY),
:fragment => self.encode_component(uri_object.fragment,
Addressable::URI::CharacterClasses::FRAGMENT)
)
if return_type == String
return encoded_uri.to_s
elsif return_type == ::Addressable::URI
return encoded_uri
end
end