Method: Addressable::URI.unencode
- Defined in:
- lib/addressable/uri.rb
.unencode(uri, return_type = String, leave_encoded = '') ⇒ String, Addressable::URI Also known as: unescape, unencode_component, unescape_component
Unencodes any percent encoded characters within a URI component. This method may be used for unencoding either components or full URIs, however, it is recommended to use the unencode_component
alias when unencoding components.
472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 |
# File 'lib/addressable/uri.rb', line 472 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 result = uri.gsub(/%[0-9a-f]{2}/i) do |sequence| c = sequence[1..3].to_i(16).chr c.force_encoding(sequence.encoding) leave_encoded.include?(c) ? sequence : c end result.force_encoding(Encoding::UTF_8) if return_type == String return result elsif return_type == ::Addressable::URI return ::Addressable::URI.parse(result) end end |