module LetterOpenerWebS3::MessageExtension

Public Class Methods

new(location, mail, part = nil) click to toggle source
# File lib/letter_opener_web_s3/message_extension.rb, line 5
def initialize(location, mail, part = nil)
  @location = location.gsub("#{Rails.root.to_s}/", '')
  @mail = mail
  @part = part
  @attachments = []
end

Public Instance Methods

body() click to toggle source
# File lib/letter_opener_web_s3/message_extension.rb, line 34
def body
  @body ||= begin
    body = (@part || @mail).decoded

    mail.attachments.each do |attachment|
      item = @attachments.select{|filename, _| filename == attachment.filename}.first
      body.gsub!(attachment.url, item.last)
    end

    body
  end
end
object(filepath) click to toggle source
# File lib/letter_opener_web_s3/message_extension.rb, line 49
def object(filepath)
  LetterOpenerWebS3.bucket.object(filepath)
end
render() click to toggle source
# File lib/letter_opener_web_s3/message_extension.rb, line 12
def render
  if mail.attachments.any?
    attachments_dir = File.join(@location, 'attachments')
    mail.attachments.each do |attachment|
      filename = attachment.filename.gsub(/[^\w.]/, '_')
      path = File.join(attachments_dir, filename)

      object(path).put(body: attachment.body.raw_source, content_length: attachment.body.raw_source.size)

      @attachments << [attachment.filename, URI.escape(object(path).public_url)]
    end
  end

  str = ERB.new(template).result(binding)
  object(filepath).put(body: str, content_length: str.size)
end
template() click to toggle source
# File lib/letter_opener_web_s3/message_extension.rb, line 29
def template
  letter_opener_path = $".select{|f| f.match(/letter_opener\/message.rb/)}.first
  File.read(File.join(letter_opener_path.gsub('message.rb', ''), 'message.html.erb'))
end