class ChupaText::Decomposers::Mail

Constants

TARGET_EXTENSIONS
TARGET_MIME_TYPES

Public Instance Methods

decompose(data) { |part_data| ... } click to toggle source
# File lib/chupa-text/decomposers/mail.rb, line 35
def decompose(data)
  mail = ::Mail.new(data.body)
  decompose_attributes(mail, data)

  if mail.multipart?
    parts = mail.body.parts
  else
    parts = [mail]
  end
  parts.each_with_index do |part, i|
    body = part.body.decoded
    if part.charset
      begin
        body.force_encoding(part.charset)
      rescue ArgumentError
        raise UnknownEncodingError.new(data, part.charset)
      end
    end

    part_data = TextData.new(body, :source_data => data)
    uri = data.uri.dup
    if uri.fragment
      uri.fragment += "-#{i}"
    else
      uri.fragment = i.to_s
    end
    part_data.uri = uri
    part_data.mime_type = part.mime_type if part.mime_type
    part_data[:encoding] = body.encoding.to_s
    yield(part_data)
  end
end
target?(data) click to toggle source
# File lib/chupa-text/decomposers/mail.rb, line 28
def target?(data)
  return true if TARGET_MIME_TYPES.include?(data.mime_type)
  return false unless TARGET_EXTENSIONS.include?(data.extension)

  data.uri.fragment.nil?
end

Private Instance Methods

decompose_attributes(mail, data) click to toggle source
# File lib/chupa-text/decomposers/mail.rb, line 69
def decompose_attributes(mail, data)
  data["message-id"] = mail.message_id
  data["subject"] = mail.subject
  data["date"] = mail.date

  from = mail[:from]
  if from
    data["from"] = from.formatted | from.addresses
  end

  to = mail[:to]
  if to
    data["to"] = to.formatted | to.addresses
  end
end