class EmlToPdf::ExtractionStep

Constants

MIME_TYPES

Public Class Methods

new(mail_or_part) click to toggle source
# File lib/eml_to_pdf/extraction_step.rb, line 12
def initialize(mail_or_part)
  @mail_or_part = mail_or_part
end

Public Instance Methods

finished?() click to toggle source
# File lib/eml_to_pdf/extraction_step.rb, line 27
def finished?
  !@mail_or_part.multipart?
end
next() click to toggle source
# File lib/eml_to_pdf/extraction_step.rb, line 16
def next
  if multipart_alternative?(@mail_or_part)
    best_part = extract_best_part(@mail_or_part.parts)
    ExtractionStep.new(best_part)
  elsif @mail_or_part.multipart?
    ExtractionStepList.new(@mail_or_part.parts.map { |part| ExtractionStep.new(part) })
  else
    self
  end
end
to_html() click to toggle source
# File lib/eml_to_pdf/extraction_step.rb, line 31
def to_html
  text_body(@mail_or_part)
end

Private Instance Methods

extract_best_part(parts) click to toggle source
# File lib/eml_to_pdf/extraction_step.rb, line 50
def extract_best_part(parts)
  parts.detect(&:multipart?) ||
    find_body_with_type(parts, :html) ||
    find_body_with_type(parts, :plain_text) ||
    EmlToPdf::EmptyPart.new
end
find_body_with_type(parts, type) click to toggle source
# File lib/eml_to_pdf/extraction_step.rb, line 57
def find_body_with_type(parts, type)
  parts.detect do |part|
    part.mime_type == MIME_TYPES[type]
  end
end
multipart_alternative?(part) click to toggle source
# File lib/eml_to_pdf/extraction_step.rb, line 36
def multipart_alternative?(part)
  part.mime_type == MIME_TYPES[:multipart_alternative]
end
text_body(mail_or_part) click to toggle source
# File lib/eml_to_pdf/extraction_step.rb, line 40
def text_body(mail_or_part)
  if mail_or_part.mime_type == MIME_TYPES[:html] && !mail_or_part.attachment?
    mail_or_part.decoded
  elsif mail_or_part.mime_type == MIME_TYPES[:plain_text] && !mail_or_part.attachment?
    wrap_text_in_pre_tag(mail_or_part.decoded)
  else
    ""
  end
end
wrap_text_in_pre_tag(text) click to toggle source
# File lib/eml_to_pdf/extraction_step.rb, line 63
def wrap_text_in_pre_tag(text)
  "<pre>#{text}</pre>"
end