Module: Mail::Jdec::UtilitiesPatch

Defined in:
lib/mail/jdec/utilities_patch.rb

Instance Method Summary collapse

Instance Method Details

#pick_encoding(charset) ⇒ Object



6
7
8
9
10
11
12
13
14
15
# File 'lib/mail/jdec/utilities_patch.rb', line 6

def pick_encoding(charset)
  if Jdec.enabled?
    Jdec.config.preferred_charsets.each do |from, to|
      if charset.to_s.downcase == from
        return Encoding.find(to)
      end
    end
  end
  super
end

#transcode_charset(str, from_encoding, to_encoding = Encoding::UTF_8) ⇒ Object



17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
# File 'lib/mail/jdec/utilities_patch.rb', line 17

def transcode_charset(str, from_encoding, to_encoding = Encoding::UTF_8)
  if Jdec.enabled?
    case from_encoding.to_s.downcase
    when 'unicode-1-1-utf-7'
      str = Decoder.decode_utf7(str).encode(to_encoding, undef: :replace, invalid: :replace)
    else
      str = super
    end
    if to_encoding.to_s.downcase == 'utf-8'
      str.gsub!(/^\x00+/, '')
      str.gsub!(/\x00+$/, '')
    end
    str
  else
    super
  end
end