Sha256: 08440fb340bb4743924735542242c92f9b89b5a8c70c48d2877481b392418db1

Contents?: true

Size: 1.93 KB

Versions: 14

Compression:

Stored size: 1.93 KB

Contents

require 'mail'
require 'erb'

module Rivendell::Import::Notifier
  class Mail < Rivendell::Import::Notifier::Base

    @@from = nil
    cattr_accessor :from

    attr_accessor :from, :to, :subject, :body

    def subject
      @subject ||= File.expand_path("../mail-subject.erb", __FILE__)
    end

    def body
      @body ||= File.expand_path("../mail-body.erb", __FILE__)
    end

    def from
      @from ||= self.class.from
    end

    def notify!(tasks)
      create_message(tasks).deliver!
    end

    def create_message(tasks)
      Message.new(tasks).tap do |mail|
        mail.from = from
        mail.to = to
        mail.subject = template(subject)
        mail.body = template(body)
      end
    end

    class Message

      attr_reader :tasks
      attr_accessor :from, :to, :subject, :body
      
      def initialize(tasks)
        @tasks = tasks
      end

      def completed
        tasks.select { |task| task.status.completed? }
      end

      def failed
        tasks.select { |task| task.status.failed? }
      end

      def failed?
        failed.present?
      end

      delegate :many?, :to => :tasks

      def mail
        ::Mail.new.tap do |mail|
          mail.from = from
          mail.to = to
          mail.subject = strip(render(subject))
          mail.body = render(body)
        end
      end

      def deliver!
        mail.deliver!
      end

      def render(template)
        ERB.new(template, nil, '%<>-').result(binding)
      end

      def strip(text)
        text.strip.gsub(/\n[ ]*/m,"")
      end

    end

    # def valid?
    #   [from, to, subject, body].all?(&:present?)
    # end

    def parameters
      %w{from to subject body}.inject({}) do |map, attribute|
        value = send attribute
        map[attribute] = value if value
        map
      end
    end

    def template(definition)
      if File.exists?(definition)
        File.read definition
      else
        definition
      end
    end

  end
end

Version data entries

14 entries across 14 versions & 1 rubygems

Version Path
rivendell-import-1.04 lib/rivendell/import/notifier/mail.rb
rivendell-import-1.03 lib/rivendell/import/notifier/mail.rb
rivendell-import-1.02 lib/rivendell/import/notifier/mail.rb
rivendell-import-1.01 lib/rivendell/import/notifier/mail.rb
rivendell-import-0.10 lib/rivendell/import/notifier/mail.rb
rivendell-import-0.9 lib/rivendell/import/notifier/mail.rb
rivendell-import-0.8 lib/rivendell/import/notifier/mail.rb
rivendell-import-0.7 lib/rivendell/import/notifier/mail.rb
rivendell-import-0.6 lib/rivendell/import/notifier/mail.rb
rivendell-import-0.0.5 lib/rivendell/import/notifier/mail.rb
rivendell-import-0.0.4 lib/rivendell/import/notifier/mail.rb
rivendell-import-0.0.3 lib/rivendell/import/notifier/mail.rb
rivendell-import-0.0.2 lib/rivendell/import/notifier/mail.rb
rivendell-import-0.0.1 lib/rivendell/import/notifier/mail.rb