sendgrid-ruby 4.0.8 → 6.0.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (95) hide show
  1. checksums.yaml +4 -4
  2. data/.codeclimate.yml +21 -0
  3. data/.env_sample +1 -0
  4. data/.github/PULL_REQUEST_TEMPLATE +26 -0
  5. data/.gitignore +1 -0
  6. data/.travis.yml +11 -2
  7. data/CHANGELOG.md +119 -3
  8. data/CODE_OF_CONDUCT.md +41 -0
  9. data/CONTRIBUTING.md +34 -30
  10. data/Gemfile +4 -1
  11. data/LICENSE.txt +1 -1
  12. data/README.md +53 -24
  13. data/Rakefile +7 -2
  14. data/TROUBLESHOOTING.md +41 -15
  15. data/UPGRADE.md +5 -0
  16. data/USAGE.md +1130 -1129
  17. data/USE_CASES.md +280 -16
  18. data/config.ru +4 -0
  19. data/docker/Dockerfile +12 -0
  20. data/docker/README.md +30 -0
  21. data/examples/helpers/mail/example.rb +62 -45
  22. data/examples/helpers/settings/example.rb +23 -0
  23. data/examples/helpers/stats/example.rb +42 -0
  24. data/examples/ips/ips.rb +13 -0
  25. data/examples/mail/mail.rb +2 -2
  26. data/examples/scopes/scopes.rb +49 -3
  27. data/examples/{whitelabel/whitelabel.rb → senderauthentication/senderauthentication.rb} +27 -27
  28. data/examples/suppression/suppression.rb +10 -10
  29. data/gemfiles/Sinatra_1.gemfile +6 -0
  30. data/gemfiles/Sinatra_2.gemfile +6 -0
  31. data/lib/sendgrid-ruby.rb +27 -1
  32. data/lib/sendgrid/client.rb +12 -9
  33. data/lib/sendgrid/helpers/inbound/README.md +98 -0
  34. data/lib/sendgrid/helpers/inbound/app.rb +32 -0
  35. data/lib/sendgrid/helpers/inbound/config.yml +26 -0
  36. data/lib/sendgrid/helpers/inbound/public/index.html +10 -0
  37. data/lib/sendgrid/helpers/inbound/sample_data/default_data.txt +58 -0
  38. data/lib/sendgrid/helpers/inbound/sample_data/raw_data.txt +57 -0
  39. data/lib/sendgrid/helpers/inbound/sample_data/raw_data_with_attachments.txt +298 -0
  40. data/lib/sendgrid/helpers/inbound/send.rb +26 -0
  41. data/lib/sendgrid/helpers/ip_management/ip_management.rb +17 -0
  42. data/lib/sendgrid/helpers/mail/README.md +1 -1
  43. data/lib/sendgrid/helpers/mail/asm.rb +33 -0
  44. data/lib/sendgrid/helpers/mail/attachment.rb +86 -0
  45. data/lib/sendgrid/helpers/mail/bcc_settings.rb +33 -0
  46. data/lib/sendgrid/helpers/mail/bypass_list_management.rb +43 -0
  47. data/lib/sendgrid/helpers/mail/category.rb +20 -0
  48. data/lib/sendgrid/helpers/mail/click_tracking.rb +33 -0
  49. data/lib/sendgrid/helpers/mail/content.rb +20 -0
  50. data/lib/sendgrid/helpers/mail/custom_arg.rb +24 -0
  51. data/lib/sendgrid/helpers/mail/email.rb +29 -0
  52. data/lib/sendgrid/helpers/mail/footer.rb +43 -0
  53. data/lib/sendgrid/helpers/mail/ganalytics.rb +74 -0
  54. data/lib/sendgrid/helpers/mail/header.rb +24 -0
  55. data/lib/sendgrid/helpers/mail/mail.rb +36 -895
  56. data/lib/sendgrid/helpers/mail/mail_settings.rb +63 -0
  57. data/lib/sendgrid/helpers/mail/open_tracking.rb +33 -0
  58. data/lib/sendgrid/helpers/mail/personalization.rb +82 -0
  59. data/lib/sendgrid/helpers/mail/section.rb +24 -0
  60. data/lib/sendgrid/helpers/mail/spam_check.rb +43 -0
  61. data/lib/sendgrid/helpers/mail/subscription_tracking.rb +53 -0
  62. data/lib/sendgrid/helpers/mail/substitution.rb +24 -0
  63. data/lib/sendgrid/helpers/mail/tracking_settings.rb +53 -0
  64. data/lib/sendgrid/helpers/permissions/scope.rb +28 -0
  65. data/lib/sendgrid/helpers/permissions/scopes.yml +309 -0
  66. data/lib/sendgrid/helpers/settings/README.md +14 -0
  67. data/lib/sendgrid/helpers/settings/mail_settings_dto.rb +13 -0
  68. data/lib/sendgrid/helpers/settings/partner_settings_dto.rb +13 -0
  69. data/lib/sendgrid/helpers/settings/settings.rb +28 -0
  70. data/lib/sendgrid/helpers/settings/tracking_settings_dto.rb +24 -0
  71. data/lib/sendgrid/helpers/settings/user_settings_dto.rb +13 -0
  72. data/lib/sendgrid/helpers/stats/email_stats.rb +46 -0
  73. data/lib/sendgrid/helpers/stats/metrics.rb +35 -0
  74. data/lib/sendgrid/helpers/stats/stats_response.rb +31 -0
  75. data/lib/sendgrid/version.rb +2 -2
  76. data/mail_helper_v3.md +390 -0
  77. data/sendgrid-ruby.gemspec +12 -5
  78. data/spec/sendgrid/helpers/ip_management/ip_management_spec.rb +12 -0
  79. data/spec/sendgrid/helpers/settings/mail_settings_dto_spec.rb +32 -0
  80. data/spec/sendgrid/helpers/settings/partner_settings_dto_spec.rb +24 -0
  81. data/spec/sendgrid/helpers/settings/settings_spec.rb +25 -0
  82. data/spec/sendgrid/helpers/settings/tracking_settings_dto_spec.rb +27 -0
  83. data/spec/sendgrid/helpers/settings/user_settings_dto_spec.rb +24 -0
  84. data/spec/sendgrid/helpers/stats/email_stats_spec.rb +112 -0
  85. data/spec/sendgrid/helpers/stats/metrics_spec.rb +46 -0
  86. data/spec/sendgrid/helpers/stats/stats_response_spec.rb +76 -0
  87. data/spec/spec_helper.rb +10 -0
  88. data/test/sendgrid/helpers/mail/test_attachment.rb +35 -0
  89. data/test/sendgrid/helpers/mail/test_category.rb +27 -0
  90. data/test/sendgrid/helpers/mail/test_email.rb +34 -0
  91. data/test/sendgrid/helpers/mail/test_mail.rb +198 -59
  92. data/test/sendgrid/helpers/mail/test_personalizations.rb +161 -0
  93. data/test/sendgrid/permissions/test_scopes.rb +38 -0
  94. data/test/sendgrid/test_sendgrid-ruby.rb +90 -9
  95. metadata +170 -10
