Class: String
- Inherits:
-
Object
- Object
- String
- Defined in:
- lib/sws/extensions.rb
Overview
Extensions to String class - encoding and decoding of URL-encoded string
Instance Method Summary collapse
-
#url_decode ⇒ Object
Decode URL-encoded string - returns decoded string.
-
#url_decode! ⇒ Object
Decode URL-encoded string - modifies the receiver.
-
#url_encode ⇒ Object
Encode string with URL-encoding - returns encoded string.
-
#url_encode! ⇒ Object
Encode string with URL-encoding - modifies the receiver.
Instance Method Details
#url_decode ⇒ Object
Decode URL-encoded string - returns decoded string.
8 9 10 11 12 13 |
# File 'lib/sws/extensions.rb', line 8 def url_decode () tr('+', ' ').gsub(/((?:%[0-9a-fA-F]{2})+)/n) do [$1.delete('%')].pack('H*') end end |
#url_decode! ⇒ Object
Decode URL-encoded string - modifies the receiver. Returns decoded string.
17 18 19 20 21 22 23 24 25 |
# File 'lib/sws/extensions.rb', line 17 def url_decode! () tr!('+', ' ') gsub!(/((?:%[0-9a-fA-F]{2})+)/n) do [$1.delete('%')].pack('H*') end return self end |
#url_encode ⇒ Object
Encode string with URL-encoding - returns encoded string.
29 30 31 32 33 34 35 |
# File 'lib/sws/extensions.rb', line 29 def url_encode () gsub(/([^ a-zA-Z0-9_.-]+)/n) do '%' + $1.unpack('H2' * $1.size).join('%').upcase end.tr(' ', '+') end |
#url_encode! ⇒ Object
Encode string with URL-encoding - modifies the receiver. Returns encoded string.
40 41 42 43 44 45 46 |
# File 'lib/sws/extensions.rb', line 40 def url_encode! () gsub!(/([^ a-zA-Z0-9_.-]+)/n) do '%' + $1.unpack('H2' * $1.size).join('%').upcase end.tr(' ', '+') end |