Sha256: c857ec54363736173b0b173954318a3a3d415635d7ce349ec889fee4d6330eb2

Contents?: true

Size: 1.09 KB

Versions: 2

Compression:

Stored size: 1.09 KB

Contents

require 'rspec/matchers'
require 'howitzer/utils/email/mail_client'

class Email
  include RSpec::Matchers
  attr_reader :recipient_address

  def initialize(message)
    message.subject.should include(self.class::SUBJECT)
    @recipient_address = ::Mail::Address.new(message.to.first)
    @message = message
  end

  def self.find_by_recipient(recipient)
    find(recipient, self::SUBJECT)
  end

  def self.find(recipient, subject)
      messages = MailClient.by_email(recipient).find_mail do |mail|
       /#{Regexp.escape(subject)}/ === mail.subject && mail.to == [recipient]
    end

    if messages.first.nil?
      log.error "#{self} was not found (recipient: '#{recipient}')"
      messages.first.should_not be_nil
    end
    new(messages.first)
  end

  def plain_text_body
    get_mime_part(@message, 'text/plain').to_s
  end

  def get_mime_part(part, type)
    return part.body if part["content-type"].to_s =~ %r!#{type}!
    # Recurse the multi-parts
    part.parts.each do |sub_part|
      r = get_mime_part(sub_part, type)
      return r if r
    end
    nil
  end

  protected :get_mime_part

end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
howitzer-0.0.3 lib/howitzer/utils/email/email.rb
howitzer-0.0.1 lib/howitzer/utils/email/email.rb