Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Added mail.reply_to_list support #500

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
38 changes: 33 additions & 5 deletions lib/sendgrid/helpers/mail/mail.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@
module SendGrid
class Mail
attr_accessor :subject, :ip_pool_name, :template_id, :send_at, :batch_id
attr_reader :personalizations, :contents, :attachments, :categories, :sections, :headers, :custom_args
attr_writer :from, :asm, :mail_settings, :tracking_settings, :reply_to
attr_reader :personalizations, :contents, :attachments, :categories, :sections, :headers, :custom_args, :reply_to_list
attr_writer :from, :asm, :mail_settings, :tracking_settings

# We allow for all nil values here to create uninitialized Mail objects
# (e.g. <project-root>/use-cases/transactional-templates.md)
Expand All @@ -28,6 +28,7 @@ def initialize(from_email = nil, subj = nil, to_email = nil, cont = nil) # ruboc
@mail_settings = nil
@tracking_settings = nil
@reply_to = nil
@reply_to_list = []

return if from_email.nil? && subj.nil? && to_email.nil? && cont.nil?

Expand Down Expand Up @@ -93,6 +94,16 @@ def tracking_settings
@tracking_settings.nil? ? nil : @tracking_settings.to_json
end

def reply_to=(email)
@reply_to = email
verify_reply_options
end

def reply_to_list=(emails)
@reply_to_list = emails.uniq(&:email)
verify_reply_options
end

def reply_to
@reply_to.nil? ? nil : @reply_to.to_json
end
Expand All @@ -114,9 +125,26 @@ def to_json(*)
'asm' => asm,
'ip_pool_name' => ip_pool_name,
'mail_settings' => mail_settings,
'tracking_settings' => tracking_settings,
'reply_to' => reply_to
}.delete_if { |_, value| value.to_s.strip == '' || value == [] || value == {} }
'tracking_settings' => tracking_settings
}.merge(reply_options).delete_if { |_, value| value.to_s.strip == '' || value == [] || value == {} }
end

private

def reply_options
if !@reply_to.nil?
{ 'reply_to' => reply_to }
elsif !@reply_to_list.size.zero?
'reply_to_list' => reply_to_list
else
{}
end
end

def verify_reply_options
if !@reply_to.nil? && !@reply_to_list.size.zero?
raise ArgumentError, 'You can either choose to use "reply_to" field or "reply_to_list" but not both.'
end
end
end
end
11 changes: 11 additions & 0 deletions test/sendgrid/helpers/mail/test_mail.rb
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,17 @@ def test_kitchen_sink
# rubocop:enable all
end

def test_reply_to_options
mail = SendGrid::Mail.new
mail.reply_to = Email.new(email: '[email protected]')
assert_raises(ArgumentError) { mail.reply_to_list = [Email.new(email: '[email protected]')] }
assert_equal(mail.to_json, '{}')
mail2 = SendGrid::Mail.new
mail2.reply_to_list = [Email.new(email: '[email protected]')]
assert_raises(ArgumentError) { mail2.reply_to = Email.new(email: '[email protected]') }
assert_equal(mail.to_json, '{}')
end

def test_that_personalizations_is_empty_initially
mail = SendGrid::Mail.new
assert_equal([], mail.personalizations)
Expand Down
Loading