Class: String

Inherits:
Object
  • Object
show all
Defined in:
lib/uri-handler.rb

Overview

better encoding support than uri - in my opinion anyways if you don’t agree, fork this project :)

Instance Method Summary collapse

Instance Method Details

#to_uri(encoding = nil) ⇒ Object



33
34
35
# File 'lib/uri-handler.rb', line 33

def to_uri encoding=nil
  uri_encode encoding
end

#to_uri!(encoding = nil) ⇒ Object



37
38
39
# File 'lib/uri-handler.rb', line 37

def to_uri! encoding=nil
  self.replace to_uri encoding
end

#uri_decodeObject



25
26
27
# File 'lib/uri-handler.rb', line 25

def uri_decode
  CGI::unescape self
end

#uri_decode!Object



29
30
31
# File 'lib/uri-handler.rb', line 29

def uri_decode!
  self.replace uri_decode
end

#uri_encode(encoding = nil) ⇒ Object



8
9
10
11
12
13
14
15
16
17
18
19
# File 'lib/uri-handler.rb', line 8

def uri_encode encoding=nil
  begin
    CGI::escape self
  rescue ArgumentError=>e
    if e.to_s == 'invalid byte sequence in UTF-8'
      encoding = 'binary' if encoding.nil?
      CGI::escape self.force_encoding(encoding)
    else
      raise e
    end
  end
end

#uri_encode!(encoding = nil) ⇒ Object



21
22
23
# File 'lib/uri-handler.rb', line 21

def uri_encode! encoding=nil
  self.replace uri_encode
end