class LetterOpener::Message

Constants

ERROR_MSG

Attributes

mail[R]

Public Class Methods

new(mail, options = {}) click to toggle source
# 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
rendered_messages(mail, options = {}) click to toggle source
# 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

Public Instance Methods

<=>(other) click to toggle source
# 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) click to toggle source
# File lib/letter_opener/message.rb, line 121
def attachment_filename(attachment)
  attachment.filename.gsub(/[^\w\-.]/, '_')
end
bcc() click to toggle source
# File lib/letter_opener/message.rb, line 95
def bcc
  @bcc ||= Array(@mail['bcc']).join(", ")
end
body() click to toggle source
# 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
cc() click to toggle source
# File lib/letter_opener/message.rb, line 91
def cc
  @cc ||= Array(@mail['cc']).join(", ")
end
content_type() click to toggle source
# File lib/letter_opener/message.rb, line 63
def content_type
  @part && @part.content_type || @mail.content_type
end
encoding() click to toggle source
# File lib/letter_opener/message.rb, line 107
def encoding
  body.respond_to?(:encoding) ? body.encoding : "utf-8"
end
filepath() click to toggle source
# File lib/letter_opener/message.rb, line 59
def filepath
  File.join(@location, "#{type}.html")
end
from() click to toggle source
# File lib/letter_opener/message.rb, line 79
def from
  @from ||= Array(@mail['from']).join(", ")
end
h(content) click to toggle source
# File lib/letter_opener/message.rb, line 117
def h(content)
  CGI.escapeHTML(content)
end
render() click to toggle source
# 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_to() click to toggle source
# File lib/letter_opener/message.rb, line 99
def reply_to
  @reply_to ||= Array(@mail['reply-to']).join(", ")
end
sender() click to toggle source
# File lib/letter_opener/message.rb, line 83
def sender
  @sender ||= Array(@mail['sender']).join(", ")
end
template() click to toggle source
# File lib/letter_opener/message.rb, line 55
def template
  File.read(File.expand_path("../templates/#{@template}.html.erb", __FILE__))
end
to() click to toggle source
# File lib/letter_opener/message.rb, line 87
def to
  @to ||= Array(@mail['to']).join(", ")
end
type() click to toggle source
# File lib/letter_opener/message.rb, line 103
def type
  content_type =~ /html/ ? "rich" : "plain"
end