Sha256: b287e33300fd60c8b09aa923b40505b8b7243d3b242ae17b596282a8e4a8ee52

Contents?: true

Size: 1.51 KB

Versions: 2

Compression:

Stored size: 1.51 KB

Contents

module EmlToPdfExt
  class ExtractionStep
    MIME_TYPES = {
      plain_text: 'text/plain',
      html: 'text/html',
      multipart_alternative: 'multipart/alternative'
    }

    def initialize(mail_or_part)
      @mail_or_part = mail_or_part
    end

    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

    def finished?
      !@mail_or_part.multipart?
    end

    def to_html
      text_body(@mail_or_part)
    end

    private
    def multipart_alternative?(part)
      part.mime_type == MIME_TYPES[:multipart_alternative]
    end

    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

    def extract_best_part(parts)
      parts.detect(&:multipart?) ||
        find_body_with_type(parts, :html) ||
        find_body_with_type(parts, :plain_text) ||
        EmptyPart.new
    end

    def find_body_with_type(parts, type)
      parts.detect do |part|
        part.mime_type == MIME_TYPES[type]
      end
    end

    def wrap_text_in_pre_tag(text)
      "<pre>#{text}</pre>"
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
eml_to_pdf_ext-0.6.1 lib/eml_to_pdf_ext/extraction_step.rb
eml_to_pdf_ext-0.6.0 lib/eml_to_pdf_ext/extraction_step.rb