Sha256: 8e467142c23a16caad71013baaf1b4b24294f7c64f6c77cdae0212f69f7a9a8e

Contents?: true

Size: 1.66 KB

Versions: 1

Compression:

Stored size: 1.66 KB

Contents

# frozen_string_literal: true

require 'drill/mail/base'

module Drill
  module Mail
    class Default < Base
      def deliver
        return if params.skip_delivery

        template_name = params.template_name

        Drill.client.messages.send_template(template_name, [], message_hash)
      end

      def deliver_later(wait: nil)
        return if params.skip_delivery

        template_name = params.template_name

        if wait
          worker.perform_in(wait.to_i, template_name, message_hash)
        else
          worker.perform_async(template_name, message_hash)
        end
      end

      private

      def worker
        Drill::DeliveryWorker
      end

      def message_hash
        message_hash = {}

        to_emails = Array(params.to).map do |email|
          { email: email }
        end
        cc_emails = Array(params.cc).map do |email|
          { email: email, type: 'cc' }
        end
        vars = Hash(params.vars).map do |name, content|
          { name: name.to_s.upcase, content: content }
        end

        all_emails = to_emails + cc_emails
        unless all_emails.empty?
          message_hash[:to] = all_emails

          unless vars.empty?
            message_hash[:merge_vars] =
              all_emails.each.with_object([]) do |email, arr|
                arr << { rcpt: email[:email], vars: vars }
              end
          end
        end
        message_hash[:from_name] = params.from_name if params.from_name
        message_hash[:from_email] = params.from_email if params.from_email
        if params.reply_to
          message_hash[:headers] = { 'Reply-To' => params.reply_to }
        end

        message_hash
      end
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
drill-mailer-0.3.0 lib/drill/mail/default.rb