Sha256: b3feba4cfe168bfdf73b9f3d3006b63f201e1590cd44e4b9c190234fc4262b40

Contents?: true

Size: 1.72 KB

Versions: 1

Compression:

Stored size: 1.72 KB

Contents

# frozen_string_literal: true

module Mailpeek
  # Public: Wrapper class for mail object
  class Email
    attr_reader(
      :id, :position, :mail, :html, :text, :attachments, :to, :from, :subject,
      :message_id, :date
    )

    def initialize(timestamp, mail)
      @id          = timestamp
      @position    = timestamp
      @mail        = mail
      @to          = mail[:to].addrs.map(&:format)
      @from        = mail[:from].addrs.map(&:format)
      @subject     = mail.subject
      @message_id  = mail.message_id
      @date        = mail.date
      @attachments = []

      mail.multipart? ? parse_parts : parse_body
    end

    def match?(query)
      subject&.match(query) || text&.match(query) || html&.match(query)
    end

    def destroy
      FileUtils.rm_rf("#{Mailpeek.configuration.location}/#{id}")
    end

    def read
      File.exist?(read_file_path)
    end

    def read=(value)
      if value && !read
        FileUtils.touch(read_file_path)
      elsif !value && read
        File.delete(read_file_path)
      end
    end

    private

    def read_file_path
      File.join(Mailpeek.configuration.location, id, '.read')
    end

    def parse_parts
      if mail.html_part
        @html = mail.html_part.body.decoded.force_encoding('utf-8')
      end

      if mail.text_part
        @text = mail.text_part.body.decoded.force_encoding('utf-8')
      end

      return unless mail.attachments.any?

      mail.attachments.each do |attachment|
        @attachments.push(attachment.filename.gsub(/[^\w\-_.]/, '_'))
      end
    end

    def parse_body
      body = mail.body.decoded.force_encoding('utf-8')

      if mail.content_type =~ /html/
        @html = body
      else
        @text = body
      end
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
mailpeek-1.0.3 lib/mailpeek/email.rb