Class: LetterOpener::Message

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

Constant Summary collapse

ERROR_MSG =
'%s or default configuration must be given'.freeze

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(mail, options = {}) ⇒ Message

Returns a new instance of Message.

Raises:

  • (ArgumentError)

21
22
23
24
25
26
27
28
29
30
# File 'lib/letter_opener/message.rb', line 21

def initialize(mail, options = {})
  @mail = mail
  @location = options[:location] || LetterOpener.configuration.location
  @part = options[:part]
  @template = options[:message_template] || LetterOpener.configuration.message_template
  @attachments = []

  raise ArgumentError, ERROR_MSG % 'options[:location]' unless @location
  raise ArgumentError, ERROR_MSG % 'options[:message_template]' unless @template
end

Instance Attribute Details

#mailObject (readonly)

Returns the value of attribute mail


8
9
10
# File 'lib/letter_opener/message.rb', line 8

def mail
  @mail
end

Class Method Details

.rendered_messages(mail, options = {}) ⇒ Object


10
11
12
13
14
15
16
17
# File 'lib/letter_opener/message.rb', line 10

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

Instance Method Details

#<=>(other) ⇒ Object


125
126
127
128
# File 'lib/letter_opener/message.rb', line 125

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

#attachment_filename(attachment) ⇒ Object


121
122
123
# File 'lib/letter_opener/message.rb', line 121

def attachment_filename(attachment)
  attachment.filename.gsub(/[^\w\-.]/, '_')
end

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

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

#bccObject


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

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

#bodyObject


67
68
69
70
71
72
73
74
75
76
77
# File 'lib/letter_opener/message.rb', line 67

def body
  @body ||= begin
    body = (@part || @mail).decoded

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

    body
  end
end

#ccObject


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

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

#content_typeObject


63
64
65
# File 'lib/letter_opener/message.rb', line 63

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

#encodingObject


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

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

#filepathObject


59
60
61
# File 'lib/letter_opener/message.rb', line 59

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

#fromObject


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

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

#h(content) ⇒ Object


117
118
119
# File 'lib/letter_opener/message.rb', line 117

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

#renderObject


32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
# File 'lib/letter_opener/message.rb', line 32

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(attachment)
      path = File.join(attachments_dir, filename)

      unless File.exist?(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/#{CGI.escape(filename)}"]
    end
  end

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

#reply_toObject


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

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

#senderObject


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

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

#templateObject


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

def template
  File.read(File.expand_path("../templates/#{@template}.html.erb", __FILE__))
end

#toObject


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

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

#typeObject


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

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