Skip to content

Instantly share code, notes, and snippets.

@timotta
Created March 23, 2011 01:39
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save timotta/882465 to your computer and use it in GitHub Desktop.
Save timotta/882465 to your computer and use it in GitHub Desktop.
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
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