Class: LetterOpener::Message

Inherits:
Object
  • Object
show all
Defined in:
lib/letter_opener/message.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(location, mail, part = nil) ⇒ Message

Returns a new instance of Message.



16
17
18
19
20
21
# File 'lib/letter_opener/message.rb', line 16

def initialize(location, mail, part = nil)
  @location = location
  @mail = mail
  @part = part
  @attachments = []
end

Instance Attribute Details

#mailObject (readonly)

Returns the value of attribute mail.



5
6
7
# File 'lib/letter_opener/message.rb', line 5

def mail
  @mail
end

Class Method Details

.rendered_messages(location, mail) ⇒ Object



7
8
9
10
11
12
13
14
# File 'lib/letter_opener/message.rb', line 7

def self.rendered_messages(location, mail)
  messages = []
  messages << new(location, mail, mail.html_part) if mail.html_part
  messages << new(location, mail, mail.text_part) if mail.text_part
  messages << new(location, mail) if messages.empty?
  messages.each(&:render)
  messages.sort
end

Instance Method Details

#<=>(other) ⇒ Object



112
113
114
115
# File 'lib/letter_opener/message.rb', line 112

def <=>(other)
  order = %w[rich plain]
  order.index(type) <=> order.index(other.type)
end


102
103
104
105
106
# File 'lib/letter_opener/message.rb', line 102

def auto_link(text)
  text.gsub(URI.regexp(%W[https http])) do |link|
    "<a href=\"#{ link }\">#{ link }</a>"
  end
end

#bccObject



86
87
88
# File 'lib/letter_opener/message.rb', line 86

def bcc
  @bcc ||= Array(@mail['bcc']).join(", ")
end

#bodyObject



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

def body
  @body ||= begin
    body = (@part && @part.body || @mail.body).to_s

    mail.attachments.each do |attachment|
      body.gsub!(attachment.url, "attachments/#{attachment.filename}")
    end

    body
  end
end

#ccObject



82
83
84
# File 'lib/letter_opener/message.rb', line 82

def cc
  @cc ||= Array(@mail['cc']).join(", ")
end

#content_typeObject



54
55
56
# File 'lib/letter_opener/message.rb', line 54

def content_type
  @part && @part.content_type || @mail.content_type
end

#encodingObject



98
99
100
# File 'lib/letter_opener/message.rb', line 98

def encoding
  body.respond_to?(:encoding) ? body.encoding : "utf-8"
end

#filepathObject



50
51
52
# File 'lib/letter_opener/message.rb', line 50

def filepath
  File.join(@location, "#{type}.html")
end

#fromObject



70
71
72
# File 'lib/letter_opener/message.rb', line 70

def from
  @from ||= Array(@mail['from']).join(", ")
end

#h(content) ⇒ Object



108
109
110
# File 'lib/letter_opener/message.rb', line 108

def h(content)
  CGI.escapeHTML(content)
end

#renderObject



23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
# File 'lib/letter_opener/message.rb', line 23

def render
  FileUtils.mkdir_p(@location)

  if mail.attachments.any?
    attachments_dir = File.join(@location, 'attachments')
    FileUtils.mkdir_p(attachments_dir)
    mail.attachments.each do |attachment|
      filename = attachment.filename.gsub(/[^\w.]/, '_')
      path = File.join(attachments_dir, filename)

      unless File.exists?(path) # true if other parts have already been rendered
        File.open(path, 'wb') { |f| f.write(attachment.body.raw_source) }
      end

      @attachments << [attachment.filename, "attachments/#{URI.escape(filename)}"]
    end
  end

  File.open(filepath, 'w') do |f|
    f.write ERB.new(template).result(binding)
  end
end

#reply_toObject



90
91
92
# File 'lib/letter_opener/message.rb', line 90

def reply_to
  @reply_to ||= Array(@mail['reply-to']).join(", ")
end

#senderObject



74
75
76
# File 'lib/letter_opener/message.rb', line 74

def sender
  @sender ||= Array(@mail['sender']).join(", ")
end

#templateObject



46
47
48
# File 'lib/letter_opener/message.rb', line 46

def template
  File.read(File.expand_path("../message.html.erb", __FILE__))
end

#toObject



78
79
80
# File 'lib/letter_opener/message.rb', line 78

def to
  @to ||= Array(@mail['to']).join(", ")
end

#typeObject



94
95
96
# File 'lib/letter_opener/message.rb', line 94

def type
  content_type =~ /html/ ? "rich" : "plain"
end