lib/sendgrid_actionmailer.rb in sendgrid-actionmailer-2.0.0 vs lib/sendgrid_actionmailer.rb in sendgrid-actionmailer-2.0.1

- old
+ new

@@ -8,14 +8,14 @@ SendgridDeliveryError = Class.new(StandardError) include SendGrid DEFAULTS = { - raise_delivery_errors: false, - } + raise_delivery_errors: false + }.freeze - attr_accessor :settings + attr_accessor :settings, :api_key def initialize(**params) self.settings = DEFAULTS.merge(params) end @@ -26,10 +26,11 @@ m.subject = mail.subject # https://sendgrid.com/docs/Classroom/Send/v3_Mail_Send/personalizations.html m.add_personalization(to_personalizations(mail)) end + add_api_key(sendgrid_mail, mail) add_content(sendgrid_mail, mail) add_send_options(sendgrid_mail, mail) add_mail_settings(sendgrid_mail, mail) add_tracking_settings(sendgrid_mail, mail) @@ -39,11 +40,11 @@ end private def client - @client ||= SendGrid::API.new(api_key: settings.fetch(:api_key)).client + @client = SendGrid::API.new(api_key: api_key).client end # type should be either :plain or :html def to_content(type, value) Content.new(type: "text/#{type}", value: value) @@ -101,9 +102,16 @@ def get_disposition(message) return if message.header.nil? content_disp = message.header[:content_disposition] return unless content_disp.respond_to?(:disposition_type) content_disp.disposition_type + end + + def add_api_key(sendgrid_mail, mail) + self.api_key = settings.fetch(:api_key) + if mail['delivery-method-options'] && mail['delivery-method-options'].value.include?('api_key') + self.api_key = JSON.parse(mail['delivery-method-options'].value.gsub('=>', ':'))['api_key'] + end end def add_content(sendgrid_mail, mail) case mail.mime_type when 'text/plain'