Sha256: 27952e4d3ef19384e32783372fa3e5e0f6c60d14851614b7d3262fd052b1d7b7

Contents?: true

Size: 1.17 KB

Versions: 1

Compression:

Stored size: 1.17 KB

Contents

# frozen_string_literal: true

require 'mail'
require 'letter_opener'
require 'drill/mail/base'

module Drill
  module Mail
    class LetterOpener < Base
      attr_reader :mail, :delivery_method

      def initialize(
        params,
        mail = ::Mail.new,
        delivery_method = ::LetterOpener::DeliveryMethod.new
      )
        super(params)

        @mail = mail
        @delivery_method = delivery_method
      end

      def deliver
        prepare_mail!

        delivery_method.deliver!(mail)
      end

      alias_method :deliver_later, :deliver

      private

      def prepare_mail!
        mail.to = Array(params.to) + Array(params.cc)
        mail.from = params.from_email
        mail.sender = params.from_name
        mail.reply_to = params.reply_to
        mail.content_type = 'text/html'
        mail.body = render_html
      end

      def render_html
        template_name = params.template_name
        merge_vars =
          params.vars.each.with_object([]) do |(name, content), arr|
            arr << { name: name.to_s.upcase, content: content }
          end

        Drill.client.templates.render(template_name, [], merge_vars)['html']
      end
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
drill-mailer-0.1.1 lib/drill/mail/letter_opener.rb