@@ -0,0 +1,27 @@
1
+ require_relative '../../../../lib/sendgrid/helpers/mail/mail'
2
+ require 'minitest/autorun'
3
+
4
+ class TestCategory < Minitest::Test
5
+
6
+ include SendGrid
7
+
8
+ def setup
9
+ @category = Category.new(name: 'foo')
10
+ end
11
+
12
+ def test_aliases
13
+ assert_equal @category.method(:name), @category.method(:category)
14
+ end
15
+
16
+ def test_name
17
+ assert_equal @category.name, 'foo'
18
+ end
19
+
20
+ def test_to_json
21
+ expected_json = {
22
+ 'category' => 'foo'
23
+ }
24
+ assert_equal @category.to_json, expected_json
25
+ end
26
+
27
+ end
@@ -0,0 +1,34 @@
1
+ require_relative '../../../../lib/sendgrid/helpers/mail/email'
2
+ require 'minitest/autorun'
3
+
4
+ class TestEmail < Minitest::Test
5
+
6
+ include SendGrid
7
+
8
+ def test_split_email_full_email
9
+ @email = Email.new(email: "Example User <test1@example.com>")
10
+ expected_json = {
11
+ "email"=>"test1@example.com",
12
+ "name"=>"Example User"
13
+ }
14
+ assert_equal @email.to_json, expected_json
15
+ end
16
+
17
+ def test_split_email_only_email
18
+ @email = Email.new(email: "test1@example.com")
19
+ expected_json = {
20
+ "email"=>"test1@example.com",
21
+ }
22
+ assert_equal @email.to_json, expected_json
23
+ end
24
+
25
+ def test_split_email_name_and_email
26
+ @email = Email.new(name: "Example User", email: "test1@example.com")
27
+ expected_json = {
28
+ "email"=>"test1@example.com",
29
+ "name"=>"Example User"
30
+ }
31
+ assert_equal @email.to_json, expected_json
32
+ end
33
+
34
+ end
@@ -9,88 +9,86 @@ class TestMail < Minitest::Test
9
9
  end
10
10
 
11
11
  def test_hello_world
