Class: MailFinder

Inherits:
Object
  • Object
show all
Defined in:
lib/spreewald_support/mail_finder.rb

Class Attribute Summary collapse

Class Method Summary collapse

Class Attribute Details

.user_identityObject

Returns the value of attribute user_identity.



6
7
8
# File 'lib/spreewald_support/mail_finder.rb', line 6

def user_identity
  @user_identity
end

Class Method Details

.email_text_body(mail) ⇒ Object



46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
# File 'lib/spreewald_support/mail_finder.rb', line 46

def email_text_body(mail)
  if mail.parts.any?
    mail_bodies = mail.parts.map { |part|
      if part.header.to_s.include?('Quoted-printable')
        if Rails.version.starts_with?('2.3')
          part.body.to_s
        else
          part.decoded
        end
      else
        part.decoded
      end
    }
    mail_bodies.join('\n')

  elsif mail.body.respond_to? :raw_source
    mail.body.raw_source
  else
    mail.body
  end
end

.find(conditions) ⇒ Object



8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
# File 'lib/spreewald_support/mail_finder.rb', line 8

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[:reply_to].nil? || mail.reply_to.include?(resolve_email conditions[:reply_to]),
      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

.log(mail) ⇒ Object



34
35
36
37
38
39
40
41
42
43
44
# File 'lib/spreewald_support/mail_finder.rb', line 34

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

.resolve_email(identity) ⇒ Object



26
27
28
29
30
31
32
# File 'lib/spreewald_support/mail_finder.rb', line 26

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