sendgrid-ruby 1.1.6 → 6.6.2

Sign up to get free protection for your applications and to get access to all the features.
Files changed (160) hide show
  1. checksums.yaml +5 -5
  2. data/.env_sample +1 -3
  3. data/.github/ISSUE_TEMPLATE/config.yml +10 -0
  4. data/.github/workflows/test-and-deploy.yml +120 -0
  5. data/.gitignore +3 -0
  6. data/.rubocop.yml +5 -27
  7. data/.rubocop_todo.yml +127 -0
  8. data/CHANGELOG.md +417 -1
  9. data/CODE_OF_CONDUCT.md +73 -0
  10. data/CONTRIBUTING.md +202 -0
  11. data/Dockerfile +14 -0
  12. data/FIRST_TIMERS.md +79 -0
  13. data/Gemfile +4 -4
  14. data/ISSUE_TEMPLATE.md +30 -0
  15. data/LICENSE +21 -0
  16. data/Makefile +14 -0
  17. data/PULL_REQUEST_TEMPLATE.md +31 -0
  18. data/README.md +153 -229
  19. data/Rakefile +9 -3
  20. data/TROUBLESHOOTING.md +151 -0
  21. data/UPGRADE.md +5 -0
  22. data/USAGE.md +5144 -0
  23. data/config.ru +4 -0
  24. data/examples/accesssettings/accesssettings.rb +80 -0
  25. data/examples/alerts/alerts.rb +59 -0
  26. data/examples/apikeys/apikeys.rb +81 -0
  27. data/examples/asm/asm.rb +170 -0
  28. data/examples/browsers/browsers.rb +13 -0
  29. data/examples/campaigns/campaigns.rb +150 -0
  30. data/examples/categories/categories.rb +33 -0
  31. data/examples/clients/clients.rb +24 -0
  32. data/examples/contactdb/contactdb.rb +392 -0
  33. data/examples/devices/devices.rb +13 -0
  34. data/examples/emailactivity/emailactivity.rb +52 -0
  35. data/examples/geo/geo.rb +13 -0
  36. data/examples/helpers/eventwebhook/example.rb +16 -0
  37. data/examples/helpers/mail/example.rb +158 -0
  38. data/examples/helpers/settings/example.rb +23 -0
  39. data/examples/helpers/stats/example.rb +42 -0
  40. data/examples/ips/ips.rb +164 -0
  41. data/examples/mail/mail.rb +170 -0
  42. data/examples/mailboxproviders/mailboxproviders.rb +13 -0
  43. data/examples/mailsettings/mailsettings.rb +216 -0
  44. data/examples/partnersettings/partnersettings.rb +36 -0
  45. data/examples/scopes/scopes.rb +59 -0
  46. data/examples/senderauthentication/senderauthentication.rb +307 -0
  47. data/examples/senders/senders.rb +95 -0
  48. data/examples/stats/stats.rb +13 -0
  49. data/examples/subusers/subusers.rb +166 -0
  50. data/examples/suppression/suppression.rb +198 -0
  51. data/examples/templates/templates.rb +127 -0
  52. data/examples/trackingsettings/trackingsettings.rb +107 -0
  53. data/examples/user/user.rb +290 -0
  54. data/gemfiles/Sinatra_1.gemfile +6 -0
  55. data/gemfiles/Sinatra_2.gemfile +6 -0
  56. data/lib/rack/sendgrid_webhook_verification.rb +55 -0
  57. data/lib/sendgrid/base_interface.rb +40 -0
  58. data/lib/sendgrid/helpers/eventwebhook/eventwebhook.rb +50 -0
  59. data/lib/sendgrid/helpers/inbound/README.md +98 -0
  60. data/lib/sendgrid/helpers/inbound/app.rb +32 -0
  61. data/lib/sendgrid/helpers/inbound/config.yml +26 -0
  62. data/lib/sendgrid/helpers/inbound/public/index.html +10 -0
  63. data/lib/sendgrid/helpers/inbound/sample_data/default_data.txt +58 -0
  64. data/lib/sendgrid/helpers/inbound/sample_data/raw_data.txt +57 -0
  65. data/lib/sendgrid/helpers/inbound/sample_data/raw_data_with_attachments.txt +298 -0
  66. data/lib/sendgrid/helpers/inbound/send.rb +26 -0
  67. data/lib/sendgrid/helpers/ip_management/ip_management.rb +17 -0
  68. data/lib/sendgrid/helpers/mail/README.md +14 -0
  69. data/lib/sendgrid/helpers/mail/asm.rb +19 -0
  70. data/lib/sendgrid/helpers/mail/attachment.rb +55 -0
  71. data/lib/sendgrid/helpers/mail/bcc_settings.rb +19 -0
  72. data/lib/sendgrid/helpers/mail/bypass_list_management.rb +31 -0
  73. data/lib/sendgrid/helpers/mail/category.rb +18 -0
  74. data/lib/sendgrid/helpers/mail/click_tracking.rb +19 -0
  75. data/lib/sendgrid/helpers/mail/content.rb +19 -0
  76. data/lib/sendgrid/helpers/mail/custom_arg.rb +18 -0
  77. data/lib/sendgrid/helpers/mail/email.rb +32 -0
  78. data/lib/sendgrid/helpers/mail/footer.rb +21 -0
  79. data/lib/sendgrid/helpers/mail/ganalytics.rb +28 -0
  80. data/lib/sendgrid/helpers/mail/header.rb +18 -0
  81. data/lib/sendgrid/helpers/mail/mail.rb +122 -0
  82. data/lib/sendgrid/helpers/mail/mail_settings.rb +45 -0
  83. data/lib/sendgrid/helpers/mail/open_tracking.rb +19 -0
  84. data/lib/sendgrid/helpers/mail/personalization.rb +93 -0
  85. data/lib/sendgrid/helpers/mail/section.rb +18 -0
  86. data/lib/sendgrid/helpers/mail/spam_check.rb +21 -0
  87. data/lib/sendgrid/helpers/mail/subscription_tracking.rb +23 -0
  88. data/lib/sendgrid/helpers/mail/substitution.rb +18 -0
  89. data/lib/sendgrid/helpers/mail/tracking_settings.rb +39 -0
  90. data/lib/sendgrid/helpers/permissions/scope.rb +28 -0
  91. data/lib/sendgrid/helpers/permissions/scopes.yml +309 -0
  92. data/lib/sendgrid/helpers/settings/README.md +14 -0
  93. data/lib/sendgrid/helpers/settings/mail_settings_dto.rb +13 -0
  94. data/lib/sendgrid/helpers/settings/partner_settings_dto.rb +13 -0
  95. data/lib/sendgrid/helpers/settings/settings.rb +28 -0
  96. data/lib/sendgrid/helpers/settings/tracking_settings_dto.rb +22 -0
  97. data/lib/sendgrid/helpers/settings/user_settings_dto.rb +13 -0
  98. data/lib/sendgrid/helpers/stats/email_stats.rb +46 -0
  99. data/lib/sendgrid/helpers/stats/metrics.rb +33 -0
  100. data/lib/sendgrid/helpers/stats/stats_response.rb +29 -0
  101. data/lib/sendgrid/sendgrid.rb +21 -0
  102. data/lib/sendgrid/twilio_email.rb +21 -0
  103. data/lib/sendgrid/version.rb +1 -1
  104. data/lib/sendgrid-ruby.rb +32 -7
  105. data/mail_helper_v3.md +390 -0
  106. data/sendgrid-ruby.gemspec +16 -20
  107. data/spec/fixtures/event_webhook.rb +22 -0
  108. data/spec/rack/sendgrid_webhook_verification_spec.rb +142 -0
  109. data/spec/sendgrid/helpers/eventwebhook/eventwebhook_spec.rb +105 -0
  110. data/spec/sendgrid/helpers/ip_management/ip_management_spec.rb +12 -0
  111. data/spec/sendgrid/helpers/settings/mail_settings_dto_spec.rb +32 -0
  112. data/spec/sendgrid/helpers/settings/partner_settings_dto_spec.rb +24 -0
  113. data/spec/sendgrid/helpers/settings/settings_spec.rb +25 -0
  114. data/spec/sendgrid/helpers/settings/tracking_settings_dto_spec.rb +27 -0
  115. data/spec/sendgrid/helpers/settings/user_settings_dto_spec.rb +24 -0
  116. data/spec/sendgrid/helpers/stats/email_stats_spec.rb +111 -0
  117. data/spec/sendgrid/helpers/stats/metrics_spec.rb +45 -0
  118. data/spec/sendgrid/helpers/stats/stats_response_spec.rb +75 -0
  119. data/spec/sendgrid/sendgrid_spec.rb +11 -0
  120. data/spec/sendgrid/twilio_email_spec.rb +11 -0
  121. data/spec/spec_helper.rb +12 -1
  122. data/static/img/github-fork.png +0 -0
  123. data/static/img/github-sign-up.png +0 -0
  124. data/test/sendgrid/helpers/mail/test_attachment.rb +33 -0
  125. data/test/sendgrid/helpers/mail/test_category.rb +25 -0
  126. data/test/sendgrid/helpers/mail/test_email.rb +41 -0
  127. data/test/sendgrid/helpers/mail/test_mail.rb +260 -0
  128. data/test/sendgrid/helpers/mail/test_personalizations.rb +214 -0
  129. data/test/sendgrid/permissions/test_scopes.rb +36 -0
  130. data/test/sendgrid/test_sendgrid-ruby.rb +2729 -0
  131. data/twilio_sendgrid_logo.png +0 -0
  132. data/use-cases/README.md +17 -0
  133. data/use-cases/domain-authentication.md +5 -0
  134. data/use-cases/email-statistics.md +52 -0
  135. data/use-cases/legacy-templates.md +98 -0
  136. data/use-cases/personalizations.md +34 -0
  137. data/use-cases/sms.md +39 -0
  138. data/use-cases/transactional-templates.md +111 -0
  139. data/use-cases/twilio-email.md +13 -0
  140. data/use-cases/twilio-setup.md +54 -0
  141. metadata +187 -112
  142. data/.rspec +0 -2
  143. data/.travis.yml +0 -20
  144. data/FETCH_HEAD +0 -0
  145. data/Guardfile +0 -10
  146. data/LICENSE.txt +0 -22
  147. data/example.rb +0 -41
  148. data/lib/sendgrid/client.rb +0 -62
  149. data/lib/sendgrid/exceptions.rb +0 -7
  150. data/lib/sendgrid/mail.rb +0 -182
  151. data/lib/sendgrid/recipient.rb +0 -29
  152. data/lib/sendgrid/response.rb +0 -14
  153. data/lib/sendgrid/template.rb +0 -26
  154. data/lib/sendgrid/template_mailer.rb +0 -59
  155. data/spec/lib/sendgrid/client_spec.rb +0 -87
  156. data/spec/lib/sendgrid/mail_spec.rb +0 -151
  157. data/spec/lib/sendgrid/recipient_spec.rb +0 -91
  158. data/spec/lib/sendgrid/template_mailer_spec.rb +0 -86
  159. data/spec/lib/sendgrid/template_spec.rb +0 -61
  160. data/spec/lib/sendgrid_spec.rb +0 -7