12
- mail = Mail.new()
13
- mail.from = Email.new(email: "test@example.com")
14
- mail.subject = "Hello World from the SendGrid Ruby Library"
15
- personalization = Personalization.new()
16
- personalization.to = Email.new(email: "test@example.com")
17
- mail.personalizations = personalization
18
- mail.contents = Content.new(type: "text/plain", value: "some text here")
19
- mail.contents = Content.new(type: "text/html", value: "<html><body>some text here</body></html>")
20
- assert_equal(mail.to_json, JSON.parse('{"content":[{"type":"text/plain","value":"some text here"},{"type":"text/html","value":"<html><body>some text here</body></html>"}],"from":{"email":"test@example.com"},"personalizations":[{"to":[{"email":"test@example.com"}]}],"subject":"Hello World from the SendGrid Ruby Library"}'))
12
+ from = Email.new(email: 'test@example.com')
13
+ to = Email.new(email: 'test@example.com')
14
+ subject = 'Sending with Twilio SendGrid is Fun'
15
+ content = Content.new(type: 'text/plain', value: 'and easy to do anywhere, even with Ruby')
16
+ mail = SendGrid::Mail.new(from, subject, to, content)
17
+
18
+ assert_equal(mail.to_json, JSON.parse('{"from":{"email":"test@example.com"}, "subject":"Sending with Twilio SendGrid is Fun", "personalizations":[{"to":[{"email":"test@example.com"}]}], "content":[{"type":"text/plain", "value":"and easy to do anywhere, even with Ruby"}]}'))
21
19
  end
22
20
 
23
21
  def test_kitchen_sink
24
- mail = Mail.new()
22
+ mail = SendGrid::Mail.new
25
23
  mail.from = Email.new(email: "test@example.com")
26
- mail.subject = "Hello World from the SendGrid Ruby Library"
27
- personalization = Personalization.new()
28
- personalization.to = Email.new(email: "test@example.com", name: "Example User")
29
- personalization.to = Email.new(email: "test@example.com", name: "Example User")
30
- personalization.cc = Email.new(email: "test@example.com", name: "Example User")
31
- personalization.cc = Email.new(email: "test@example.com", name: "Example User")
32
- personalization.bcc = Email.new(email: "test@example.com", name: "Example User")
33
- personalization.bcc = Email.new(email: "test@example.com", name: "Example User")
34
- personalization.subject = "Hello World from the Personalized SendGrid Python Library"
35
- personalization.headers = Header.new(key: "X-Test", value: "True")
36
- personalization.headers = Header.new(key: "X-Mock", value: "False")
37
- personalization.substitutions = Substitution.new(key: "%name%", value: "Example User")
38
- personalization.substitutions = Substitution.new(key: "%city%", value: "Denver")
39
- personalization.custom_args = CustomArg.new(key: "user_id", value: "343")
40
- personalization.custom_args = CustomArg.new(key: "type", value: "marketing")
24
+ mail.subject = "Hello World from the Twilio SendGrid Ruby Library"
25
+ personalization = Personalization.new
26
+ personalization.add_to(Email.new(email: "test@example.com", name: "Example User"))
27
+ personalization.add_to(Email.new(email: "test@example.com", name: "Example User"))
28
+ personalization.add_cc(Email.new(email: "test@example.com", name: "Example User"))
29
+ personalization.add_cc(Email.new(email: "test@example.com", name: "Example User"))
30
+ personalization.add_bcc(Email.new(email: "test@example.com", name: "Example User"))
31
+ personalization.add_bcc(Email.new(email: "test@example.com", name: "Example User"))
32
+ personalization.subject = "Hello World from the Personalized Twilio SendGrid Ruby Library"
33
+ personalization.add_header(Header.new(key: "X-Test", value: "True"))
34
+ personalization.add_header(Header.new(key: "X-Mock", value: "False"))
35
+ personalization.add_substitution(Substitution.new(key: "%name%", value: "Example User"))
36
+ personalization.add_substitution(Substitution.new(key: "%city%", value: "Denver"))
37
+ personalization.add_custom_arg(CustomArg.new(key: "user_id", value: "343"))
38
+ personalization.add_custom_arg(CustomArg.new(key: "type", value: "marketing"))
41
39
  personalization.send_at = 1443636843
