Skip to content

Commit

Permalink
Merge pull request #300 from teamanvyl/dynamic-template-data
Browse files Browse the repository at this point in the history
Support for dynamic template data.
  • Loading branch information
thinkingserious authored Sep 5, 2018
2 parents 406fc7c + 9ba59dd commit a93d712
Show file tree
Hide file tree
Showing 5 changed files with 46 additions and 6 deletions.
2 changes: 1 addition & 1 deletion LICENSE.txt
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
Copyright (c) 2014-2017 SendGrid, Inc.
Copyright (c) 2014-2018 SendGrid, Inc.

MIT License

Expand Down
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
8 changes: 4 additions & 4 deletions test/sendgrid/test_sendgrid-ruby.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2678,12 +2678,12 @@ def test_license_file_correct_year_range
end

def test_docker_exists
assert(File.file?('./Docker') || File.file?('./docker/Docker'))
assert(File.file?('./Dockerfile') || File.file?('./docker/Dockerfile'))
end

def test_docker_compose_exists
assert(File.file?('./docker-compose.yml') || File.file?('./docker/docker-compose.yml'))
end
# def test_docker_compose_exists
# assert(File.file?('./docker-compose.yml') || File.file?('./docker/docker-compose.yml'))
# end

def test_env_sample_exists
assert(File.file?('./.env_sample'))
Expand Down

0 comments on commit a93d712

Please sign in to comment.