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
@@ -0,0 +1,392 @@
1
+ require 'sendgrid-ruby'
2
+
3
+ sg = SendGrid::API.new(api_key: ENV['SENDGRID_API_KEY'])
4
+
5
+ ##################################################
6
+ # Create a Custom Field #
7
+ # POST /contactdb/custom_fields #
8
+
9
+ data = JSON.parse('{
10
+ "name": "pet",
11
+ "type": "text"
12
+ }')
13
+ response = sg.client.contactdb.custom_fields.post(request_body: data)
14
+ puts response.status_code
15
+ puts response.body
16
+ puts response.headers
17
+
18
+ ##################################################
19
+ # Retrieve all custom fields #
20
+ # GET /contactdb/custom_fields #
21
+
22
+ response = sg.client.contactdb.custom_fields.get
23
+ puts response.status_code
24
+ puts response.body
25
+ puts response.headers
26
+
27
+ ##################################################
28
+ # Retrieve a Custom Field #
29
+ # GET /contactdb/custom_fields/{custom_field_id} #
30
+
31
+ custom_field_id = 'test_url_param'
32
+ response = sg.client.contactdb.custom_fields._(custom_field_id).get
33
+ puts response.status_code
34
+ puts response.body
35
+ puts response.headers
36
+
37
+ ##################################################
38
+ # Delete a Custom Field #
39
+ # DELETE /contactdb/custom_fields/{custom_field_id} #
40
+
41
+ custom_field_id = 'test_url_param'
42
+ response = sg.client.contactdb.custom_fields._(custom_field_id).delete
43
+ puts response.status_code
44
+ puts response.body
45
+ puts response.headers
46
+
47
+ ##################################################
48
+ # Create a List #
49
+ # POST /contactdb/lists #
50
+
51
+ data = JSON.parse('{
52
+ "name": "your list name"
53
+ }')
54
+ response = sg.client.contactdb.lists.post(request_body: data)
55
+ puts response.status_code
56
+ puts response.body
57
+ puts response.headers
58
+
59
+ ##################################################
60
+ # Retrieve all lists #
61
+ # GET /contactdb/lists #
62
+
63
+ response = sg.client.contactdb.lists.get
64
+ puts response.status_code
65
+ puts response.body
66
+ puts response.headers
67
+
68
+ ##################################################
69
+ # Delete Multiple lists #
70
+ # DELETE /contactdb/lists #
71
+
72
+ data = JSON.parse('[
73
+ 1,
74
+ 2,
75
+ 3,
76
+ 4
77
+ ]')
78
+ response = sg.client.contactdb.lists.delete(request_body: data)
79
+ puts response.status_code
80
+ puts response.body
81
+ puts response.headers
82
+
83
+ ##################################################
84
+ # Update a List #
85
+ # PATCH /contactdb/lists/{list_id} #
86
+
87
+ data = JSON.parse('{
88
+ "name": "newlistname"
89
+ }')
90
+ params = JSON.parse('{"list_id": 1}')
91
+ list_id = 'test_url_param'
92
+ response = sg.client.contactdb.lists._(list_id).patch(request_body: data, query_params: params)
93
+ puts response.status_code
94
+ puts response.body
95
+ puts response.headers
96
+
97
+ ##################################################
98
+ # Retrieve a single list #
99
+ # GET /contactdb/lists/{list_id} #
100
+
101
+ params = JSON.parse('{"list_id": 1}')
102
+ list_id = 'test_url_param'
103
+ response = sg.client.contactdb.lists._(list_id).get(query_params: params)
104
+ puts response.status_code
105
+ puts response.body
106
+ puts response.headers
107
+
108
+ ##################################################
109
+ # Delete a List #
110
+ # DELETE /contactdb/lists/{list_id} #
111
+
112
+ params = JSON.parse('{"delete_contacts": "true"}')
113
+ list_id = 'test_url_param'
114
+ response = sg.client.contactdb.lists._(list_id).delete(query_params: params)
115
+ puts response.status_code
116
+ puts response.body
117
+ puts response.headers
118
+
119
+ ##################################################
120
+ # Add Multiple Recipients to a List #
121
+ # POST /contactdb/lists/{list_id}/recipients #
122
+
123
+ data = JSON.parse('[
124
+ "recipient_id1",
125
+ "recipient_id2"
126
+ ]')
127
+ list_id = 'test_url_param'
128
+ response = sg.client.contactdb.lists._(list_id).recipients.post(request_body: data)
129
+ puts response.status_code
130
+ puts response.body
131
+ puts response.headers
132
+
133
+ ##################################################
134
+ # Retrieve all recipients on a List #
135
+ # GET /contactdb/lists/{list_id}/recipients #
136
+
137
+ params = JSON.parse('{"page": 1, "page_size": 1, "list_id": 1}')
138
+ list_id = 'test_url_param'
139
+ response = sg.client.contactdb.lists._(list_id).recipients.get(query_params: params)
140
+ puts response.status_code
141
+ puts response.body
142
+ puts response.headers
143
+
144
+ ##################################################
145
+ # Add a Single Recipient to a List #
146
+ # POST /contactdb/lists/{list_id}/recipients/{recipient_id} #
147
+
148
+ list_id = 'test_url_param'
149
+ recipient_id = 'test_url_param'
150
+ response = sg.client.contactdb.lists._(list_id).recipients._(recipient_id).post
151
+ puts response.status_code
152
+ puts response.body
153
+ puts response.headers
154
+
155
+ ##################################################
156
+ # Delete a Single Recipient from a Single List #
157
+ # DELETE /contactdb/lists/{list_id}/recipients/{recipient_id} #
158
+
159
+ params = JSON.parse('{"recipient_id": 1, "list_id": 1}')
160
+ list_id = 'test_url_param'
161
+ recipient_id = 'test_url_param'
162
+ response = sg.client.contactdb.lists._(list_id).recipients._(recipient_id).delete(query_params: params)
163
+ puts response.status_code
164
+ puts response.body
165
+ puts response.headers
166
+
167
+ ##################################################
168
+ # Update Recipient #
169
+ # PATCH /contactdb/recipients #
170
+
171
+ data = JSON.parse('[
172
+ {
173
+ "email": "jones@example.com",
174
+ "first_name": "Guy",
175
+ "last_name": "Jones"
176
+ }
177
+ ]')
178
+ response = sg.client.contactdb.recipients.patch(request_body: data)
179
+ puts response.status_code
180
+ puts response.body
181
+ puts response.headers
182
+
183
+ ##################################################
184
+ # Add recipients #
185
+ # POST /contactdb/recipients #
186
+
187
+ data = JSON.parse('[
188
+ {
189
+ "age": 25,
190
+ "email": "example@example.com",
191
+ "first_name": "",
192
+ "last_name": "User"
193
+ },
194
+ {
195
+ "age": 25,
196
+ "email": "example2@example.com",
197
+ "first_name": "Example",
198
+ "last_name": "User"
199
+ }
200
+ ]')
201
+ response = sg.client.contactdb.recipients.post(request_body: data)
202
+ puts response.status_code
203
+ puts response.body
204
+ puts response.headers
205
+
206
+ ##################################################
207
+ # Retrieve recipients #
208
+ # GET /contactdb/recipients #
209
+
210
+ params = JSON.parse('{"page": 1, "page_size": 1}')
211
+ response = sg.client.contactdb.recipients.get(query_params: params)
212
+ puts response.status_code
213
+ puts response.body
214
+ puts response.headers
215
+
216
+ ##################################################
217
+ # Delete Recipient #
218
+ # DELETE /contactdb/recipients #
219
+
220
+ data = JSON.parse('[
221
+ "recipient_id1",
222
+ "recipient_id2"
223
+ ]')
224
+ response = sg.client.contactdb.recipients.delete(request_body: data)
225
+ puts response.status_code
226
+ puts response.body
227
+ puts response.headers
228
+
229
+ ##################################################
230
+ # Retrieve the count of billable recipients #
231
+ # GET /contactdb/recipients/billable_count #
232
+
233
+ response = sg.client.contactdb.recipients.billable_count.get
234
+ puts response.status_code
235
+ puts response.body
236
+ puts response.headers
237
+
238
+ ##################################################
239
+ # Retrieve a Count of Recipients #
240
+ # GET /contactdb/recipients/count #
241
+
242
+ response = sg.client.contactdb.recipients.count.get
243
+ puts response.status_code
244
+ puts response.body
245
+ puts response.headers
246
+
247
+ ##################################################
248
+ # Retrieve recipients matching search criteria #
249
+ # GET /contactdb/recipients/search #
250
+
251
+ params = JSON.parse('{"{field_name}": "test_string"}')
252
+ response = sg.client.contactdb.recipients.search.get(query_params: params)
253
+ puts response.status_code
254
+ puts response.body
255
+ puts response.headers
256
+
257
+ ##################################################
258
+ # Retrieve a single recipient #
259
+ # GET /contactdb/recipients/{recipient_id} #
260
+
261
+ recipient_id = 'test_url_param'
262
+ response = sg.client.contactdb.recipients._(recipient_id).get
263
+ puts response.status_code
264
+ puts response.body
265
+ puts response.headers
266
+
267
+ ##################################################
268
+ # Delete a Recipient #
269
+ # DELETE /contactdb/recipients/{recipient_id} #
270
+
271
+ recipient_id = 'test_url_param'
272
+ response = sg.client.contactdb.recipients._(recipient_id).delete
273
+ puts response.status_code
274
+ puts response.body
275
+ puts response.headers
276
+
277
+ ##################################################
278
+ # Retrieve the lists that a recipient is on #
279
+ # GET /contactdb/recipients/{recipient_id}/lists #
280
+
281
+ recipient_id = 'test_url_param'
282
+ response = sg.client.contactdb.recipients._(recipient_id).lists.get
283
+ puts response.status_code
284
+ puts response.body
285
+ puts response.headers
286
+
287
+ ##################################################
288
+ # Retrieve reserved fields #
289
+ # GET /contactdb/reserved_fields #
290
+
291
+ response = sg.client.contactdb.reserved_fields.get
292
+ puts response.status_code
293
+ puts response.body
294
+ puts response.headers
295
+
296
+ ##################################################
297
+ # Create a Segment #
298
+ # POST /contactdb/segments #
299
+
300
+ data = JSON.parse('{
301
+ "conditions": [
302
+ {
303
+ "and_or": "",
304
+ "field": "last_name",
305
+ "operator": "eq",
306
+ "value": "Miller"
307
+ },
308
+ {
309
+ "and_or": "and",
310
+ "field": "last_clicked",
311
+ "operator": "gt",
312
+ "value": "01/02/2015"
313
+ },
314
+ {
315
+ "and_or": "or",
316
+ "field": "clicks.campaign_identifier",
317
+ "operator": "eq",
318
+ "value": "513"
319
+ }
320
+ ],
321
+ "list_id": 4,
322
+ "name": "Last Name Miller"
323
+ }')
324
+ response = sg.client.contactdb.segments.post(request_body: data)
325
+ puts response.status_code
326
+ puts response.body
327
+ puts response.headers
328
+
329
+ ##################################################
330
+ # Retrieve all segments #
331
+ # GET /contactdb/segments #
332
+
333
+ response = sg.client.contactdb.segments.get
334
+ puts response.status_code
335
+ puts response.body
336
+ puts response.headers
337
+
338
+ ##################################################
339
+ # Update a segment #
340
+ # PATCH /contactdb/segments/{segment_id} #
341
+
342
+ data = JSON.parse('{
343
+ "conditions": [
344
+ {
345
+ "and_or": "",
346
+ "field": "last_name",
347
+ "operator": "eq",
348
+ "value": "Miller"
349
+ }
350
+ ],
351
+ "list_id": 5,
352
+ "name": "The Millers"
353
+ }')
354
+ params = JSON.parse('{"segment_id": "test_string"}')
355
+ segment_id = 'test_url_param'
356
+ response = sg.client.contactdb.segments._(segment_id).patch(request_body: data, query_params: params)
357
+ puts response.status_code
358
+ puts response.body
359
+ puts response.headers
360
+
361
+ ##################################################
362
+ # Retrieve a segment #
363
+ # GET /contactdb/segments/{segment_id} #
364
+
365
+ params = JSON.parse('{"segment_id": 1}')
366
+ segment_id = 'test_url_param'
367
+ response = sg.client.contactdb.segments._(segment_id).get(query_params: params)
368
+ puts response.status_code
369
+ puts response.body
370
+ puts response.headers
371
+
372
+ ##################################################
373
+ # Delete a segment #
374
+ # DELETE /contactdb/segments/{segment_id} #
375
+
376
+ params = JSON.parse('{"delete_contacts": "true"}')
377
+ segment_id = 'test_url_param'
378
+ response = sg.client.contactdb.segments._(segment_id).delete(query_params: params)
379
+ puts response.status_code
380
+ puts response.body
381
+ puts response.headers
382
+
383
+ ##################################################
384
+ # Retrieve recipients on a segment #
385
+ # GET /contactdb/segments/{segment_id}/recipients #
386
+
387
+ params = JSON.parse('{"page": 1, "page_size": 1}')
388
+ segment_id = 'test_url_param'
389
+ response = sg.client.contactdb.segments._(segment_id).recipients.get(query_params: params)
390
+ puts response.status_code
391
+ puts response.body
392
+ puts response.headers
@@ -0,0 +1,13 @@
1
+ require 'sendgrid-ruby'
2
+
3
+ sg = SendGrid::API.new(api_key: ENV['SENDGRID_API_KEY'])
4
+
5
+ ##################################################
6
+ # Retrieve email statistics by device type. #
7
+ # GET /devices/stats #
8
+
9
+ params = JSON.parse('{"aggregated_by": "day", "limit": 1, "start_date": "2016-01-01", "end_date": "2016-04-01", "offset": 1}')
10
+ response = sg.client.devices.stats.get(query_params: params)
11
+ puts response.status_code
12
+ puts response.body
13
+ puts response.headers
@@ -0,0 +1,52 @@
1
+ require 'sendgrid-ruby'
2
+
3
+ sg = SendGrid::API.new(api_key: ENV['SENDGRID_API_KEY'])
4
+
5
+ ##################################################
6
+ # Filter all messages #
7
+ # GET /messages #
8
+
9
+ require 'erb'
10
+
11
+ filter_key = 'to_email'
12
+ filter_operator = ERB::Util.url_encode('=')
13
+ filter_value = 'testing@sendgrid.net'
14
+ filter_value = ERB::Util.url_encode(format('"%s"', filter_value))
15
+ query_params = {}
16
+ query_params['query'] = format("%s%s%s", filter_key, filter_operator, filter_value)
17
+ query_params['limit'] = '1'
18
+
19
+ params = query_params
20
+ response = sg.client.messages.get(query_params: params)
21
+ puts response.status_code
22
+ puts response.body
23
+ puts response.headers
24
+
25
+ ##################################################
26
+ # Filter messages by message ID #
27
+ # GET /messages/{msg_id} #
28
+
29
+ msg_id = "test_url_param"
30
+ response = sg.client.messages._(msg_id).get
31
+ puts response.status_code
32
+ puts response.body
33
+ puts response.headers
34
+
35
+ ##################################################
36
+ # Request a CSV #
37
+ # POST /messages/download #
38
+
39
+ response = sg.client.messages.download.post
40
+ puts response.status_code
41
+ puts response.body
42
+ puts response.headers
43
+
44
+ ##################################################
45
+ # Download CSV #
46
+ # GET /messages/download/{download_uuid} #
47
+
48
+ download_uuid = "test_url_param"
49
+ response = sg.client.messages.download._(download_uuid).get
50
+ puts response.status_code
51
+ puts response.body
52
+ puts response.headers
@@ -0,0 +1,13 @@
1
+ require 'sendgrid-ruby'
2
+
3
+ sg = SendGrid::API.new(api_key: ENV['SENDGRID_API_KEY'])
4
+
5
+ ##################################################
6
+ # Retrieve email statistics by country and state/province. #
7
+ # GET /geo/stats #
8
+
9
+ params = JSON.parse('{"end_date": "2016-04-01", "country": "US", "aggregated_by": "day", "limit": 1, "offset": 1, "start_date": "2016-01-01"}')
10
+ response = sg.client.geo.stats.get(query_params: params)
11
+ puts response.status_code
12
+ puts response.body
13
+ puts response.headers
@@ -0,0 +1,16 @@
1
+ require 'sengrid-ruby'
2
+ include SendGrid
3
+
4
+ def is_valid_signature(request)
5
+ public_key = 'base64-encoded public key'
6
+
7
+ event_webhook = SendGrid::EventWebhook.new
8
+ ec_public_key = event_webhook.convert_public_key_to_ecdsa(public_key)
9
+
10
+ event_webhook.verify_signature(
11
+ ec_public_key,
12
+ request.body.read,
13
+ request.env[SendGrid::EventWebhookHeader::SIGNATURE],
14
+ request.env[SendGrid::EventWebhookHeader::TIMESTAMP]
15
+ )
16
+ end
@@ -0,0 +1,158 @@
1
+ require 'sendgrid-ruby'
2
+ include SendGrid
3
+ require 'json'
4
+
5
+ def hello_world
6
+ from = Email.new(email: 'test@example.com')
7
+ subject = 'Hello World from the Twilio SendGrid Ruby Library'
8
+ to = Email.new(email: 'test@example.com')
9
+ content = Content.new(type: 'text/plain', value: 'some text here')
10
+ mail = SendGrid::Mail.new(from, subject, to, content)
11
+ # puts JSON.pretty_generate(mail.to_json)
12
+ puts mail.to_json
13
+
14
+ sg = SendGrid::API.new(api_key: ENV['SENDGRID_API_KEY'])
15
+ response = sg.client.mail._('send').post(request_body: mail.to_json)
16
+ puts response.status_code
17
+ puts response.body
18
+ puts response.headers
19
+ end
20
+
21
+ # rubocop:disable Metrics/AbcSize
22
+ def kitchen_sink
23
+ mail = SendGrid::Mail.new
24
+ mail.from = Email.new(email: 'test@example.com')
25
+ mail.subject = 'Hello World from the Twilio SendGrid Ruby Library'
26
+ personalization = Personalization.new
27
+ personalization.add_to(Email.new(email: 'test1@example.com', name: 'Example User'))
28
+ personalization.add_to(Email.new(email: 'test2@example.com', name: 'Example User'))
29
+ personalization.add_cc(Email.new(email: 'test3@example.com', name: 'Example User'))
30
+ personalization.add_cc(Email.new(email: 'test4@example.com', name: 'Example User'))
31
+ personalization.add_bcc(Email.new(email: 'test5@example.com', name: 'Example User'))
32
+ personalization.add_bcc(Email.new(email: 'test6@example.com', name: 'Example User'))
33
+ # Note, the domain of the from email property specified in any personalization must
34
+ # match the domain of the from email property specified at root level
35
+ personalization.add_from(Email.new(email: 'alias@example.com', name: "My alias"))
36
+ personalization.subject = 'Hello World from the Personalized Twilio SendGrid Ruby Library'
37
+ personalization.add_header(Header.new(key: 'X-Test', value: 'True'))
38
+ personalization.add_header(Header.new(key: 'X-Mock', value: 'False'))
39
+ personalization.add_substitution(Substitution.new(key: '%name%', value: 'Example User'))
40
+ personalization.add_substitution(Substitution.new(key: '%city%', value: 'Denver'))
41
+ personalization.add_custom_arg(CustomArg.new(key: 'user_id', value: '343'))
42
+ personalization.add_custom_arg(CustomArg.new(key: 'type', value: 'marketing'))
43
+ personalization.send_at = 1_443_636_843
44
+ mail.add_personalization(personalization)
45
+
46
+ personalization2 = Personalization.new
47
+ personalization2.add_to(Email.new(email: 'test1@example.com', name: 'Example User'))
48
+ personalization2.add_to(Email.new(email: 'test2@example.com', name: 'Example User'))
49
+ personalization2.add_cc(Email.new(email: 'test3@example.com', name: 'Example User'))
50
+ personalization2.add_cc(Email.new(email: 'test4@example.com', name: 'Example User'))
51
+ personalization2.add_bcc(Email.new(email: 'test5@example.com', name: 'Example User'))
52
+ personalization2.add_bcc(Email.new(email: 'test6@example.com', name: 'Example User'))
53
+ personalization2.subject = 'Hello World from the Personalized Twilio SendGrid Ruby Library'
54
+ personalization2.add_header(Header.new(key: 'X-Test', value: 'True'))
55
+ personalization2.add_header(Header.new(key: 'X-Mock', value: 'False'))
56
+ personalization2.add_substitution(Substitution.new(key: '%name%', value: 'Example User'))
57
+ personalization2.add_substitution(Substitution.new(key: '%city%', value: 'Denver'))
58
+ personalization2.add_custom_arg(CustomArg.new(key: 'user_id', value: '343'))
59
+ personalization2.add_custom_arg(CustomArg.new(key: 'type', value: 'marketing'))
60
+ personalization2.send_at = 1_443_636_843
61
+ mail.add_personalization(personalization2)
62
+
63
+ mail.add_content(Content.new(type: 'text/plain', value: 'some text here'))
64
+ mail.add_content(Content.new(type: 'text/html', value: '<html><body>some text here</body></html>'))
65
+
66
+ attachment = Attachment.new
67
+ attachment.content = 'TG9yZW0gaXBzdW0gZG9sb3Igc2l0IGFtZXQsIGNvbnNlY3RldHVyIGFkaXBpc2NpbmcgZWxpdC4gQ3JhcyBwdW12'
68
+ attachment.type = 'application/pdf'
69
+ attachment.filename = 'balance_001.pdf'
70
+ attachment.disposition = 'attachment'
71
+ attachment.content_id = 'Balance Sheet'
72
+ mail.add_attachment(attachment)
73
+
74
+ attachment2 = Attachment.new
75
+ attachment2.content = 'BwdW'
76
+ attachment2.type = 'image/png'
77
+ attachment2.filename = 'banner.png'
78
+ attachment2.disposition = 'inline'
79
+ attachment2.content_id = 'Banner'
80
+ mail.add_attachment(attachment2)
81
+
82
+ mail.template_id = '13b8f94f-bcae-4ec6-b752-70d6cb59f932'
83
+
84
+ mail.add_section(Section.new(key: '%section1%', value: 'Substitution Text for Section 1'))
85
+ mail.add_section(Section.new(key: '%section2%', value: 'Substitution Text for Section 2'))
86
+
87
+ mail.add_header(Header.new(key: 'X-Test3', value: 'test3'))
88
+ mail.add_header(Header.new(key: 'X-Test4', value: 'test4'))
89
+
90
+ mail.add_category(Category.new(name: 'May'))
91
+ mail.add_category(Category.new(name: '2016'))
92
+
93
+ mail.add_custom_arg(CustomArg.new(key: 'campaign', value: 'welcome'))
94
+ mail.add_custom_arg(CustomArg.new(key: 'weekday', value: 'morning'))
95
+
96
+ mail.send_at = 1_443_636_842
97
+
98
+ # This must be a valid [batch ID](https://sendgrid.com/docs/API_Reference/SMTP_API/scheduling_parameters.html) to work
99
+ # mail.batch_id = 'sendgrid_batch_id'
100
+
101
+ mail.asm = ASM.new(group_id: 99, groups_to_display: [4, 5, 6, 7, 8])
102
+
103
+ mail.ip_pool_name = '23'
104
+
105
+ mail_settings = MailSettings.new
106
+ mail_settings.bcc = BccSettings.new(enable: true, email: 'test@example.com')
107
+ mail_settings.bypass_list_management = BypassListManagement.new(enable: true)
108
+ mail_settings.footer = Footer.new(enable: true, text: 'Footer Text', html: '<html><body>Footer Text</body></html>')
109
+ mail_settings.sandbox_mode = SandBoxMode.new(enable: true)
110
+ mail_settings.spam_check = SpamCheck.new(enable: true, threshold: 1, post_to_url: 'https://spamcatcher.sendgrid.com')
111
+ mail.mail_settings = mail_settings
112
+
113
+ tracking_settings = TrackingSettings.new
114
+ tracking_settings.click_tracking = ClickTracking.new(enable: false, enable_text: false)
115
+ tracking_settings.open_tracking = OpenTracking.new(enable: true, substitution_tag: 'Optional tag to replace with the open image in the body of the message')
116
+ 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
+ tracking_settings.ganalytics = Ganalytics.new(enable: true, utm_source: 'some source', utm_medium: 'some medium', utm_term: 'some term', utm_content: 'some content', utm_campaign: 'some campaign')
118
+ mail.tracking_settings = tracking_settings
119
+
120
+ mail.reply_to = Email.new(email: 'test@example.com')
121
+
122
+ # puts JSON.pretty_generate(mail.to_json)
123
+ puts mail.to_json
124
+
125
+ sg = SendGrid::API.new(api_key: ENV['SENDGRID_API_KEY'])
126
+ response = sg.client.mail._('send').post(request_body: mail.to_json)
127
+ puts response.status_code
128
+ puts response.body
129
+ puts response.headers
130
+ end
131
+ # rubocop:enable Metrics/AbcSize
132
+
133
+ def dynamic_template_data_hello_world
134
+ mail = Mail.new
135
+ mail.template_id = '' # a non-legacy template id
136
+ mail.from = Email.new(email: 'test@example.com')
137
+ personalization = Personalization.new
138
+ personalization.add_to(Email.new(email: 'test1@example.com', name: 'Example User'))
139
+ personalization.add_dynamic_template_data(
140
+ 'variable' => [
141
+ { 'foo' => 'bar' }, { 'foo' => 'baz' }
142
+ ]
143
+ )
144
+ mail.add_personalization(personalization)
145
+
146
+ # puts JSON.pretty_generate(mail.to_json)
147
+ puts mail.to_json
148
+
149
+ sg = SendGrid::API.new(api_key: ENV['SENDGRID_API_KEY'])
150
+ response = sg.client.mail._('send').post(request_body: mail.to_json)
151
+ puts response.status_code
152
+ puts response.body
153
+ puts response.headers
154
+ end
155
+
156
+ hello_world
157
+ kitchen_sink
158
+ dynamic_template_data_hello_world
@@ -0,0 +1,23 @@
1
+ require 'sendgrid-ruby'
2
+ include SendGrid
3
+
4
+ sg_client = SendGrid::API.new(api_key: ENV['SENDGRID_API_KEY']).client
5
+ settings = SendGrid::Settings.new(sendgrid_client: sg_client)
6
+
7
+ # Fetch settings
8
+ response = settings.bcc
9
+ puts response.status_code
10
+ puts response.body
11
+ puts response.headers
12
+
13
+ # Turn on bcc settings
14
+ response = settings.update_bcc(enabled: true, email: 'email@example.com')
15
+ puts response.status_code
16
+ puts response.body
17
+ puts response.headers
18
+
19
+ # Turn off bcc settings
20
+ response = settings.update_bcc(enabled: false)
21
+ puts response.status_code
22
+ puts response.body
23
+ puts response.headers