Guest User

Untitled

a guest
Feb 20th, 2018
77
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.16 KB | None | 0 0
  1. def self.encode(uri, character_class=nil, returning=String)
  2. return nil if uri.nil?
  3. if !uri.respond_to?(:to_str)
  4. raise TypeError, "Can't convert #{uri.class} into String."
  5. end
  6. if ![String, ::Addressable::URI].include?(returning)
  7. raise TypeError,
  8. "Expected String or Addressable::URI, got #{returning.inspect}"
  9. end
  10. uri_object = uri.kind_of?(self) ? uri : self.parse(uri.to_str)
  11. encoded_uri = Addressable::URI.new(
  12. :scheme => self.encode_component(uri_object.scheme,
  13. character_class || Addressable::URI::CharacterClasses::SCHEME),
  14. :authority => self.encode_component(uri_object.authority,
  15. character_class || Addressable::URI::CharacterClasses::AUTHORITY),
  16. :path => self.encode_component(uri_object.path,
  17. character_class || Addressable::URI::CharacterClasses::PATH),
  18. :query => self.encode_component(uri_object.query,
  19. character_class || Addressable::URI::CharacterClasses::QUERY),
  20. :fragment => self.encode_component(uri_object.fragment,
  21. character_class || Addressable::URI::CharacterClasses::FRAGMENT)
  22. )
Add Comment
Please, Sign In to add comment