Class: EmailSpec::EmailViewer

Inherits:
Object
  • Object
show all
Extended by:
Deliveries
Defined in:
lib/email_spec/email_viewer.rb

Class Method Summary collapse

Methods included from Deliveries

all_emails, last_email_sent, mailbox_for, reset_mailer

Class Method Details

.open_in_browser(filename) ⇒ Object



83
84
85
# File 'lib/email_spec/email_viewer.rb', line 83

def self.open_in_browser(filename)
  Launchy.open(URI.parse("file://#{File.expand_path(filename)}"))
end

.open_in_text_editor(filename) ⇒ Object



79
80
81
# File 'lib/email_spec/email_viewer.rb', line 79

def self.open_in_text_editor(filename)
  Launchy.open(URI.parse("file://#{File.expand_path(filename)}"), :application => :editor)
end

.save_and_open_all_html_emailsObject



18
19
20
21
22
23
24
25
26
27
28
# File 'lib/email_spec/email_viewer.rb', line 18

def self.save_and_open_all_html_emails
  all_emails.each_with_index do |m, index|
    if m.multipart? && m.parts.detect{ |p| p.content_type.include?('text/html') }
      filename = tmp_email_filename("-#{index}.html")
      File.open(filename, "w") do |f|
        f.write m.parts[1].body
      end
      open_in_browser(filename)
    end
  end
end

.save_and_open_all_raw_emailsObject



5
6
7
8
9
10
11
12
13
14
15
16
# File 'lib/email_spec/email_viewer.rb', line 5

def self.save_and_open_all_raw_emails
  filename = tmp_email_filename

  File.open(filename, "w") do |f|
    all_emails.each do |m|
      f.write m.to_s
      f.write "\n" + '='*80 + "\n"
    end
  end

  open_in_text_editor(filename)
end

.save_and_open_all_text_emailsObject



30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
# File 'lib/email_spec/email_viewer.rb', line 30

def self.save_and_open_all_text_emails
  filename = tmp_email_filename

  File.open(filename, "w") do |f|
    all_emails.each do |m|
      if m.multipart? && text_part = m.parts.detect{ |p| p.content_type.include?('text/plain') }
        if m.respond_to?(:ordered_each) # Rails 2 / TMail
          m.ordered_each{|k,v| f.write "#{k}: #{v}\n" }
        else # Rails 4 / Mail
          f.write(text_part.header.to_s + "\n")
        end

        f.write text_part.body
      else
        f.write m.to_s
      end
      f.write "\n" + '='*80 + "\n"
    end
  end

  open_in_text_editor(filename)
end

.save_and_open_email(mail) ⇒ Object



53
54
55
56
57
58
59
60
61
# File 'lib/email_spec/email_viewer.rb', line 53

def self.save_and_open_email(mail)
  filename = tmp_email_filename

  File.open(filename, "w") do |f|
    f.write mail.to_s
  end

  open_in_text_editor(filename)
end

.save_and_open_email_attachments_list(mail) ⇒ Object



63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
# File 'lib/email_spec/email_viewer.rb', line 63

def self.save_and_open_email_attachments_list(mail)
  filename = tmp_email_filename

  File.open(filename, "w") do |f|
    mail.attachments.each_with_index do |attachment, index|
      info = "#{index + 1}:"
      info += "\n\tfilename: #{attachment.original_filename}"
      info += "\n\tcontent type: #{attachment.content_type}"
      info += "\n\tsize: #{attachment.size}"
      f.write info + "\n"
    end
  end

  open_in_text_editor(filename)
end

.tmp_email_filename(extension = '.txt') ⇒ Object



87
88
89
# File 'lib/email_spec/email_viewer.rb', line 87

def self.tmp_email_filename(extension = '.txt')
  "#{Rails.root}/tmp/email-#{Time.now.to_i}#{extension}"
end