12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
|
# File 'lib/grit_ext.rb', line 12
def encode!(message)
return nil unless message.respond_to? :force_encoding
message.force_encoding("UTF-8")
return message if message.valid_encoding?
detect = CharlockHolmes::EncodingDetector.detect(message)
return message.force_encoding("BINARY") if detect && detect[:type] == :binary
if detect && detect[:encoding]
message.force_encoding(detect[:encoding])
end
message.replace clean(message)
rescue
encoding = detect ? detect[:encoding] : "unknown"
"--broken encoding: #{encoding}"
end
|