Parent

LetterOpener::Message

Attributes

mail[R]

Public Class Methods

new(location, mail, part = nil) click to toggle source
# File lib/letter_opener/message.rb, line 14
def initialize(location, mail, part = nil)
  @location = location
  @mail = mail
  @part = part
  @attachments = []
end
rendered_messages(location, mail) click to toggle source
# File lib/letter_opener/message.rb, line 5
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

Public Instance Methods

<=>(other) click to toggle source
# File lib/letter_opener/message.rb, line 97
def <=>(other)
  order = ]rich plain]
  order.index(type) <=> order.index(other.type)
end
body() click to toggle source
# File lib/letter_opener/message.rb, line 55
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
content_type() click to toggle source
# File lib/letter_opener/message.rb, line 51
def content_type
  @part && @part.content_type || @mail.content_type
end
encoding() click to toggle source
# File lib/letter_opener/message.rb, line 83
def encoding
  body.respond_to?(:encoding) ? body.encoding : "utf-8"
end
filepath() click to toggle source
# File lib/letter_opener/message.rb, line 47
def filepath
  File.join(@location, "#{type}.html")
end
from() click to toggle source
# File lib/letter_opener/message.rb, line 67
def from
  @from ||= Array(@mail.from).join(", ")
end
h(content) click to toggle source
# File lib/letter_opener/message.rb, line 93
def h(content)
  CGI.escapeHTML(content)
end
render() click to toggle source
# File lib/letter_opener/message.rb, line 21
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|
      path = File.join(attachments_dir, attachment.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(attachment.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 75
def reply_to
  @reply_to ||= Array(@mail.reply_to).join(", ")
end
template() click to toggle source
# File lib/letter_opener/message.rb, line 43
def template
  File.read(File.expand_path("../message.html.erb", __FILE__))
end
to() click to toggle source
# File lib/letter_opener/message.rb, line 71
def to
  @to ||= Array(@mail.to).join(", ")
end
type() click to toggle source
# File lib/letter_opener/message.rb, line 79
def type
  content_type =~ /html/ ? "rich" : "plain"
end

[Validate]

Generated with the Darkfish Rdoc Generator 2.