Sha256: 3174a46c53dd098b74b67be205752504672c896b88870a5088d024b22a0306c1

Contents?: true

Size: 1.13 KB

Versions: 1

Compression:

Stored size: 1.13 KB

Contents

require "digest/sha1"
require "launchy"

module LetterOpener
  class DeliveryMethod
    class InvalidOption < StandardError; end

    attr_accessor :settings

    def initialize(options = {})
      options[:message_template] ||= LetterOpener.configuration.message_template
      options[:location] ||= LetterOpener.configuration.location

      raise InvalidOption, "A location option is required when using the Letter Opener delivery method" if options[:location].nil?

      self.settings = options
    end

    def deliver!(mail)
      validate_mail!(mail)
      location = File.join(settings[:location], "#{Time.now.to_f.to_s.tr('.', '_')}_#{Digest::SHA1.hexdigest(mail.encoded)[0..6]}")

      messages = Message.rendered_messages(mail, location: location, message_template: settings[:message_template])
      Launchy.open("file:///#{messages.first.filepath}")
    end

    private

    def validate_mail!(mail)
      if !mail.from || mail.from.empty?
        raise ArgumentError, "SMTP From address may not be blank"
      end

      if !mail.to || mail.to.empty?
        raise ArgumentError, "SMTP To address may not be blank"
      end
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
letter_opener-1.6.0 lib/letter_opener/delivery_method.rb