data/mail_helper_v3.md ADDED
@@ -0,0 +1,390 @@
1
+ Hello!
2
+
3
+ It is now time to implement the final piece of our v2 to v3 migration. Before we dig into writing the code, we would love to get feedback on the following proposed interfaces.
4
+
5
+ We are starting with the use cases below for the first iteration. (we have completed this work on [our C# library](https://github.com/sendgrid/sendgrid-csharp/blob/HEAD/USE_CASES.md), you can check that out for a sneak peek of where we are heading).
6
+
7
+ # Send a Single Email to a Single Recipient
8
+
9
+ The following code assumes you are storing the API key in an [environment variable (recommended)](TROUBLESHOOTING.md#environment-variables-and-your-sendgrid-api-key). If you don't have your key stored in an environment variable, you can assign it directly to `api_key` for testing purposes.
10
+
11
+ ```ruby
12
+ require 'sendgrid-ruby'
13
+
14
+ from = SendGrid::Email.new('test@example.com', 'Example User')
15
+ to = SendGrid::Email.new('test@example.com', 'Example User')
16
+ subject = 'Sending with Twilio SendGrid is Fun'
17
+ plain_text_content = 'and easy to do anywhere, even with Ruby'
18
+ html_content = '<strong>and easy to do anywhere, even with Ruby</strong>'
19
+ msg = SendGrid::Mail.create(from: from,
20
+ tos: to,
21
+ subject: subject,
22
+ plain_text_content: plain_text_content,
23
+ html_content: html_content,
24
+ substitutions: {})
25
+
26
+ client = SendGrid::Client.new(api_key: ENV['SENDGRID_API_KEY'])
27
+
28
+ begin
29
+ response = client.send_email(msg)
30
+ rescue Exception => e
31
+ puts e.message
32
+ end
33
+ puts response.status_code
34
+ puts response.body
35
+ puts response.headers
36
+ ```
37
+
38
+ # Send a Single Email to Multiple Recipients
39
+
40
+ The following code assumes you are storing the API key in an [environment variable (recommended)](TROUBLESHOOTING.md#environment-variables-and-your-sendgrid-api-key). If you don't have your key stored in an environment variable, you can assign it directly to `api_key` for testing purposes.
41
+
42
+ ```ruby
43
+ require 'sendgrid-ruby'
44
+
45
+ from = SendGrid::Email.new('test@example.com', 'Example User')
46
+ tos = [
47
+ SendGrid::Email.new('test1@example.com', 'Example User1'),
48
+ SendGrid::Email.new('test2@example.com', 'Example User2'),
49
+ SendGrid::Email.new('test3@example.com', 'Example User3')
50
+ ];
51
+ subject = 'Sending with Twilio SendGrid is Fun'
52
+ plain_text_content = 'and easy to do anywhere, even with Ruby'
53
+ html_content = '<strong>and easy to do anywhere, even with Ruby</strong>'
54
+ msg = SendGrid::Mail.create(from: from,
55
+ tos: tos,
56
+ subject: subject,
57
+ plain_text_content: plain_text_content,
58
+ html_content: html_content,
59
+ substitutions: {})
60
+
61
+ client = SendGrid::Client.new(api_key: ENV['SENDGRID_API_KEY'])
62
+
63
+ begin
64
+ response = client.send_email(msg)
65
+ rescue Exception => e
66
+ puts e.message
67
+ end
68
+ puts response.status_code
69
+ puts response.body
70
+ puts response.headers
71
+ ```
72
+
73
+ # Send Multiple Emails to Multiple Recipients
74
+
75
+ The following code assumes you are storing the API key in an [environment variable (recommended)](TROUBLESHOOTING.md#environment-variables-and-your-sendgrid-api-key). If you don't have your key stored in an environment variable, you can assign it directly to `api_key` for testing purposes.
76
+
77
+ ```ruby
78
+ require 'sendgrid-ruby'
79
+
80
+ from = SendGrid::Email.new('test@example.com', 'Example User')
81
+ tos = [
82
+ SendGrid::Email.new('test1@example.com', 'Example User1'),
83
+ SendGrid::Email.new('test2@example.com', 'Example User2'),
84
+ SendGrid::Email.new('test3@example.com', 'Example User3')
85
+ ];
86
+ subjects = [
87
+ 'Sending with Twilio SendGrid is Fun',
88
+ 'Sending with Twilio SendGrid is Super Fun',
89
+ 'Sending with Twilio SendGrid is Super Duper Fun'
90
+ ];
91
+ plain_text_content = 'and easy to do anywhere, even with Ruby'
92
+ html_content = '<strong>and easy to do anywhere, even with Ruby</strong>'
93
+ values = [
94
+ 'Name 1',
95
+ 'Name 2',
96
+ 'Name 3'
97
+ ]
98
+ substitutions = {
99
+ '-name1-' => values
100
+ }
101
+ msg = SendGrid::Mail.create(from: from,
102
+ tos: tos,
103
+ subject: subjects,
104
+ plain_text_content: plain_text_content,
105
+ html_content: html_content,
106
+ substitutions: substitutions)
107
+
108
+ client = SendGrid::Client.new(api_key: ENV['SENDGRID_API_KEY'])
109
+
110
+ begin
111
+ response = client.send_email(msg)
112
+ rescue Exception => e
113
+ puts e.message
114
+ end
115
+ puts response.status_code
116
+ puts response.body
117
+ puts response.headers
118
+ ```
119
+
120
+ # Kitchen Sink - an example with all settings used
121
+
122
+ The following code assumes you are storing the API key in an [environment variable (recommended)](TROUBLESHOOTING.md#environment-variables-and-your-sendgrid-api-key). If you don't have your key stored in an environment variable, you can assign it directly to `api_key` for testing purposes.
123
+
124
+ ```ruby
125
+ client = SendGrid::Client.new(api_key: ENV['SENDGRID_API_KEY'])
126
+
127
+ from = SendGrid::Email.new('test@example.com', 'Example User')
128
+ to = SendGrid::Email.new('test@example.com', 'Example User')
129
+ subject = 'Sending with Twilio SendGrid is Fun'
130
+ plain_text_content = 'and easy to do anywhere, even with Ruby'
131
+ html_content = '<strong>and easy to do anywhere, even with Ruby</strong>'
132
+ msg = SendGrid::Message.new(from, to, subject, plain_text_content, html_content)
133
+
134
+ # For a detailed description of each of these settings, please see the [documentation](https://sendgrid.com/docs/API_Reference/api_v3.html).
135
+
136
+ msg.add_to(SendGrid::Email.new('test1@example.com', 'Example User1'))
137
+ to_emails = [
138
+ SendGrid::Email.new('test2@example.com', 'Example User2'),
139
+ SendGrid::Email.new('test3@example.com', 'Example User3')
140
+ ];
141
+ msg.add_tos(to_emails)
142
+
143
+ msg.add_cc(SendGrid::Email.new('test4@example.com', 'Example User4'))
144
+ cc_emails = [
145
+ SendGrid::Email.new('test5@example.com', 'Example User5'),
146
+ SendGrid::Email.new('test6@example.com', 'Example User6')
147
+ ];
148
+ msg.add_ccs(cc_emails)
149
+
150
+ msg.add_bcc(SendGrid::Email.new('test7@example.com', 'Example User7'))
151
+ bcc_emails = [
152
+ SendGrid::Email.new('test8@example.com', 'Example User8'),
153
+ SendGrid::Email.new('test9@example.com', 'Example User9')
154
+ ];
155
+ msg.add_bccs(bcc_emails)
156
+
157
+ msg.add_header('X-Test1', 'Test1')
158
+ msg.add_header('X-Test2', 'Test2')
159
+ headers = [
160
+ 'X-Test3' => 'Test3',
161
+ 'X-Test4' => 'Test4'
162
+ ]
163
+ msg.add_headers(headers)
164
+
165
+ msg.add_substitution('%name1%', 'Example Name 1');
166
+ msg.add_substitution('%city1%', 'Denver');
167
+ substitutions = [
168
+ '%name2%' => 'Example Name 2',
169
+ '%city2%' => 'Orange'
170
+ ]
171
+ msg.add_substitutions(substitutions)
172
+
173
+ msg.add_custom_arg('marketing1', 'false')
174
+ msg.add_custom_arg('transactional1', 'true')
175
+ custom_args = [
176
+ 'marketing2' => 'true',
177
+ 'transactional2' => 'false'
178
+ ]
179
+ msg.add_custom_args(custom_args)
180
+
181
+ msg.set_send_at(1461775051)
182
+
183
+ msg.set_subject('this subject overrides the Global Subject on the default Personalization')
184
+
185
+ # If you need to add more [Personalizations](https://sendgrid.com/docs/Classroom/Send/v3_Mail_Send/personalizations.html), here is an example of adding another Personalization by passing in a personalization index.
186
+
187
+ msg.add_to(SendGrid::Email.new('test10@example.com', 'Example User10'), 1)
188
+ to_emails = [
189
+ SendGrid::Email.new('test11@example.com', 'Example User11'),
190
+ SendGrid::Email.new('test12@example.com', 'Example User12')
191
+ ];
192
+ msg.add_tos(to_emails, 1)
193
+
194
+ msg.add_cc(SendGrid::Email.new('test13@example.com', 'Example User13'), 1)
195
+ cc_emails = [
196
+ SendGrid::Email.new('test14@example.com', 'Example User14'),
197
+ SendGrid::Email.new('test15@example.com', 'Example User15')
198
+ ];
199
+ msg.add_ccs(cc_emails, 1)
200
+
201
+ msg.add_bcc(SendGrid::Email.new('test16@example.com', 'Example User16'), 1)
202
+ bcc_emails = [
203
+ SendGrid::Email.new('test17@example.com', 'Example User17'),
204
+ SendGrid::Email.new('test18@example.com', 'Example User18')
205
+ ];
206
+ msg.add_bccs(bcc_emails, 1)
207
+
208
+ msg.add_header('X-Test5', 'Test5', 1)
209
+ msg.add_header('X-Test6', 'Test6', 1)
210
+ headers = [
211
+ 'X-Test7' => 'Test7',
212
+ 'X-Test8' => 'Test8'
213
+ ]
214
+ msg.add_headers(headers, 1)
215
+
216
+ msg.add_substitution('%name3%', 'Example Name 3', 1);
217
+ msg.add_substitution('%city3%', 'Redwood City', 1);
218
+ substitutions = [
219
+ '%name4%' => 'Example Name 4',
220
+ '%city4%' => 'London'
221
+ ]
222
+ msg.add_substitutions(substitutions, 1)
223
+
224
+ msg.add_custom_arg('marketing3', 'true', 1)
225
+ msg.add_custom_arg('transactional3', 'false', 1)
226
+ custom_args = [
227
+ 'marketing4' => 'false',
228
+ 'transactional4' => 'true'
229
+ ]
230
+ msg.add_custom_args(custom_args, 1)
231
+
232
+ msg.set_send_at(1461775052, 1)
233
+
234
+ msg.set_subject('this subject overrides the Global Subject on the second Personalization', 1)
235
+
236
+ # The values below this comment are global to the entire message
237
+
238
+ msg.set_from(SendGrid::Email.new('test0@example.com', 'Example User0'))
239
+
240
+ msg.set_global_subject('Sending with Twilio SendGrid is Fun');
241
+
242
+ msg.add_content(MimeType::Text, 'and easy to do anywhere, even with Ruby')
243
+ msg.add_content(MimeType::Html, '<strong>and easy to do anywhere, even with Ruby</strong>')
244
+ contents = [
245
+ SendGrid::Content.new('text/calendar', 'Party Time!!'),
246
+ SendGrid::Content.new('text/calendar2', 'Party Time 2!!')
247
+ ]
248
+ msg.add_contents(contents)
249
+
250
+ msg.add_attachment('balance_001.pdf',
251
+ 'base64 encoded content',
252
+ 'application/pdf',
253
+ 'attachment',
254
+ 'Balance Sheet')
255
+
256
+ attachments = [
257
+ SendGrid::Attachment.new('banner.png',
258
+ 'base64 encoded content',
259
+ 'image/png',
260
+ 'inline',
261
+ 'Banner'),
262
+ SendGrid::Attachment.new('banner2.png',
263
+ 'base64 encoded content',
264
+ 'image/png',
265
+ 'inline',
266
+ 'Banner 2'),
267
+ ]
268
+ msg.add_attachments(attachments)
269
+
270
+ msg.set_template_id('13b8f94f-bcae-4ec6-b752-70d6cb59f932')
271
+
272
+ msg.add_global_header('X-Day', 'Monday')
273
+ global_headers = [
274
+ 'X-Month' => 'January',
275
+ 'X-Year' => '2017'
276
+ ]
277
+ msg.set_global_headers(global_headers)
278
+
279
+ msg.add_section('%section1%', 'Substitution for Section 1 Tag')
280
+ sections = [
281
+ '%section2%' => 'Substitution for Section 2 Tag',
282
+ '%section3%' => 'Substitution for Section 3 Tag'
283
+ ]
284
+ msg.add_sections(sections)
285
+
286
+ begin
287
+ response = client.send_email(msg)
288
+ rescue Exception => e
289
+ puts e.message
290
+ end
291
+ puts response.status_code
292
+ puts response.body
293
+ puts response.headers
294
+ ```
295
+
296
+ # Attachments
297
+
298
+ The following code assumes you are storing the API key in an [environment variable (recommended)](TROUBLESHOOTING.md#environment-variables-and-your-sendgrid-api-key). If you don't have your key stored in an environment variable, you can assign it directly to `api_key` for testing purposes.
299
+
300
+ ```ruby
301
+ client = SendGrid::Client.new(api_key: ENV['SENDGRID_API_KEY'])
302
+
303
+ from = SendGrid::Email.new('test@example.com', 'Example User')
304
+ to = SendGrid::Email.new('test@example.com', 'Example User')
305
+ subject = 'Sending with Twilio SendGrid is Fun'
306
+ plain_text_content = 'and easy to do anywhere, even with Ruby'
307
+ html_content = '<strong>and easy to do anywhere, even with Ruby</strong>'
308
+ msg = SendGrid::Message.new(from, to, subject, plain_text_content, html_content)
309
+ bytes = File.read('/path/to/the/attachment.pdf')
310
+ encoded = Base64.encode64(bytes)
311
+ msg.add_attachment('balance_001.pdf',
312
+ encoded,
313
+ 'application/pdf',
314
+ 'attachment',
315
+ 'Balance Sheet')
316
+
317
+ begin
318
+ response = client.send_email(msg)
319
+ rescue Exception => e
320
+ puts e.message
321
+ end
322
+ puts response.status_code
323
+ puts response.body
324
+ puts response.headers
325
+ ```
326
+
327
+ # Transactional Templates
328
+
329
+ The following code assumes you are storing the API key in an [environment variable (recommended)](TROUBLESHOOTING.md#environment-variables-and-your-sendgrid-api-key). If you don't have your key stored in an environment variable, you can assign it directly to `api_key` for testing purposes.
330
+
331
+ For this example, we assume you have created a [legacy transactional template](https://sendgrid.com/docs/User_Guide/Transactional_Templates/index.html) in the UI or via the API. Following is the template content we used for testing.
332
+
333
+ Template ID (replace with your own):
334
+
335
+ ```text
336
+ 13b8f94f-bcae-4ec6-b752-70d6cb59f932
337
+ ```
338
+
339
+ Email Subject:
340
+
341
+ ```text
342
+ <%subject%>
343
+ ```
344
+
345
+ Template Body:
346
+
347
+ ```html
348
+ <html>
349
+ <head>
350
+ <title></title>
351
+ </head>
352
+ <body>
353
+ Hello -name-,
354
+ <br /><br/>
355
+ I'm glad you are trying out the template feature!
356
+ <br /><br/>
357
+ <%body%>
358
+ <br /><br/>
359
+ I hope you are having a great day in -city- :)
360
+ <br /><br/>
361
+ </body>
362
+ </html>
363
+ ```
364
+
365
+ ```ruby
366
+ client = SendGrid::Client.new(api_key: ENV['SENDGRID_API_KEY'])
367
+
368
+ from = SendGrid::Email.new('test@example.com', 'Example User')
369
+ to = SendGrid::Email.new('test@example.com', 'Example User')
370
+ subject = 'Sending with Twilio SendGrid is Fun'
371
+ plain_text_content = 'and easy to do anywhere, even with Ruby'
372
+ html_content = '<strong>and easy to do anywhere, even with Ruby</strong>'
373
+ msg = SendGrid::Message.new(from, to, subject, plain_text_content, html_content)
374
+
375
+ substitutions = [
376
+ '-name-' => 'Example User',
377
+ '-city-' => 'Denver'
378
+ ]
379
+ msg.add_substitutions(substitutions)
380
+ msg.set_template_id('13b8f94f-bcae-4ec6-b752-70d6cb59f932')
381
+
382
+ begin
383
+ response = client.send_email(msg)
384
+ rescue Exception => e
385
+ puts e.message
386
+ end
387
+ puts response.status_code
388
+ puts response.body
389
+ puts response.headers
390
+ ```
@@ -1,34 +1,30 @@
1
- # coding: utf-8
2
- lib = File.expand_path('../lib', __FILE__)
1
+ lib = File.expand_path('lib', __dir__)
3
2
  $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
3
  require 'sendgrid/version'
5
4
 
6
5
  Gem::Specification.new do |spec|
7
6
  spec.name = 'sendgrid-ruby'
8
7
  spec.version = SendGrid::VERSION
9
- spec.authors = ['Robin Johnson', 'Eddie Zaneski']
10
- spec.email = 'community@sendgrid.com'
11
- spec.summary = 'Official SendGrid Gem'
12
- spec.description = 'Interact with SendGrids API in native Ruby'
8
+ spec.authors = ['Elmer Thomas', 'Robin Johnson', 'Eddie Zaneski']
9
+ spec.email = 'help@twilio.com'
10
+ spec.summary = 'Official Twilio SendGrid Gem'
11
+ spec.description = 'Official Twilio SendGrid Gem to Interact with Twilio SendGrids API in native Ruby'
13
12
  spec.homepage = 'http://github.com/sendgrid/sendgrid-ruby'
14
- spec.license = 'MIT'
15
13
 
14
+ spec.required_ruby_version = '>= 2.2'
15
+
16
+ spec.license = 'MIT'
16
17
  spec.files = `git ls-files -z`.split("\x0")
17
18
  spec.executables = spec.files.grep(/^bin/) { |f| File.basename(f) }
18
19
  spec.test_files = spec.files.grep(/^(test|spec|features)/)
19
20
  spec.require_paths = ['lib']
20
-
21
- spec.add_dependency 'smtpapi', '~> 0.1'
22
- spec.add_dependency 'faraday', '~> 0.9'
23
- spec.add_dependency 'mimemagic'
24
- spec.add_development_dependency 'rake'
21
+ spec.add_dependency 'ruby_http_client', '~> 3.4'
22
+ spec.add_development_dependency 'faker'
23
+ spec.add_development_dependency 'minitest', '~> 5.9'
24
+ spec.add_development_dependency 'pry'
25
+ spec.add_development_dependency 'rack'
26
+ spec.add_development_dependency 'rake', '~> 13.0'
25
27
  spec.add_development_dependency 'rspec'
26
- spec.add_development_dependency 'rspec-nc'
27
- spec.add_development_dependency 'webmock'
28
- spec.add_development_dependency 'guard'
29
- spec.add_development_dependency 'guard-rspec'
30
- spec.add_development_dependency 'rubocop'
31
- spec.add_development_dependency 'guard-rubocop'
32
- spec.add_development_dependency 'ruby_gntp'
33
- spec.add_development_dependency 'bundler', '~> 1.6'
28
+ spec.add_development_dependency 'simplecov', '~> 0.18.5'
29
+ spec.add_development_dependency 'sinatra', '>= 1.4.7', '< 3'
34
30
  end
@@ -0,0 +1,22 @@
1
+ require "json"
2
+
3
+ module Fixtures
4
+ module EventWebhook
5
+ PUBLIC_KEY = 'MFkwEwYHKoZIzj0CAQYIKoZIzj0DAQcDQgAE83T4O/n84iotIvIW4mdBgQ/7dAfSmpqIM8kF9mN1flpVKS3GRqe62gw+2fNNRaINXvVpiglSI8eNEc6wEA3F+g=='.freeze
6
+ FAILING_PUBLIC_KEY = 'MFkwEwYHKoZIzj0CAQYIKoZIzj0DAQcDQgAEqTxd43gyp8IOEto2LdIfjRQrIbsd4SXZkLW6jDutdhXSJCWHw8REntlo7aNDthvj+y7GjUuFDb/R1NGe1OPzpA=='.freeze
7
+ SIGNATURE = 'MEUCIGHQVtGj+Y3LkG9fLcxf3qfI10QysgDWmMOVmxG0u6ZUAiEAyBiXDWzM+uOe5W0JuG+luQAbPIqHh89M15TluLtEZtM='.freeze
8
+ FAILING_SIGNATURE = 'MEUCIQCtIHJeH93Y+qpYeWrySphQgpNGNr/U+UyUlBkU6n7RAwIgJTz2C+8a8xonZGi6BpSzoQsbVRamr2nlxFDWYNH3j/0='.freeze
9
+ TIMESTAMP = '1600112502'.freeze
10
+ PAYLOAD = "#{[
11
+ {
12
+ email: 'hello@world.com',
13
+ event: 'dropped',
14
+ reason: 'Bounced Address',
15
+ sg_event_id: 'ZHJvcC0xMDk5NDkxOS1MUnpYbF9OSFN0T0doUTRrb2ZTbV9BLTA',
16
+ sg_message_id: 'LRzXl_NHStOGhQ4kofSm_A.filterdrecv-p3mdw1-756b745b58-kmzbl-18-5F5FC76C-9.0',
17
+ 'smtp-id': '<LRzXl_NHStOGhQ4kofSm_A@ismtpd0039p1iad1.sendgrid.net>',
18
+ timestamp: 1_600_112_492
19
+ }
20
+ ].to_json}\r\n".freeze # Be sure to include the trailing carriage return and newline!
21
+ end
22
+ end
@@ -0,0 +1,142 @@
1
+ require 'spec_helper'
2
+ require 'rack/mock'
3
+ require './spec/fixtures/event_webhook'
4
+
5
+ unless RUBY_PLATFORM == 'java'
6
+ describe Rack::SendGridWebhookVerification do
7
+ let(:public_key) { Fixtures::EventWebhook::PUBLIC_KEY }
8
+ before do
9
+ @app = ->(_env) { [200, { 'Content-Type' => 'text/plain' }, ['Hello']] }
10
+ end
11
+
12
+ describe 'new' do
13
+ it 'should initialize with an app, public key and a path' do
14
+ expect do
15
+ Rack::SendGridWebhookVerification.new(@app, 'ABC', %r{/email})
16
+ end.not_to raise_error
17
+ end
18
+
19
+ it 'should initialize with an app, public key and paths' do
20
+ expect do
21
+ Rack::SendGridWebhookVerification.new(@app, 'ABC', %r{/email}, %r{/event})
22
+ end.not_to raise_error
23
+ end
24
+ end
25
+
26
+ describe 'calling against one path' do
27
+ let(:middleware) { Rack::SendGridWebhookVerification.new(@app, public_key, %r{/email}) }
28
+
29
+ it "should not intercept when the path doesn't match" do
30
+ expect(SendGrid::EventWebhook).to_not receive(:new)
31
+ request = Rack::MockRequest.env_for('/login')
32
+ status, headers, body = middleware.call(request)
33
+ expect(status).to eq(200)
34
+ end
35
+
36
+ it 'should allow a request through if it is verified' do
37
+ options = {
38
+ :input => Fixtures::EventWebhook::PAYLOAD,
39
+ 'Content-Type' => "application/json"
40
+ }
41
+ options[SendGrid::EventWebhookHeader::SIGNATURE] = Fixtures::EventWebhook::SIGNATURE
42
+ options[SendGrid::EventWebhookHeader::TIMESTAMP] = Fixtures::EventWebhook::TIMESTAMP
43
+ request = Rack::MockRequest.env_for('/email', options)
44
+ status, headers, body = middleware.call(request)
45
+ expect(status).to eq(200)
46
+ end
47
+
48
+ it 'should short circuit a request to 403 if there is no signature or timestamp' do
49
+ options = {
50
+ :input => Fixtures::EventWebhook::PAYLOAD,
51
+ 'Content-Type' => "application/json"
52
+ }
53
+ request = Rack::MockRequest.env_for('/email', options)
54
+ status, headers, body = middleware.call(request)
55
+ expect(status).to eq(403)
56
+ end
57
+
58
+ it 'should short circuit a request to 403 if the signature is incorrect' do
59
+ options = {
60
+ :input => Fixtures::EventWebhook::PAYLOAD,
61
+ 'Content-Type' => "application/json"
62
+ }
63
+ options[SendGrid::EventWebhookHeader::SIGNATURE] = Fixtures::EventWebhook::FAILING_SIGNATURE
64
+ options[SendGrid::EventWebhookHeader::TIMESTAMP] = Fixtures::EventWebhook::TIMESTAMP
65
+ request = Rack::MockRequest.env_for('/email', options)
66
+ status, headers, body = middleware.call(request)
67
+ expect(status).to eq(403)
68
+ end
69
+
70
+ it 'should short circuit a request to 403 if the payload is incorrect' do
71
+ options = {
72
+ :input => 'payload',
73
+ 'Content-Type' => "application/json"
74
+ }
75
+ options[SendGrid::EventWebhookHeader::SIGNATURE] = Fixtures::EventWebhook::SIGNATURE
76
+ options[SendGrid::EventWebhookHeader::TIMESTAMP] = Fixtures::EventWebhook::TIMESTAMP
77
+ request = Rack::MockRequest.env_for('/email', options)
78
+ status, headers, body = middleware.call(request)
79
+ expect(status).to eq(403)
80
+ end
81
+ end
82
+
83
+ describe 'calling with multiple paths' do
84
+ let(:middleware) { Rack::SendGridWebhookVerification.new(@app, public_key, %r{/email}, %r{/events}) }
85
+
86
+ it "should not intercept when the path doesn't match" do
87
+ expect(SendGrid::EventWebhook).to_not receive(:new)
88
+ request = Rack::MockRequest.env_for('/sms_events')
89
+ status, headers, body = middleware.call(request)
90
+ expect(status).to eq(200)
91
+ end
92
+
93
+ it 'should allow a request through if it is verified' do
94
+ options = {
95
+ :input => Fixtures::EventWebhook::PAYLOAD,
96
+ 'Content-Type' => "application/json"
97
+ }
98
+ options[SendGrid::EventWebhookHeader::SIGNATURE] = Fixtures::EventWebhook::SIGNATURE
99
+ options[SendGrid::EventWebhookHeader::TIMESTAMP] = Fixtures::EventWebhook::TIMESTAMP
100
+ request = Rack::MockRequest.env_for('/events', options)
101
+ status, headers, body = middleware.call(request)
102
+ expect(status).to eq(200)
103
+ end
104
+
105
+ it 'should short circuit a request to 403 if there is no signature or timestamp' do
106
+ options = {
107
+ :input => Fixtures::EventWebhook::PAYLOAD,
108
+ 'Content-Type' => "application/json"
109
+ }
110
+ request = Rack::MockRequest.env_for('/events', options)
111
+ status, headers, body = middleware.call(request)
112
+ expect(status).to eq(403)
113
+ end
114
+ end
115
+
116
+ describe 'request body which passed to an app' do
117
+ before do
118
+ @payload = nil
119
+ @spy_app = lambda do |env|
120
+ @payload = Rack::Request.new(env).body
121
+ [200, { 'Content-Type' => 'text/plain' }, ['Hello']]
122
+ end
123
+ end
124
+
125
+ let(:middleware) { Rack::SendGridWebhookVerification.new(@spy_app, public_key, %r{/email}) }
126
+
127
+ it 'keeps orignal reading position' do
128
+ options = {
129
+ :input => Fixtures::EventWebhook::PAYLOAD,
130
+ 'Content-Type' => "application/json"
131
+ }
132
+ options[SendGrid::EventWebhookHeader::SIGNATURE] = Fixtures::EventWebhook::SIGNATURE
133
+ options[SendGrid::EventWebhookHeader::TIMESTAMP] = Fixtures::EventWebhook::TIMESTAMP
134
+ request = Rack::MockRequest.env_for('/email', options)
135
+ status, headers, body = middleware.call(request)
136
+ expect(status).to eq(200)
137
+ expect(@payload).not_to be_nil
138
+ expect(@payload.pos).to be_zero
139
+ end
140
+ end
141
+ end
142
+ end