42
- mail.personalizations = personalization
43
-
44
- personalization2 = Personalization.new()
45
- personalization2.to = Email.new(email: "test@example.com", name: "Example User")
46
- personalization2.to = Email.new(email: "test@example.com", name: "Example User")
47
- personalization2.cc = Email.new(email: "test@example.com", name: "Example User")
48
- personalization2.cc = Email.new(email: "test@example.com", name: "Example User")
49
- personalization2.bcc = Email.new(email: "test@example.com", name: "Example User")
50
- personalization2.bcc = Email.new(email: "test@example.com", name: "Example User")
51
- personalization2.subject = "Hello World from the Personalized SendGrid Python Library"
52
- personalization2.headers = Header.new(key: "X-Test", value: "True")
53
- personalization2.headers = Header.new(key: "X-Mock", value: "False")
54
- personalization2.substitutions = Substitution.new(key: "%name%", value: "Example User")
55
- personalization2.substitutions = Substitution.new(key: "%city%", value: "Denver")
56
- personalization2.custom_args = CustomArg.new(key: "user_id", value: "343")
57
- personalization2.custom_args = CustomArg.new(key: "type", value: "marketing")
40
+ mail.add_personalization(personalization)
41
+
42
+ personalization2 = Personalization.new
43
+ personalization2.add_to(Email.new(email: "test@example.com", name: "Example User"))
44
+ personalization2.add_to(Email.new(email: "test@example.com", name: "Example User"))
45
+ personalization2.add_cc(Email.new(email: "test@example.com", name: "Example User"))
46
+ personalization2.add_cc(Email.new(email: "test@example.com", name: "Example User"))
47
+ personalization2.add_bcc(Email.new(email: "test@example.com", name: "Example User"))
48
+ personalization2.add_bcc(Email.new(email: "test@example.com", name: "Example User"))
49
+ personalization2.subject = "Hello World from the Personalized Twilio SendGrid Ruby Library"
50
+ personalization2.add_header(Header.new(key: "X-Test", value: "True"))
51
+ personalization2.add_header(Header.new(key: "X-Mock", value: "False"))
52
+ personalization2.add_substitution(Substitution.new(key: "%name%", value: "Example User"))
53
+ personalization2.add_substitution(Substitution.new(key: "%city%", value: "Denver"))
54
+ personalization2.add_custom_arg(CustomArg.new(key: "user_id", value: "343"))
55
+ personalization2.add_custom_arg(CustomArg.new(key: "type", value: "marketing"))
58
56
  personalization2.send_at = 1443636843
59
- mail.personalizations = personalization2
57
+ mail.add_personalization(personalization2)
60
58
 
61
- mail.contents = Content.new(type: "text/plain", value: "some text here")
62
- mail.contents = Content.new(type: "text/html", value: "<html><body>some text here</body></html>")
59
+ mail.add_content(Content.new(type: "text/plain", value: "some text here"))
60
+ mail.add_content(Content.new(type: "text/html", value: "<html><body>some text here</body></html>"))
63
61
 
64
- attachment = Attachment.new()
62
+ attachment = Attachment.new
65
63
  attachment.content = "TG9yZW0gaXBzdW0gZG9sb3Igc2l0IGFtZXQsIGNvbnNlY3RldHVyIGFkaXBpc2NpbmcgZWxpdC4gQ3JhcyBwdW12"
66
64
  attachment.type = "application/pdf"
67
65
  attachment.filename = "balance_001.pdf"
68
66
  attachment.disposition = "attachment"
69
67
  attachment.content_id = "Balance Sheet"
70
68
 
71
- mail.attachments = attachment
69
+ mail.add_attachment(attachment)
72
70
 
73
- attachment2 = Attachment.new()
71
+ attachment2 = Attachment.new
74
72
  attachment2.content = "BwdW"
75
73
  attachment2.type = "image/png"
76
74
  attachment2.filename = "banner.png"
77
75
  attachment2.disposition = "inline"
78
76
  attachment2.content_id = "Banner"
79
- mail.attachments = attachment2
77
+ mail.add_attachment(attachment2)
80
78
 
81
79
  mail.template_id = "13b8f94f-bcae-4ec6-b752-70d6cb59f932"
82
80
 
83
- mail.sections = Section.new(key: "%section1%", value: "Substitution Text for Section 1")
84
- mail.sections = Section.new(key: "%section2%", value: "Substitution Text for Section 2")
81
+ mail.add_section(Section.new(key: "%section1%", value: "Substitution Text for Section 1"))
82
+ mail.add_section(Section.new(key: "%section2%", value: "Substitution Text for Section 2"))
85
83
 
86
- mail.headers = Header.new(key: "X-Test3", value: "test3")
87
- mail.headers = Header.new(key: "X-Test4", value: "test4")
84
+ mail.add_header(Header.new(key: "X-Test3", value: "test3"))
85
+ mail.add_header(Header.new(key: "X-Test4", value: "test4"))
88
86
 
89
- mail.categories = Category.new(name: "May")
90
- mail.categories = Category.new(name: "2016")
87
+ mail.add_category(Category.new(name: "May"))
88
+ mail.add_category(Category.new(name: "2016"))
91
89
 
92
- mail.custom_args = CustomArg.new(key: "campaign", value: "welcome")
93
- mail.custom_args = CustomArg.new(key: "weekday", value: "morning")
90
+ mail.add_custom_arg(CustomArg.new(key: "campaign", value: "welcome"))
91
+ mail.add_custom_arg(CustomArg.new(key: "weekday", value: "morning"))
94
92
 
95
93
  mail.send_at = 1443636842
96
94
 
@@ -100,7 +98,7 @@ class TestMail < Minitest::Test
100
98
 
101
99
  mail.ip_pool_name = "23"
102
100
 
103
- mail_settings = MailSettings.new()
101
+ mail_settings = MailSettings.new
104
102
  mail_settings.bcc = BccSettings.new(enable: true, email: "test@example.com")
