Sha256: 877d0085faef9d7834c9c5ed6669244e1e2a93e71af3fa8f8aff4ff5c64d1afc

Contents?: true

Size: 1.23 KB

Versions: 7

Compression:

Stored size: 1.23 KB

Contents

# frozen_string_literal: true

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

      def initialize(settings)
        raise ArgumentError, "You must specify an API key" if settings[:api_key].blank?

        @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], @settings[:base_url])
      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

7 entries across 7 versions & 1 rubygems

Version Path
mail-notify-1.2.0 lib/mail/notify/delivery_method.rb
mail-notify-1.1.0 lib/mail/notify/delivery_method.rb
mail-notify-1.0.5 lib/mail/notify/delivery_method.rb
mail-notify-1.0.4 lib/mail/notify/delivery_method.rb
mail-notify-1.0.3 lib/mail/notify/delivery_method.rb
mail-notify-1.0.2 lib/mail/notify/delivery_method.rb
mail-notify-1.0.1 lib/mail/notify/delivery_method.rb