lib/uri-handler.rb in uri-handler-1.0.1 vs lib/uri-handler.rb in uri-handler-1.0.2

- old
+ new

@@ -1,17 +1,26 @@ # Adds basic funtionality to String class that will hopefully # make URI encoding tasks easier for people other than just myself :) require 'cgi' # better encoding support than uri - in my opinion anyways - # if you don't agree, for this project :) + # if you don't agree, fork this project :) class String - def uri_encode - CGI::escape self + 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 - def uri_encode! + def uri_encode! encoding=nil self.replace uri_encode end def uri_decode CGI::unescape self @@ -19,13 +28,13 @@ def uri_decode! self.replace uri_decode end - def to_uri - uri_encode + def to_uri encoding=nil + uri_encode encoding end - def to_uri! - self.replace to_uri + def to_uri! encoding=nil + self.replace to_uri encoding end end \ No newline at end of file