Sha256: 632243c0f1bf68e0d2504e923b51faaf5e844643a49880bc99cf22c8bfaf1daf

Contents?: true

Size: 1.29 KB

Versions: 4

Compression:

Stored size: 1.29 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
      options[:file_uri_scheme] ||= LetterOpener.configuration.file_uri_scheme

      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("#{settings[:file_uri_scheme]}#{messages.first.filepath}")
    end

    private

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

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

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
letter_opener-1.10.0 lib/letter_opener/delivery_method.rb
letter_opener-1.9.0 lib/letter_opener/delivery_method.rb
letter_opener-1.8.1 lib/letter_opener/delivery_method.rb
letter_opener-1.8.0 lib/letter_opener/delivery_method.rb