105
103
  mail_settings.bypass_list_management = BypassListManagement.new(enable: true)
106
104
  mail_settings.footer = Footer.new(enable: true, text: "Footer Text", html: "<html><body>Footer Text</body></html>")
@@ -108,7 +106,7 @@ class TestMail < Minitest::Test
108
106
  mail_settings.spam_check = SpamCheck.new(enable: true, threshold: 1, post_to_url: "https://spamcatcher.sendgrid.com")
109
107
  mail.mail_settings = mail_settings
110
108
 
111
- tracking_settings = TrackingSettings.new()
109
+ tracking_settings = TrackingSettings.new
112
110
  tracking_settings.click_tracking = ClickTracking.new(enable: false, enable_text: false)
113
111
  tracking_settings.open_tracking = OpenTracking.new(enable: true, substitution_tag: "Optional tag to replace with the open image in the body of the message")
114
112
  tracking_settings.subscription_tracking = SubscriptionTracking.new(enable: true, text: "text to insert into the text/plain portion of the message", html: "html to insert into the text/html portion of the message", substitution_tag: "Optional tag to replace with the open image in the body of the message")
@@ -117,6 +115,147 @@ class TestMail < Minitest::Test
117
115
 
118
116
  mail.reply_to = Email.new(email: "test@example.com")
119
117
 
