class Goatmail::Message

Attributes

id[R]
meta[R]
sent_at[R]

Public Class Methods

bulk_delete(ids) click to toggle source
# File lib/goatmail/message.rb, line 20
def bulk_delete(ids)
  ids.to_a.each do |id|
    message_path = File.join(location, id)
    FileUtils.rm_rf(message_path) if File.exist?(message_path)
  end
end
find(id) click to toggle source
# File lib/goatmail/message.rb, line 16
def find(id)
  new id: id
end
load_all() click to toggle source
# File lib/goatmail/message.rb, line 10
def load_all
  Dir.glob("#{location}/*").map { |f|
    new id: File.basename(f), sent_at: File.mtime(f)
  }.sort_by(&:sent_at).reverse
end
location() click to toggle source
# File lib/goatmail/message.rb, line 6
def location
  Goatmail.location
end
new(args={}) click to toggle source
# File lib/goatmail/message.rb, line 31
def initialize(args={})
  @id       = args.fetch(:id)
  @sent_at  = args[:sent_at] # optional
  meta_file = File.join(base_dir, 'meta')
  meta_data = File.exist?(meta_file) ? File.read(meta_file) : ''
  @meta     = meta_data.empty? ? {} : Marshal.load(meta_data)
end

Public Instance Methods

attachments() click to toggle source
# File lib/goatmail/message.rb, line 48
def attachments
  @attachments ||= Dir["#{base_dir}/attachments/*"].each_with_object({}) do |file, hash|
    hash[File.basename(file)] = File.expand_path(file)
  end
end
path() click to toggle source
# File lib/goatmail/message.rb, line 54
def path
  File.exist?(File.join(base_dir, "rich.html")) ? "#{id}.html" : id
end
render(format=nil) click to toggle source
# File lib/goatmail/message.rb, line 39
def render(format=nil)
  type = :plain if format.nil?
  type = :rich  if format.to_s == 'html'
  raise ArgumentError if type.nil?
  message_file = File.join(base_dir, "#{type}.html")
  raise ArgumentError unless File.exist?(message_file)
  File.read(message_file)
end

Private Instance Methods

base_dir() click to toggle source
# File lib/goatmail/message.rb, line 60
def base_dir
  "#{self.class.location}/#{id}"
end