class Addressable::URI

def self.unencode(uri, return_type=String, leave_encoded='')

Returns:
  • (String, Addressable::URI) -

Parameters:
  • leave_encoded (String) --
  • return_type (Class) --
  • uri (String, Addressable::URI, #to_str) --
def self.unencode(uri, return_type=String, leave_encoded='')
  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 = uri.dup
  # Seriously, only use UTF-8. I'm really not kidding!
  uri.force_encoding("utf-8")
  leave_encoded = leave_encoded.dup.force_encoding("utf-8")
  result = uri.gsub(/%[0-9a-f]{2}/iu) do |sequence|
    c = sequence[1..3].to_i(16).chr
    c.force_encoding("utf-8")
    leave_encoded.include?(c) ? sequence : c
  end
  result.force_encoding("utf-8")
  if return_type == String
    return result
  elsif return_type == ::Addressable::URI
    return ::Addressable::URI.parse(result)
  end
end