end
def charset
- if header.field?("content-type") && header.fetch("content-type") =~ /charset="?(.*?)"?(;|$)/
+ if header.field?("content-type") && header.fetch("content-type") =~ /charset="?(.*?)"?(;|$)/i
$1
end
end
def defer_all_other_method_calls_to obj
class_eval %{
def method_missing meth, *a, &b; @#{obj}.send meth, *a, &b; end
- def respond_to? meth; @#{obj}.respond_to?(meth); end
+ def respond_to?(m, include_private = false)
+ @#{obj}.respond_to?(m, include_private)
+ end
}
end
end
def to_yaml x; __pass :to_yaml, x; end
def is_a? c; @o.is_a? c; end
- def respond_to? m; @o.respond_to? m end
+ def respond_to?(m, include_private=false)
+ @o.respond_to?(m, include_private)
+ end
def __pass m, *a, &b
begin
@m.synchronize { !@over && @over = true }
end
end
+
+class Iconv
+ def self.easy_decode target, charset, text
+ return text if charset =~ /^(x-unknown|unknown[-_ ]?8bit|ascii[-_ ]?7[-_ ]?bit)$/i
+ charset = case charset
+ when /UTF[-_ ]?8/i: "utf-8"
+ when /(iso[-_ ])?latin[-_ ]?1$/i: "ISO-8859-1"
+ when /iso[-_ ]?8859[-_ ]?15/i: 'ISO-8859-15'
+ when /unicode[-_ ]1[-_ ]1[-_ ]utf[-_]7/i: "utf-7"
+ else charset
+ end
+
+ # Convert:
+ #
+ # Remember - Iconv.open(to, from)!
+ Iconv.iconv(target + "//IGNORE", charset, text + " ").join[0 .. -2]
+ end
+end