Sha256: 662ac7dbff01046ebc553fd09b004ec2a123041ad706670ee4071edd9b9d030c

Contents?: true

Size: 1.57 KB

Versions: 9

Compression:

Stored size: 1.57 KB

Contents

module SendGridMailer
  module Interceptor
    module RecipientInterceptor
      def self.perform(sg_definition, interceptor)
        add_custom_headers(sg_definition)
        add_recipients(sg_definition, interceptor)
        add_subject_prefix(sg_definition, interceptor)
      end

      def self.exec_recipients_interceptor(sg_definition, interceptor)
        add_custom_headers(sg_definition)
        add_recipients(sg_definition, interceptor)
        add_subject_prefix(sg_definition, interceptor)
      end

      def self.add_subject_prefix(sg_definition, interceptor)
        subject_prefix = interceptor.instance_variable_get(:@subject_prefix)
        subject = [subject_prefix, sg_definition.personalization.subject].join(" ").strip
        sg_definition.set_subject(subject)
      end

      def self.add_recipients(sg_definition, interceptor)
        recipients = interceptor.instance_variable_get(:@recipients)
        sg_definition.clean_recipients(:to)
        sg_definition.clean_recipients(:cc)
        sg_definition.clean_recipients(:bcc)
        sg_definition.set_recipients(:to, recipients)
      end

      def self.add_custom_headers(sg_definition)
        {
          'X-Intercepted-To' => sg_definition.personalization.tos || [],
          'X-Intercepted-Cc' => sg_definition.personalization.ccs || [],
          'X-Intercepted-Bcc' => sg_definition.personalization.bccs || []
        }.each do |header, addresses|
          addresses_str = addresses.map { |a| a["email"] }.join(", ")
          sg_definition.add_header(header, addresses_str)
        end
      end
    end
  end
end

Version data entries

9 entries across 9 versions & 1 rubygems

Version Path
send_grid_mailer-2.4.0 lib/send_grid_mailer/interceptor/recipient_interceptor.rb
send_grid_mailer-2.3.0 lib/send_grid_mailer/interceptor/recipient_interceptor.rb
send_grid_mailer-2.2.0 lib/send_grid_mailer/interceptor/recipient_interceptor.rb
send_grid_mailer-2.0.1 lib/send_grid_mailer/interceptor/recipient_interceptor.rb
send_grid_mailer-2.0.0 lib/send_grid_mailer/interceptor/recipient_interceptor.rb
send_grid_mailer-1.2.1 lib/send_grid_mailer/interceptor/recipient_interceptor.rb
send_grid_mailer-1.2.0 lib/send_grid_mailer/interceptor/recipient_interceptor.rb
send_grid_mailer-1.1.0 lib/send_grid_mailer/interceptor/recipient_interceptor.rb
send_grid_mailer-1.0.0 lib/send_grid_mailer/interceptor/recipient_interceptor.rb