120
- assert_equal(mail.to_json, JSON.parse('{"asm":{"group_id":99,"groups_to_display":[4,5,6,7,8]},"attachments":[{"content":"TG9yZW0gaXBzdW0gZG9sb3Igc2l0IGFtZXQsIGNvbnNlY3RldHVyIGFkaXBpc2NpbmcgZWxpdC4gQ3JhcyBwdW12","content_id":"Balance Sheet","disposition":"attachment","filename":"balance_001.pdf","type":"application/pdf"},{"content":"BwdW","content_id":"Banner","disposition":"inline","filename":"banner.png","type":"image/png"}],"batch_id":"sendgrid_batch_id","categories":["May","2016"],"content":[{"type":"text/plain","value":"some text here"},{"type":"text/html","value":"<html><body>some text here</body></html>"}],"custom_args":{"campaign":"welcome","weekday":"morning"},"from":{"email":"test@example.com"},"headers":{"X-Test3":"test3","X-Test4":"test4"},"ip_pool_name":"23","mail_settings":{"bcc":{"email":"test@example.com","enable":true},"bypass_list_management":{"enable":true},"footer":{"enable":true,"html":"<html><body>Footer Text</body></html>","text":"Footer Text"},"sandbox_mode":{"enable":true},"spam_check":{"enable":true,"post_to_url":"https://spamcatcher.sendgrid.com","threshold":1}},"personalizations":[{"bcc":[{"email":"test@example.com","name":"Example User"},{"email":"test@example.com","name":"Example User"}],"cc":[{"email":"test@example.com","name":"Example User"},{"email":"test@example.com","name":"Example User"}],"custom_args":{"type":"marketing","user_id":"343"},"headers":{"X-Mock":"False","X-Test":"True"},"send_at":1443636843,"subject":"Hello World from the Personalized SendGrid Python Library","substitutions":{"%city%":"Denver","%name%":"Example User"},"to":[{"email":"test@example.com","name":"Example User"},{"email":"test@example.com","name":"Example User"}]},{"bcc":[{"email":"test@example.com","name":"Example User"},{"email":"test@example.com","name":"Example User"}],"cc":[{"email":"test@example.com","name":"Example User"},{"email":"test@example.com","name":"Example User"}],"custom_args":{"type":"marketing","user_id":"343"},"headers":{"X-Mock":"False","X-Test":"True"},"send_at":1443636843,"subject":"Hello World from the Personalized SendGrid Python Library","substitutions":{"%city%":"Denver","%name%":"Example User"},"to":[{"email":"test@example.com","name":"Example User"},{"email":"test@example.com","name":"Example User"}]}],"reply_to":{"email":"test@example.com"},"sections":{"%section1%":"Substitution Text for Section 1","%section2%":"Substitution Text for Section 2"},"send_at":1443636842,"subject":"Hello World from the SendGrid Ruby Library","template_id":"13b8f94f-bcae-4ec6-b752-70d6cb59f932","tracking_settings":{"click_tracking":{"enable":false,"enable_text":false},"ganalytics":{"enable":true,"utm_campaign":"some campaign","utm_content":"some content","utm_medium":"some medium","utm_source":"some source","utm_term":"some term"},"open_tracking":{"enable":true,"substitution_tag":"Optional tag to replace with the open image in the body of the message"},"subscription_tracking":{"enable":true,"html":"html to insert into the text/html portion of the message","substitution_tag":"Optional tag to replace with the open image in the body of the message","text":"text to insert into the text/plain portion of the message"}}}'))
118
+ assert_equal(mail.to_json, JSON.parse('{"asm":{"group_id":99,"groups_to_display":[4,5,6,7,8]},"attachments":[{"content":"TG9yZW0gaXBzdW0gZG9sb3Igc2l0IGFtZXQsIGNvbnNlY3RldHVyIGFkaXBpc2NpbmcgZWxpdC4gQ3JhcyBwdW12","content_id":"Balance Sheet","disposition":"attachment","filename":"balance_001.pdf","type":"application/pdf"},{"content":"BwdW","content_id":"Banner","disposition":"inline","filename":"banner.png","type":"image/png"}],"batch_id":"sendgrid_batch_id","categories":["May","2016"],"content":[{"type":"text/plain","value":"some text here"},{"type":"text/html","value":"<html><body>some text here</body></html>"}],"custom_args":{"campaign":"welcome","weekday":"morning"},"from":{"email":"test@example.com"},"headers":{"X-Test3":"test3","X-Test4":"test4"},"ip_pool_name":"23","mail_settings":{"bcc":{"email":"test@example.com","enable":true},"bypass_list_management":{"enable":true},"footer":{"enable":true,"html":"<html><body>Footer Text</body></html>","text":"Footer Text"},"sandbox_mode":{"enable":true},"spam_check":{"enable":true,"post_to_url":"https://spamcatcher.sendgrid.com","threshold":1}},"personalizations":[{"bcc":[{"email":"test@example.com","name":"Example User"},{"email":"test@example.com","name":"Example User"}],"cc":[{"email":"test@example.com","name":"Example User"},{"email":"test@example.com","name":"Example User"}],"custom_args":{"type":"marketing","user_id":"343"},"headers":{"X-Mock":"False","X-Test":"True"},"send_at":1443636843,"subject":"Hello World from the Personalized Twilio SendGrid Ruby Library","substitutions":{"%city%":"Denver","%name%":"Example User"},"to":[{"email":"test@example.com","name":"Example User"},{"email":"test@example.com","name":"Example User"}]},{"bcc":[{"email":"test@example.com","name":"Example User"},{"email":"test@example.com","name":"Example User"}],"cc":[{"email":"test@example.com","name":"Example User"},{"email":"test@example.com","name":"Example User"}],"custom_args":{"type":"marketing","user_id":"343"},"headers":{"X-Mock":"False","X-Test":"True"},"send_at":1443636843,"subject":"Hello World from the Personalized Twilio SendGrid Ruby Library","substitutions":{"%city%":"Denver","%name%":"Example User"},"to":[{"email":"test@example.com","name":"Example User"},{"email":"test@example.com","name":"Example User"}]}],"reply_to":{"email":"test@example.com"},"sections":{"%section1%":"Substitution Text for Section 1","%section2%":"Substitution Text for Section 2"},"send_at":1443636842,"subject":"Hello World from the Twilio SendGrid Ruby Library","template_id":"13b8f94f-bcae-4ec6-b752-70d6cb59f932","tracking_settings":{"click_tracking":{"enable":false,"enable_text":false},"ganalytics":{"enable":true,"utm_campaign":"some campaign","utm_content":"some content","utm_medium":"some medium","utm_source":"some source","utm_term":"some term"},"open_tracking":{"enable":true,"substitution_tag":"Optional tag to replace with the open image in the body of the message"},"subscription_tracking":{"enable":true,"html":"html to insert into the text/html portion of the message","substitution_tag":"Optional tag to replace with the open image in the body of the message","text":"text to insert into the text/plain portion of the message"}}}'))
119
+ end
120
+
121
+ def test_that_personalizations_is_empty_initially
122
+ mail = SendGrid::Mail.new
123
+ assert_equal([], mail.personalizations)
124
+ end
125
+
126
+ def test_that_contents_is_empty_initially
127
+ mail = SendGrid::Mail.new
128
+ assert_equal([], mail.contents)
129
+ end
130
+
131
+ def test_that_attachments_is_empty_initially
132
+ mail = SendGrid::Mail.new
133
+ assert_equal([], mail.attachments)
134
+ end
135
+
136
+ def test_that_categories_is_empty_initially
137
+ mail = SendGrid::Mail.new
138
+ assert_equal([], mail.categories)
139
+ end
140
+
141
+ def test_add_personalization
142
+ mail = SendGrid::Mail.new
143
+ mail.add_personalization('foo')
144
+ assert_equal(['foo'.to_json], mail.personalizations)
145
+ end
146
+
147
+ def test_add_content
148
+ mail = SendGrid::Mail.new
149
+ mail.add_content('foo')
150
+ assert_equal(['foo'.to_json], mail.contents)
151
+ end
152
+
153
+ def test_add_section
154
+ mail = SendGrid::Mail.new
155
+ mail.add_section(Section.new(key: '%section1%', value: 'Substitution Text for Section 1'))
156
+ expected_json = {
157
+ "sections"=>{
158
+ "%section1%"=>"Substitution Text for Section 1"
159
+ }
160
+ }
161
+ assert_equal mail.to_json, expected_json
162
+ mail.add_section(Section.new(key: '%section2%', value: 'Substitution Text for Section 2'))
163
+ expected_json = {
164
+ "sections"=>{
165
+ "%section1%"=>"Substitution Text for Section 1",
166
+ "%section2%"=>"Substitution Text for Section 2"
167
+ }
168
+ }
169
+ assert_equal mail.to_json, expected_json
170
+ end
171
+
172
+ def test_add_header
173
+ mail = SendGrid::Mail.new
174
+ mail.add_header(Header.new(key: 'X-Test3', value: 'test3'))
175
+ expected_json = {
176
+ "headers"=>{
177
+ "X-Test3"=>"test3"
178
+ }
179
+ }
180
+ assert_equal mail.to_json, expected_json
181
+ mail.add_header(Header.new(key: 'X-Test4', value: 'test4'))
182
+ expected_json = {
183
+ "headers"=>{
184
+ "X-Test3"=>"test3",
185
+ "X-Test4"=>"test4"
186
+ }
187
+ }
188
+ assert_equal mail.to_json, expected_json
189
+ end
190
+
191
+ def test_add_custom_arg
192
+ mail = SendGrid::Mail.new
193
+ mail.add_custom_arg(CustomArg.new(key: 'campaign 1', value: 'welcome 1'))
194
+ expected_json = {
195
+ "custom_args"=>{
196
+ "campaign 1"=>"welcome 1"
197
+ }
198
+ }
199
+ assert_equal mail.to_json, expected_json
200
+ mail.add_custom_arg(CustomArg.new(key: 'campaign 2', value: 'welcome 2'))
201
+ expected_json = {
202
+ "custom_args"=>{
203
+ "campaign 1"=>"welcome 1",
204
+ "campaign 2"=>"welcome 2"
205
+ }
206
+ }
207
+ assert_equal mail.to_json, expected_json
208
+ end
209
+
210
+ def test_add_non_string_custom_arg
211
+ mail = Mail.new
212
+ mail.add_custom_arg(CustomArg.new(key: "Integer", value: 1))
213
+ mail.add_custom_arg(CustomArg.new(key: "Array", value: [1, "a", true]))
214
+ mail.add_custom_arg(CustomArg.new(key: "Hash", value: {"a" => 1, "b" => 2}))
215
+ expected_json = {
216
+ "custom_args"=>{
217
+ "Integer"=>"1",
218
+ "Array"=>"[1, \"a\", true]",
219
+ "Hash"=>"{\"a\"=>1, \"b\"=>2}",
220
+ }
221
+ }
222
+ assert_equal mail.to_json, expected_json
223
+ end
224
+
225
+ def test_add_attachment
226
+ mail = SendGrid::Mail.new
227
+ mail.add_attachment('foo')
228
+ assert_equal(['foo'.to_json], mail.attachments)
229
+ end
230
+
231
+ def test_add_valid_category
232
+ mail = SendGrid::Mail.new
233
+ category = Category.new(name: 'foo')
234
+ mail.add_category(category)
235
+ assert_equal(['foo'], mail.categories)
236
+ end
237
+
238
+ def test_add_more_than_1_valid_category
239
+ mail = SendGrid::Mail.new
240
+ category_1 = Category.new(name: 'foo')
241
+ category_2 = Category.new(name: 'bar')
242
+ mail.add_category(category_1)
243
+ mail.add_category(category_2)
244
+ assert_equal(['foo', 'bar'], mail.categories)
245
+ end
246
+
247
+ def test_add_invalid_category
248
+ mail = SendGrid::Mail.new
249
+ assert_raises(NoMethodError) do
250
+ mail.add_category('foo')
251
+ end
252
+ end
253
+
254
+ def test_check_for_secrets
255
+ mail = Mail.new
256
+ mail.add_content(Content.new(type: 'text/plain', value: 'Sensitive information: SG.a123b456'))
257
+ assert_raises(SecurityError) do
258
+ mail.check_for_secrets([/SG.[a-zA-Z0-9_-]*/])
259
+ end
121
260
  end
