Sha256: 24b318e78382e86c3940b29304b85f2986b769b960a17fe198af5bdcfcda9f6d

Contents?: true

Size: 1.48 KB

Versions: 4

Compression:

Stored size: 1.48 KB

Contents

require 'yaml'
module LetterOpener
  module Web
    class Message

      class << self

        def location
          LetterOpener.location
        end

        def load_all
          Dir.glob("#{location}/*").map { |f|
            new :id => File.basename(f), :sent_at => File.mtime(f)
          }.sort_by(&:sent_at).reverse
        end

        def find(id)
          new :id => id
        end

        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

      end

      attr_reader :id, :sent_at, :info

      def initialize(args={})
        @id       = args.fetch(:id)
        @sent_at  = args[:sent_at] # optional
        info = File.join(base_dir, 'info.yml')
        @info     = File.exist?(info) ?  YAML.load_file(info) : {}
      end

      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

      def attachments
        @attachments ||= Dir["#{base_dir}/attachments/*"].each_with_object({}) do |file, hash|
          hash[File.basename(file)] = File.expand_path(file)
        end
      end

      private

      def base_dir
        "#{self.class.location}/#{id}"
      end

    end
  end
end

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
letter_opener-web-0.2.0 lib/letter_opener/web/message.rb
letter_opener-web-0.1.2 lib/letter_opener/web/message.rb
letter_opener-web-0.1.1 lib/letter_opener/web/message.rb
letter_opener-web-0.1.0 lib/letter_opener/web/message.rb