Class: MailFinder

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

Defined Under Namespace

Classes: Results

Class Attribute Summary collapse

Class Method Summary collapse

Class Attribute Details

.user_identityObject

Returns the value of attribute user_identity.



9
10
11
# File 'lib/spreewald_support/mail_finder.rb', line 9

def user_identity
  @user_identity
end

Class Method Details

.email_text_body(mail, type = '') ⇒ Object



71
72
73
# File 'lib/spreewald_support/mail_finder.rb', line 71

def email_text_body(mail, type = '')
  Spreewald::MailToPlaintextConverter.new(mail, type).run
end

.expected_body_regex(expected_body) ⇒ Object



87
88
89
90
91
92
93
94
95
96
97
# File 'lib/spreewald_support/mail_finder.rb', line 87

def expected_body_regex(expected_body)
  expected = '\A\n' + Regexp.quote(expected_body.strip) + '\n\Z'
  expected.gsub! '\n\*\n', '\n[\s\S]*\n'
  expected.gsub! '\*\n', '.*\n'
  expected.gsub! '\n\*', '\n.*'

  expected.gsub! '\A\n', '\A'
  expected.gsub! '\n\Z', '\Z'

  Regexp.new(expected)
end

.find(raw_data, type = '') ⇒ Object



11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
# File 'lib/spreewald_support/mail_finder.rb', line 11

def find(raw_data, type = '')
  header, body = raw_data.split(/\n\n/, 2).map(&:strip_heredoc) # 2: maximum number of fields
  conditions = {}
  header.split("\n").each do |row|
    if row.match(/^[A-Za-z\-]+: /i)
      key, value = row.split(": ", 2)
      raise Spreewald::UnsupportedEmailHeader.new(key, value) unless Spreewald::SUPPORTED_EMAIL_HEADERS.include? key.strip
      conditions[key.strip.underscore.to_sym] = value.strip
    end
  end

  # Interpret all lines as body if there are no header-link lines
  if conditions.blank?
    body = [header, body].join("\n\n")
  end

  matching_header = ActionMailer::Base.deliveries.select do |mail|
    [ conditions[:to].nil? || mail.to.include?(resolve_email conditions[:to]),
      conditions[:cc].nil? || mail.cc && mail.cc.include?(resolve_email conditions[:cc]),
      conditions[:bcc].nil? || mail.bcc && mail.bcc.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[:attachments].nil? || conditions[:attachments].split(/\s*,\s*/).sort == attachment_filenames(mail)
    ].all?
  end

  matching_mails = if body
    body_regex = expected_body_regex(body)
    matching_header.select do |mail|
      body_regex =~ email_text_body(mail, type).strip
    end
  else
    matching_header
  end

  Results.new(matching_mails, matching_header, body_regex)
end

.header_representation(mail) ⇒ Object



58
59
60
61
62
63
64
65
66
67
68
69
# File 'lib/spreewald_support/mail_finder.rb', line 58

def header_representation(mail)
  header = ""
  header << "From: #{mail.from}\n"
  header << "Reply-To: #{mail.reply_to.join(', ')}\n" if mail.reply_to
  header << "To: #{mail.to.join(', ')}\n" if mail.to
  header << "CC: #{mail.cc.join(', ')}\n" if mail.cc
  header << "BCC: #{mail.bcc.join(', ')}\n" if mail.bcc
  header << "Subject: #{mail.subject}\n"
  header << "Attachments: #{attachment_filenames(mail).join(', ')}\n" if mail.attachments.any?

  header
end

.resolve_email(identity) ⇒ Object



50
51
52
53
54
55
56
# File 'lib/spreewald_support/mail_finder.rb', line 50

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

.show_mails(mails, only_header = false) ⇒ Object



75
76
77
78
79
80
81
82
83
84
85
# File 'lib/spreewald_support/mail_finder.rb', line 75

def show_mails(mails, only_header = false)
  message = ""
  mails.each_with_index do |mail, i|
    message << "E-Mail ##{i}\n"
    message <<  "-" * 80 + "\n"
    message << header_representation(mail)
    message << "\n" + email_text_body(mail).strip + "\n" unless only_header
    message <<  "-" * 80 + "\n\n"
  end
  message
end