122
261
  end
@@ -0,0 +1,161 @@
1
+ require_relative '../../../../lib/sendgrid/helpers/mail/personalization'
2
+ require 'minitest/autorun'
3
+
4
+ class TestPersonalization < Minitest::Test
5
+
6
+ include SendGrid
7
+
8
+ def test_add_to
9
+ @personalization = Personalization.new()
10
+ @personalization.add_to(Email.new(email: 'test1@example.com', name: 'Example User'))
11
+ expected_json = {
12
+ "to"=>[
13
+ {
14
+ "email"=>"test1@example.com",
15
+ "name"=>"Example User"
16
+ }
17
+ ]
18
+ }
19
+ @personalization.add_to(Email.new(email: 'test2@example.com', name: 'Example User 2'))
20
+ expected_json = {
21
+ "to"=>[
22
+ {
23
+ "email"=>"test1@example.com",
24
+ "name"=>"Example User"
25
+ },
26
+ {
27
+ "email"=>"test2@example.com",
28
+ "name"=>"Example User 2"
29
+ }
30
+ ]
31
+ }
32
+ assert_equal @personalization.to_json, expected_json
33
+ end
34
+
35
+ def test_add_cc
36
+ @personalization = Personalization.new()
37
+ @personalization.add_cc(Email.new(email: 'test1@example.com', name: 'Example User'))
38
+ expected_json = {
39
+ "cc"=>[
40
+ {
41
+ "email"=>"test1@example.com",
42
+ "name"=>"Example User"
43
+ }
44
+ ]
45
+ }
46
+ @personalization.add_cc(Email.new(email: 'test2@example.com', name: 'Example User 2'))
47
+ expected_json = {
48
+ "cc"=>[
49
+ {
50
+ "email"=>"test1@example.com",
51
+ "name"=>"Example User"
52
+ },
53
+ {
54
+ "email"=>"test2@example.com",
55
+ "name"=>"Example User 2"
56
+ }
57
+ ]
58
+ }
59
+ assert_equal @personalization.to_json, expected_json
60
+ end
61
+
62
+ def test_add_bcc
63
+ @personalization = Personalization.new()
64
+ @personalization.add_bcc(Email.new(email: 'test1@example.com', name: 'Example User'))
65
+ expected_json = {
66
+ "bcc"=>[
67
+ {
68
+ "email"=>"test1@example.com",
69
+ "name"=>"Example User"
70
+ }
71
+ ]
72
+ }
73
+ @personalization.add_bcc(Email.new(email: 'test2@example.com', name: 'Example User 2'))
74
+ expected_json = {
75
+ "bcc"=>[
76
+ {
77
+ "email"=>"test1@example.com",
78
+ "name"=>"Example User"
79
+ },
80
+ {
81
+ "email"=>"test2@example.com",
82
+ "name"=>"Example User 2"
83
+ }
84
+ ]
85
+ }
86
+ assert_equal @personalization.to_json, expected_json
87
+ end
88
+
89
+ def test_add_header
90
+ @personalization = Personalization.new()
91
+ @personalization.add_header(Header.new(key: 'X-Test', value: 'True'))
92
+ expected_json = {
93
+ "headers"=>{
94
+ "X-Test"=>"True"
95
+ }
96
+ }
97
+ assert_equal @personalization.to_json, expected_json
98
+ @personalization.add_header(Header.new(key: 'X-Test 1', value: 'False'))
99
+ expected_json = {
100
+ "headers"=>{
101
+ "X-Test"=>"True",
102
+ "X-Test 1"=>"False"
103
+ }
104
+ }
105
+ assert_equal @personalization.to_json, expected_json
106
+ end
107
+
108
+ def test_add_substitution
109
+ @personalization = Personalization.new()
110
+ @personalization.add_substitution(Substitution.new(key: '%name%', value: 'Example User'))
111
+ expected_json = {
112
+ "substitutions"=>{
113
+ "%name%"=>"Example User"
114
+ }
115
+ }
116
+ assert_equal @personalization.to_json, expected_json
117
+ @personalization.add_substitution(Substitution.new(key: '%name 1%', value: 'Example User 1'))
118
+ expected_json = {
119
+ "substitutions"=>{
120
+ "%name%"=>"Example User",
121
+ "%name 1%"=>"Example User 1"
122
+ }
123
+ }
124
+ assert_equal @personalization.to_json, expected_json
125
+ end
126
+
127
+ def test_add_custom_arg
128
+ @personalization = Personalization.new()
129
+ @personalization.add_custom_arg(CustomArg.new(key: 'user_id', value: '343'))
130
+ expected_json = {
131
+ "custom_args"=>{
132
+ "user_id"=>"343"
133
+ }
134
+ }
135
+ assert_equal @personalization.to_json, expected_json
136
+ @personalization.add_custom_arg(CustomArg.new(key: 'city', value: 'denver'))
137
+ expected_json = {
138
+ "custom_args"=>{
139
+ "user_id"=>"343",
140
+ "city"=>"denver"
141
+ }
142
+ }
143
+ assert_equal @personalization.to_json, expected_json
144
+ end
145
+
146
+ def test_add_dynamic_template_data
147
+ @personalization = Personalization.new()
148
+ @personalization.add_dynamic_template_data({
149
+ "name"=>"Example User",
150
+ "city"=> "Denver"
151
+ })
152
+ expected_json = {
153
+ "dynamic_template_data"=>{
154
+ "name"=>"Example User",
155
+ "city"=>"Denver"
156
+ }
157
+ }
158
+ assert_equal @personalization.to_json, expected_json
159
+ end
160
+
161
+ end