Class: Mail::Jdec::Decoder

Inherits:
Object
  • Object
show all
Defined in:
lib/mail/jdec/decoder.rb

Class Method Summary collapse

Class Method Details

.decode_if_needed(text) ⇒ Object



7
8
9
10
11
12
13
14
15
16
17
18
19
20
# File 'lib/mail/jdec/decoder.rb', line 7

def decode_if_needed(text)
  return text if text.nil? || !text.respond_to?(:encoding) || text.encoding == Encoding::UTF_8

  detected = Detector.detect(text)

  if detected
    charset = detected[:encoding].to_s
    text = Mail::Encodings.transcode_charset(text.dup.force_encoding(charset), charset, 'utf-8')
  else
    text = Mail::Encodings.transcode_charset(text, text.encoding, 'utf-8')
  end

  text
end

.decode_utf7(str) ⇒ Object



26
27
28
29
30
31
32
33
34
# File 'lib/mail/jdec/decoder.rb', line 26

def decode_utf7(str)
  str.gsub(/\+([^-]+)?-/n) do
    if $1
      ($1.tr(",", "/") + "===").unpack("m")[0].encode(Encoding::UTF_8, Encoding::UTF_16BE)
    else
      '+'
    end
  end
end

.force_utf8(str) ⇒ Object



22
23
24
# File 'lib/mail/jdec/decoder.rb', line 22

def force_utf8(str)
  str.dup.force_encoding('utf-8').encode('utf-8', invalid: :replace, undef: :replace)
end