Created
March 23, 2011 01:39
-
-
Save timotta/882465 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class HttpCharset | |
def initialize(content_type) | |
@content_type = content_type | |
end | |
def encoding | |
return nil unless @content_type | |
par = @content_type.split(';').select do |c| | |
c.split('=').first.strip == 'charset' | |
end.first | |
return nil unless par | |
par.split('=').last.strip | |
end | |
def encode(str,to) | |
atual = encoding | |
return str unless atual | |
str.force_encoding(atual).encode(to) | |
end | |
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require 'http_charset' | |
class Net::HTTPResponse | |
alias :super_body :body | |
def body | |
HttpCharset.new(self['content-type']).encode(super_body,'UTF-8') | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment