Skip to content

Commit

Permalink
Support for dynamic template data.
Browse files Browse the repository at this point in the history
  • Loading branch information
nedcampion committed Jul 25, 2018
1 parent 31677ea commit 3705080
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 1 deletion.
18 changes: 18 additions & 0 deletions examples/helpers/mail/example.rb
Original file line number Diff line number Diff line change
Expand Up @@ -125,5 +125,23 @@ def kitchen_sink
puts response.headers
end

def dynamic_template_data_hello_world
mail = Mail.new
mail.template_id = '' # a non-legacy template id
mail.from = Email.new(email: '[email protected]')
subject = 'Dynamic Template Data Hello World from the SendGrid Ruby Library'
mail.subject = subject
personalization = Personalization.new
personalization.add_to(Email.new(email: '[email protected]', name: 'Example User'))
personalization.add_dynamic_template_data({
"variable" => [
{"foo" => "bar"}, {"foo" => "baz"}
]
})
mail.add_personalization(personalization)
end

hello_world
kitchen_sink
dynamic_template_data_hello_world

9 changes: 8 additions & 1 deletion lib/sendgrid/helpers/mail/personalization.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,8 @@
module SendGrid
class Personalization

attr_reader :tos, :ccs, :bccs, :headers, :substitutions, :custom_args
attr_reader :tos, :ccs, :bccs, :headers, :substitutions, :custom_args,
:dynamic_template_data

def initialize
@tos = []
Expand All @@ -13,6 +14,7 @@ def initialize
@headers = {}
@substitutions = {}
@custom_args = {}
@dynamic_template_data = {}
@send_at = nil
end

Expand Down Expand Up @@ -51,6 +53,10 @@ def add_custom_arg(custom_arg)
@custom_args = @custom_args.merge(custom_arg['custom_arg'])
end

def add_dynamic_template_data(dynamic_template_data)
@dynamic_template_data.merge!(dynamic_template_data)
end

def send_at=(send_at)
@send_at = send_at
end
Expand All @@ -68,6 +74,7 @@ def to_json(*)
'headers' => self.headers,
'substitutions' => self.substitutions,
'custom_args' => self.custom_args,
'dynamic_template_data' => self.dynamic_template_data,
'send_at' => self.send_at
}.delete_if { |_, value| value.to_s.strip == '' || value == [] || value == {}}
end
Expand Down
15 changes: 15 additions & 0 deletions test/sendgrid/helpers/mail/test_personalizations.rb
Original file line number Diff line number Diff line change
Expand Up @@ -143,4 +143,19 @@ def test_add_custom_arg
assert_equal @personalization.to_json, expected_json
end

def test_add_dynamic_template_data
@personalization = Personalization.new()
@personalization.add_dynamic_template_data({
"name"=>"Example User",
"city"=> "Denver"
})
expected_json = {
"dynamic_template_data"=>{
"name"=>"Example User",
"city"=>"Denver"
}
}
assert_equal @personalization.to_json, expected_json
end

end

0 comments on commit 3705080

Please sign in to comment.