Sha256: e3f5cf5f3345f8cd7d8c7b324f90fd371cd3198e4a602bc1aef38d9ac9e1b5d7

Contents?: true

Size: 1.12 KB

Versions: 2

Compression:

Stored size: 1.12 KB

Contents

# frozen_string_literal: true

module Mail
  module Notify
    class DeliveryMethod
      attr_accessor :settings, :response

      def initialize(settings)
        @settings = settings
      end

      def deliver!(mail)
        @mail = mail
        @personalisation = Personalisation.new(mail)
        send_email
      end

      def preview(mail)
        personalisation = Personalisation.new(mail).to_h
        template_id = mail[:template_id].to_s
        client.generate_template_preview(template_id, personalisation: personalisation)
      end

      private

      def client
        @client ||= Notifications::Client.new(@settings[:api_key])
      end

      def email_params
        {
          email_address: @mail.to.first,
          template_id: @mail[:template_id].to_s,
          personalisation: @personalisation.to_h,
          email_reply_to_id: optional_param(:reply_to_id),
          reference: optional_param(:reference)
        }
      end

      def optional_param(name)
        @mail[name].presence&.to_s
      end

      def send_email
        @response = client.send_email(email_params.compact)
      end
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
mail-notify-0.2.1 lib/mail/notify/delivery_method.rb
mail-notify-0.2.0 lib/mail/notify/delivery_method.rb