Sha256: 328e2c87b2b5eb6685dd6675a1156ed2e00f904e254638785346b81d3d8ea129

Contents?: true

Size: 1.83 KB

Versions: 49

Compression:

Stored size: 1.83 KB

Contents

# coding: UTF-8

class MailFinder
  class << self

    attr_accessor :user_identity

    def find(conditions)
      filename_method = Rails::VERSION::MAJOR < 3 ? 'original_filename' : 'filename'
      ActionMailer::Base.deliveries.detect do |mail|
        mail_body = email_text_body(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(&:"#{filename_method}").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" if mail.to
          file << "Subject: #{mail.subject}\n\n"
          file << email_text_body(mail)
          file << "\n-------------------------\n\n"
        end
      end
    end

    def email_text_body(mail)
      if mail.parts.any?
        mail.parts.select { |part| part.content_type.include?('text/') }.collect(&:decoded).join("\n")
      elsif mail.body.respond_to? :raw_source
        mail.body.raw_source
      else
        mail.body
      end
    end

  end
end

Version data entries

49 entries across 49 versions & 1 rubygems

Version Path
spreewald-0.5.8 lib/spreewald_support/mail_finder.rb
spreewald-0.5.7 lib/spreewald_support/mail_finder.rb
spreewald-0.5.6 lib/spreewald_support/mail_finder.rb
spreewald-0.5.5 lib/spreewald_support/mail_finder.rb
spreewald-0.5.4 lib/spreewald_support/mail_finder.rb
spreewald-0.5.3 lib/spreewald_support/mail_finder.rb
spreewald-0.5.2 lib/spreewald_support/mail_finder.rb
spreewald-0.5.1 lib/spreewald_support/mail_finder.rb
spreewald-0.5.0 lib/spreewald_support/mail_finder.rb
spreewald-0.4.6 lib/spreewald_support/mail_finder.rb
spreewald-0.4.5 lib/spreewald_support/mail_finder.rb
spreewald-0.4.4 lib/spreewald_support/mail_finder.rb
spreewald-0.4.3 lib/spreewald_support/mail_finder.rb
spreewald-0.4.2 lib/spreewald_support/mail_finder.rb
spreewald-0.4.1 lib/spreewald_support/mail_finder.rb
spreewald-0.4.0 lib/spreewald_support/mail_finder.rb
spreewald-0.3.10 lib/spreewald_support/mail_finder.rb
spreewald-0.3.9 lib/spreewald_support/mail_finder.rb
spreewald-0.3.8 lib/spreewald_support/mail_finder.rb
spreewald-0.3.7 lib/spreewald_support/mail_finder.rb