Class: EmailSpec::EmailViewer

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

Class Method Summary collapse

Class Method Details

.open_in_browser(filename) ⇒ Object



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

def self.open_in_browser(filename)
  `open #{filename}`
end

.open_in_text_editor(filename) ⇒ Object

TODO: use the launchy gem for this stuff…



75
76
77
# File 'lib/tape/email_viewer.rb', line 75

def self.open_in_text_editor(filename)
  `open #{filename}`
end

.save_and_open_all_html_emailsObject



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

def self.save_and_open_all_html_emails
  all_emails.each_with_index do |m, index|
    if m.multipart? && html_part = m.parts.detect{ |p| p.content_type == '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/tape/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
# File 'lib/tape/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 == 'text/plain' }
        m.ordered_each{|k,v| f.write "#{k}: #{v}\n" }
        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



48
49
50
51
52
53
54
55
56
# File 'lib/tape/email_viewer.rb', line 48

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



58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
# File 'lib/tape/email_viewer.rb', line 58

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



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

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