lib/sendgrid_actionmailer.rb in sendgrid-actionmailer-2.6.0 vs lib/sendgrid_actionmailer.rb in sendgrid-actionmailer-3.0.0
- old
+ new
@@ -2,10 +2,11 @@
require 'sendgrid_actionmailer/railtie' if defined? Rails
require 'sendgrid-ruby'
module SendGridActionMailer
class DeliveryMethod
+
# TODO: use custom class to customer excpetion payload
SendgridDeliveryError = Class.new(StandardError)
include SendGrid
@@ -72,52 +73,54 @@
puts "unknown type #{input.class.name}"
end
end
def setup_personalization(mail, personalization_hash)
- p = Personalization.new
+ personalization = Personalization.new
+ personalization_hash = self.class.transform_keys(personalization_hash, &:to_s)
+
(personalization_hash['to'] || []).each do |to|
- p.add_to Email.new(email: to['email'], name: to['name'])
+ personalization.add_to Email.new(email: to['email'], name: to['name'])
end
(personalization_hash['cc'] || []).each do |cc|
- p.add_cc Email.new(email: cc['email'], name: cc['name'])
+ personalization.add_cc Email.new(email: cc['email'], name: cc['name'])
end
(personalization_hash['bcc'] || []).each do |bcc|
- p.add_bcc Email.new(email: bcc['email'], name: bcc['name'])
+ personalization.add_bcc Email.new(email: bcc['email'], name: bcc['name'])
end
(personalization_hash['headers'] || []).each do |header_key, header_value|
- p.add_header Header.new(key: header_key, value: header_value)
+ personalization.add_header Header.new(key: header_key, value: header_value)
end
(personalization_hash['substitutions'] || {}).each do |sub_key, sub_value|
- p.add_substitution(Substitution.new(key: sub_key, value: sub_value))
+ personalization.add_substitution(Substitution.new(key: sub_key, value: sub_value))
end
(personalization_hash['custom_args'] || {}).each do |arg_key, arg_value|
- p.add_custom_arg(CustomArg.new(key: arg_key, value: arg_value))
+ personalization.add_custom_arg(CustomArg.new(key: arg_key, value: arg_value))
end
if personalization_hash['send_at']
- p.send_at = personalization_hash['send_at']
+ personalization.send_at = personalization_hash['send_at']
end
if personalization_hash['subject']
- p.subject = personalization_hash['subject']
+ personalization.subject = personalization_hash['subject']
end
if mail['dynamic_template_data'] || personalization_hash['dynamic_template_data']
if mail['dynamic_template_data']
- data = json_parse(mail['dynamic_template_data'].value, false)
+ data = mail['dynamic_template_data'].unparsed_value
data.merge!(personalization_hash['dynamic_template_data'] || {})
else
data = personalization_hash['dynamic_template_data']
end
- p.add_dynamic_template_data(data)
+ personalization.add_dynamic_template_data(data)
elsif mail['template_id'].nil?
- p.add_substitution(Substitution.new(key: "%asm_group_unsubscribe_raw_url%", value: "<%asm_group_unsubscribe_raw_url%>"))
- p.add_substitution(Substitution.new(key: "%asm_global_unsubscribe_raw_url%", value: "<%asm_global_unsubscribe_raw_url%>"))
- p.add_substitution(Substitution.new(key: "%asm_preferences_raw_url%", value: "<%asm_preferences_raw_url%>"))
+ personalization.add_substitution(Substitution.new(key: "%asm_group_unsubscribe_raw_url%", value: "<%asm_group_unsubscribe_raw_url%>"))
+ personalization.add_substitution(Substitution.new(key: "%asm_global_unsubscribe_raw_url%", value: "<%asm_global_unsubscribe_raw_url%>"))
+ personalization.add_substitution(Substitution.new(key: "%asm_preferences_raw_url%", value: "<%asm_preferences_raw_url%>"))
end
- p
+ return personalization
end
def to_attachment(part)
Attachment.new.tap do |a|
a.content = Base64.strict_encode64(part.body.decoded)
@@ -140,11 +143,11 @@
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']
+ self.api_key = mail['delivery-method-options'].unparsed_value['api_key']
end
end
def add_attachments(sendgrid_mail, mail)
mail.attachments.each do |part|
@@ -170,76 +173,71 @@
add_attachments(sendgrid_mail, mail)
end
end
end
- def json_parse(text, symbolize=true)
- JSON.parse(text.empty? ? '{}' : text.gsub(/:*\"*([\%a-zA-Z0-9_-]*)\"*(( *)=>\ *)/) { "\"#{$1}\":" }, symbolize_names: symbolize)
- end
-
def add_personalizations(sendgrid_mail, mail)
+ if mail['personalizations']
+ mail['personalizations'].unparsed_value.each do |p|
+ sendgrid_mail.add_personalization(setup_personalization(mail, p))
+ end
+ end
if (mail.to && mail.to.any?) || (mail.cc && mail.cc.any?) || (mail.bcc && mail.bcc.any?)
personalization = setup_personalization(mail, {})
to_emails(mail.to).each { |to| personalization.add_to(to) }
to_emails(mail.cc).each { |cc| personalization.add_cc(cc) }
to_emails(mail.bcc).each { |bcc| personalization.add_bcc(bcc) }
sendgrid_mail.add_personalization(personalization)
end
-
- if mail['personalizations']
- personalizations = json_parse('[' + mail['personalizations'].value + ']', false)
- personalizations.each do |p|
- sendgrid_mail.add_personalization(setup_personalization(mail, p))
- end
- end
end
def add_send_options(sendgrid_mail, mail)
if mail['template_id']
sendgrid_mail.template_id = mail['template_id'].to_s
end
if mail['sections']
- json_parse(mail['sections'].value, false).each do |key, value|
+ mail['sections'].unparsed_value.each do |key, value|
sendgrid_mail.add_section(Section.new(key: key, value: value))
end
end
if mail['headers']
- json_parse(mail['headers'].value, false).each do |key, value|
+ mail['headers'].unparsed_value.each do |key, value|
sendgrid_mail.add_header(Header.new(key: key, value: value))
end
end
if mail['categories']
mail['categories'].value.split(",").each do |value|
sendgrid_mail.add_category(Category.new(name: value.strip))
end
end
if mail['custom_args']
- json_parse(mail['custom_args'].value, false).each do |key, value|
+ mail['custom_args'].unparsed_value.each do |key, value|
sendgrid_mail.add_custom_arg(CustomArg.new(key: key, value: value))
end
end
if mail['send_at']
sendgrid_mail.send_at = mail['send_at'].value.to_i
end
if mail['batch_id']
sendgrid_mail.batch_id = mail['batch_id'].to_s
end
if mail['asm']
- asm = json_parse(mail['asm'].value)
- asm = asm.delete_if { |key, value| !key.to_s.match(/(group_id)|(groups_to_display)/) }
- if asm[:group_id]
+ asm = mail['asm'].unparsed_value
+ asm = asm.delete_if { |key, value|
+ !key.to_s.match(/(group_id)|(groups_to_display)/) }
+ if asm.keys.map(&:to_s).include?('group_id')
sendgrid_mail.asm = ASM.new(asm)
end
end
if mail['ip_pool_name']
sendgrid_mail.ip_pool_name = mail['ip_pool_name'].to_s
end
end
def add_mail_settings(sendgrid_mail, mail)
if mail['mail_settings']
- settings = json_parse(mail['mail_settings'].value)
+ settings = mail['mail_settings'].unparsed_value || {}
sendgrid_mail.mail_settings = MailSettings.new.tap do |m|
if settings[:bcc]
m.bcc = BccSettings.new(settings[:bcc])
end
if settings[:bypass_list_management]
@@ -258,11 +256,11 @@
end
end
def add_tracking_settings(sendgrid_mail, mail)
if mail['tracking_settings']
- settings = json_parse(mail['tracking_settings'].value)
+ settings = mail['tracking_settings'].unparsed_value
sendgrid_mail.tracking_settings = TrackingSettings.new.tap do |t|
if settings[:click_tracking]
t.click_tracking = ClickTracking.new(settings[:click_tracking])
end
if settings[:open_tracking]
@@ -287,8 +285,20 @@
settings[:raise_delivery_errors] ? raise(SendgridDeliveryError, full_message) : warn(full_message)
end
result
+ end
+
+ # Recursive key transformation based on Rails deep_transform_values
+ def self.transform_keys(object, &block)
+ case object
+ when Hash
+ object.map { |key, value| [yield(key), transform_keys(value, &block)] }.to_h
+ when Array
+ object.map { |e| transform_keys(e, &block) }
+ else
+ object
+ end
end
end
end