Sha256: 85dcc06e90b37275cea5af9e62d1566ee7126453b0baef154cb3adf0912e0218

Contents?: true

Size: 1.39 KB

Versions: 9

Compression:

Stored size: 1.39 KB

Contents

class MailFinder
  class << self

    attr_accessor :user_identity

    def find(conditions)
      ActionMailer::Base.deliveries.detect do |mail|
        [ conditions[:to].nil? || mail.to.include?(resolve_email conditions[:to]),
          conditions[:cc].nil? || mail.cc.andand.include?(resolve_email conditions[:cc]),
          conditions[:bcc].nil? || mail.bcc.andand.include?(resolve_email conditions[:bcc]),
          conditions[:from].nil? || mail.from.include?(resolve_email conditions[:from]),
          conditions[:subject].nil? || mail.subject.include?(conditions[:subject]),
          conditions[:body].nil? || mail.body.include?(conditions[:body]),
          conditions[:attachments].nil? || conditions[:attachments].split(/\s*,\s*/).sort == Array(mail.attachments).collect(&:original_filename).sort
        ].all?
      end.tap do |mail|
        log(mail)
      end
    end

    def resolve_email(identity)
      if identity =~ /^.+\@.+$/
        identity
      else
        User.send("find_by_#{user_identity || 'email'}!", identity).email
      end
    end

    def log(mail)
      if mail.present?
        File.open("log/test_mails.log", "a") do |file|
          file << "From: #{mail.from}\n"
          file << "To: #{mail.to.join(', ')}\n"
          file << "Subject: #{mail.subject}\n\n"
          file << mail.body
          file << "\n-------------------------\n\n"
        end
      end
    end

  end
end

Version data entries

9 entries across 9 versions & 1 rubygems

Version Path
spreewald-0.1.4 lib/spreewald_support/mail_finder.rb
spreewald-0.1.3 lib/spreewald_support/mail_finder.rb
spreewald-0.1.2 lib/spreewald_support/mail_finder.rb
spreewald-0.1.1 lib/spreewald_support/mail_finder.rb
spreewald-0.1.0 lib/spreewald_support/mail_finder.rb
spreewald-0.0.4 lib/spreewald_support/mail_finder.rb
spreewald-0.0.3 lib/spreewald_support/mail_finder.rb
spreewald-0.0.2 lib/spreewald_support/mail_finder.rb
spreewald-0.0.1 lib/spreewald_support/mail_finder.rb