Module: Mail::Jdec::MessagePatch

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

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.autodetect?(message) ⇒ Boolean

Returns:

  • (Boolean)


25
26
27
28
# File 'lib/mail/jdec/message_patch.rb', line 25

def autodetect?(message)
  !message.has_content_type? ||
    (mime_types_for_autodetect?(message.mime_type) && !message.has_charset? && !message.attachment? && !message.multipart?)
end

.mime_types_for_autodetect?(mime_type) ⇒ Boolean

Returns:

  • (Boolean)


30
31
32
33
34
35
36
37
38
# File 'lib/mail/jdec/message_patch.rb', line 30

def mime_types_for_autodetect?(mime_type)
  Jdec.config.mime_types_for_autodetect.any? do |type|
    if type.is_a?(Regexp)
      type.match?(mime_type)
    else
      type == mime_type
    end
  end
end

Instance Method Details

#decodedObject



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

def decoded
  decoded = super

  if Jdec.enabled? && MessagePatch.autodetect?(self)
    detected = Detector.detect(decoded)
    if detected && detected[:type] == :text
      charset = detected[:encoding].downcase
      decoded = Mail::Encodings.transcode_charset(decoded.dup.force_encoding(charset), charset, 'utf-8')
      header[:content_type] = 'text/plain' unless has_content_type?
      header[:content_type].parameters[:charset] = charset unless has_charset?
    else
      decoded = Mail::Encodings.transcode_charset(decoded, decoded.encoding, 'utf-8')
    end
  end

  decoded
end