sendgrid-ruby 1.1.6 → 6.0.4
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +5 -5
- data/.codeclimate.yml +21 -0
- data/.env_sample +1 -3
- data/.gitignore +1 -0
- data/.travis.yml +28 -17
- data/CHANGELOG.md +231 -1
- data/CODE_OF_CONDUCT.md +73 -0
- data/CONTRIBUTING.md +214 -0
- data/Gemfile +5 -4
- data/ISSUE_TEMPLATE.md +26 -0
- data/LICENSE.md +21 -0
- data/Makefile +7 -0
- data/PULL_REQUEST_TEMPLATE.md +31 -0
- data/README.md +161 -225
- data/Rakefile +9 -2
- data/TROUBLESHOOTING.md +137 -0
- data/UPGRADE.md +5 -0
- data/USAGE.md +5037 -0
- data/USE_CASES.md +377 -0
- data/config.ru +4 -0
- data/docker/Dockerfile +12 -0
- data/docker/README.md +30 -0
- data/examples/accesssettings/accesssettings.rb +83 -0
- data/examples/alerts/alerts.rb +62 -0
- data/examples/apikeys/apikeys.rb +84 -0
- data/examples/asm/asm.rb +173 -0
- data/examples/browsers/browsers.rb +16 -0
- data/examples/campaigns/campaigns.rb +153 -0
- data/examples/categories/categories.rb +36 -0
- data/examples/clients/clients.rb +27 -0
- data/examples/contactdb/contactdb.rb +395 -0
- data/examples/devices/devices.rb +16 -0
- data/examples/geo/geo.rb +16 -0
- data/examples/helpers/mail/example.rb +147 -0
- data/examples/helpers/settings/example.rb +23 -0
- data/examples/helpers/stats/example.rb +42 -0
- data/examples/ips/ips.rb +167 -0
- data/examples/mail/mail.rb +173 -0
- data/examples/mailboxproviders/mailboxproviders.rb +16 -0
- data/examples/mailsettings/mailsettings.rb +219 -0
- data/examples/partnersettings/partnersettings.rb +39 -0
- data/examples/scopes/scopes.rb +61 -0
- data/examples/senderauthentication/senderauthentication.rb +310 -0
- data/examples/senders/senders.rb +98 -0
- data/examples/stats/stats.rb +16 -0
- data/examples/subusers/subusers.rb +169 -0
- data/examples/suppression/suppression.rb +201 -0
- data/examples/templates/templates.rb +129 -0
- data/examples/trackingsettings/trackingsettings.rb +110 -0
- data/examples/user/user.rb +293 -0
- data/gemfiles/Sinatra_1.gemfile +6 -0
- data/gemfiles/Sinatra_2.gemfile +6 -0
- data/lib/sendgrid/client.rb +33 -57
- data/lib/sendgrid/helpers/inbound/README.md +98 -0
- data/lib/sendgrid/helpers/inbound/app.rb +32 -0
- data/lib/sendgrid/helpers/inbound/config.yml +26 -0
- data/lib/sendgrid/helpers/inbound/public/index.html +10 -0
- data/lib/sendgrid/helpers/inbound/sample_data/default_data.txt +58 -0
- data/lib/sendgrid/helpers/inbound/sample_data/raw_data.txt +57 -0
- data/lib/sendgrid/helpers/inbound/sample_data/raw_data_with_attachments.txt +298 -0
- data/lib/sendgrid/helpers/inbound/send.rb +26 -0
- data/lib/sendgrid/helpers/ip_management/ip_management.rb +17 -0
- data/lib/sendgrid/helpers/mail/README.md +14 -0
- data/lib/sendgrid/helpers/mail/asm.rb +33 -0
- data/lib/sendgrid/helpers/mail/attachment.rb +86 -0
- data/lib/sendgrid/helpers/mail/bcc_settings.rb +33 -0
- data/lib/sendgrid/helpers/mail/bypass_list_management.rb +43 -0
- data/lib/sendgrid/helpers/mail/category.rb +20 -0
- data/lib/sendgrid/helpers/mail/click_tracking.rb +33 -0
- data/lib/sendgrid/helpers/mail/content.rb +20 -0
- data/lib/sendgrid/helpers/mail/custom_arg.rb +24 -0
- data/lib/sendgrid/helpers/mail/email.rb +29 -0
- data/lib/sendgrid/helpers/mail/footer.rb +43 -0
- data/lib/sendgrid/helpers/mail/ganalytics.rb +74 -0
- data/lib/sendgrid/helpers/mail/header.rb +24 -0
- data/lib/sendgrid/helpers/mail/mail.rb +140 -0
- data/lib/sendgrid/helpers/mail/mail_settings.rb +63 -0
- data/lib/sendgrid/helpers/mail/open_tracking.rb +33 -0
- data/lib/sendgrid/helpers/mail/personalization.rb +82 -0
- data/lib/sendgrid/helpers/mail/section.rb +24 -0
- data/lib/sendgrid/helpers/mail/spam_check.rb +43 -0
- data/lib/sendgrid/helpers/mail/subscription_tracking.rb +53 -0
- data/lib/sendgrid/helpers/mail/substitution.rb +24 -0
- data/lib/sendgrid/helpers/mail/tracking_settings.rb +53 -0
- data/lib/sendgrid/helpers/permissions/scope.rb +28 -0
- data/lib/sendgrid/helpers/permissions/scopes.yml +309 -0
- data/lib/sendgrid/helpers/settings/README.md +14 -0
- data/lib/sendgrid/helpers/settings/mail_settings_dto.rb +13 -0
- data/lib/sendgrid/helpers/settings/partner_settings_dto.rb +13 -0
- data/lib/sendgrid/helpers/settings/settings.rb +28 -0
- data/lib/sendgrid/helpers/settings/tracking_settings_dto.rb +24 -0
- data/lib/sendgrid/helpers/settings/user_settings_dto.rb +13 -0
- data/lib/sendgrid/helpers/stats/email_stats.rb +46 -0
- data/lib/sendgrid/helpers/stats/metrics.rb +35 -0
- data/lib/sendgrid/helpers/stats/stats_response.rb +31 -0
- data/lib/sendgrid/version.rb +1 -1
- data/lib/sendgrid-ruby.rb +27 -6
- data/mail_helper_v3.md +390 -0
- data/sendgrid-ruby.gemspec +13 -17
- data/spec/sendgrid/helpers/ip_management/ip_management_spec.rb +12 -0
- data/spec/sendgrid/helpers/settings/mail_settings_dto_spec.rb +32 -0
- data/spec/sendgrid/helpers/settings/partner_settings_dto_spec.rb +24 -0
- data/spec/sendgrid/helpers/settings/settings_spec.rb +25 -0
- data/spec/sendgrid/helpers/settings/tracking_settings_dto_spec.rb +27 -0
- data/spec/sendgrid/helpers/settings/user_settings_dto_spec.rb +24 -0
- data/spec/sendgrid/helpers/stats/email_stats_spec.rb +112 -0
- data/spec/sendgrid/helpers/stats/metrics_spec.rb +46 -0
- data/spec/sendgrid/helpers/stats/stats_response_spec.rb +76 -0
- data/spec/spec_helper.rb +10 -1
- data/test/prism.sh +42 -0
- data/test/sendgrid/helpers/mail/test_attachment.rb +35 -0
- data/test/sendgrid/helpers/mail/test_category.rb +27 -0
- data/test/sendgrid/helpers/mail/test_email.rb +34 -0
- data/test/sendgrid/helpers/mail/test_mail.rb +261 -0
- data/test/sendgrid/helpers/mail/test_personalizations.rb +161 -0
- data/test/sendgrid/permissions/test_scopes.rb +38 -0
- data/test/sendgrid/test_sendgrid-ruby.rb +2751 -0
- metadata +149 -115
- data/.rspec +0 -2
- data/.rubocop.yml +0 -30
- data/FETCH_HEAD +0 -0
- data/Guardfile +0 -10
- data/LICENSE.txt +0 -22
- data/example.rb +0 -41
- data/lib/sendgrid/exceptions.rb +0 -7
- data/lib/sendgrid/mail.rb +0 -182
- data/lib/sendgrid/recipient.rb +0 -29
- data/lib/sendgrid/response.rb +0 -14
- data/lib/sendgrid/template.rb +0 -26
- data/lib/sendgrid/template_mailer.rb +0 -59
- data/spec/lib/sendgrid/client_spec.rb +0 -87
- data/spec/lib/sendgrid/mail_spec.rb +0 -151
- data/spec/lib/sendgrid/recipient_spec.rb +0 -91
- data/spec/lib/sendgrid/template_mailer_spec.rb +0 -86
- data/spec/lib/sendgrid/template_spec.rb +0 -61
- data/spec/lib/sendgrid_spec.rb +0 -7
data/USE_CASES.md
ADDED
@@ -0,0 +1,377 @@
|
|
1
|
+
This documentation provides examples for specific use cases. Please [open an issue](https://github.com/sendgrid/sendgrid-ruby/issues) or make a pull request for any use cases you would like us to document here. Thank you!
|
2
|
+
|
3
|
+
# Table of Contents
|
4
|
+
|
5
|
+
* [Transactional Templates](#transactional-templates)
|
6
|
+
* [With Mail Helper Class](#with-mail-helper-class)
|
7
|
+
* [Without Mail Helper Class](#without-mail-helper-class)
|
8
|
+
* [Legacy Templates](#legacy-templates)
|
9
|
+
* [With Mail Helper Class](#with-mail-helper-class-1)
|
10
|
+
* [Without Mail Helper Class](#without-mail-helper-class-1)
|
11
|
+
* [Adding Attachments](#adding-attachments)
|
12
|
+
* [How to Setup a Domain Authentication](#how-to-setup-a-domain-authentication)
|
13
|
+
* [How to View Email Statistics](#how-to-view-email-statistics)
|
14
|
+
* [Send a SMS Message](#send-a-sms-message)
|
15
|
+
* [1. Obtain a Free Twilio Account](#1-obtain-a-free-twilio-account)
|
16
|
+
* [2. Update Your Environment Variables](#2-update-your-environment-variables)
|
17
|
+
* [Mac](#mac)
|
18
|
+
* [Windows](#windows)
|
19
|
+
* [3. Install the Twilio Helper Library](#3-install-the-twilio-helper-library)
|
20
|
+
* [4. Setup Work](#4-setup-work)
|
21
|
+
* [5. Send an SMS](#5-send-an-sms)
|
22
|
+
|
23
|
+
<a name="transactional-templates"></a>
|
24
|
+
# Transactional Templates
|
25
|
+
|
26
|
+
For this example, we assume you have created a [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.
|
27
|
+
|
28
|
+
Template ID (replace with your own):
|
29
|
+
|
30
|
+
```text
|
31
|
+
d-2c214ac919e84170b21855cc129b4a5f
|
32
|
+
```
|
33
|
+
Email Subject:
|
34
|
+
```text
|
35
|
+
{{subject}}
|
36
|
+
```
|
37
|
+
|
38
|
+
Template Body:
|
39
|
+
|
40
|
+
```html
|
41
|
+
<html>
|
42
|
+
<head>
|
43
|
+
<title></title>
|
44
|
+
</head>
|
45
|
+
<body>
|
46
|
+
Hello {{name}},
|
47
|
+
<br/><br/>
|
48
|
+
I'm glad you are trying out the template feature!
|
49
|
+
<br/><br/>
|
50
|
+
I hope you are having a great day in {{city}} :)
|
51
|
+
<br/><br/>
|
52
|
+
</body>
|
53
|
+
</html>
|
54
|
+
```
|
55
|
+
|
56
|
+
## With Mail Helper Class
|
57
|
+
```ruby
|
58
|
+
require 'sendgrid-ruby'
|
59
|
+
include SendGrid
|
60
|
+
|
61
|
+
mail = Mail.new
|
62
|
+
mail.from = Email.new(email: 'test@example.com')
|
63
|
+
personalization = Personalization.new
|
64
|
+
personalization.add_to(Email.new(email: 'test@example.com'))
|
65
|
+
personalization.add_dynamic_template_data({
|
66
|
+
"subject" => "Testing Templates",
|
67
|
+
"name" => "Example User",
|
68
|
+
"city" => "Denver"
|
69
|
+
})
|
70
|
+
mail.add_personalization(personalization)
|
71
|
+
mail.template_id = 'd-2c214ac919e84170b21855cc129b4a5f'
|
72
|
+
|
73
|
+
sg = SendGrid::API.new(api_key: ENV['SENDGRID_API_KEY'])
|
74
|
+
begin
|
75
|
+
response = sg.client.mail._("send").post(request_body: mail.to_json)
|
76
|
+
rescue Exception => e
|
77
|
+
puts e.message
|
78
|
+
end
|
79
|
+
puts response.status_code
|
80
|
+
puts response.body
|
81
|
+
puts response.parsed_body
|
82
|
+
puts response.headers
|
83
|
+
```
|
84
|
+
|
85
|
+
## Without Mail Helper Class
|
86
|
+
|
87
|
+
```ruby
|
88
|
+
require 'sendgrid-ruby'
|
89
|
+
include SendGrid
|
90
|
+
|
91
|
+
data = JSON.parse('{
|
92
|
+
"personalizations": [
|
93
|
+
{
|
94
|
+
"to": [
|
95
|
+
{
|
96
|
+
"email": "test@example.com"
|
97
|
+
}
|
98
|
+
],
|
99
|
+
"dynamic_template_data": {
|
100
|
+
"subject": "Testing Templates",
|
101
|
+
"name": "Example User",
|
102
|
+
"city": "Denver"
|
103
|
+
}
|
104
|
+
}
|
105
|
+
],
|
106
|
+
"from": {
|
107
|
+
"email": "test@example.com"
|
108
|
+
},
|
109
|
+
"template_id": "d-2c214ac919e84170b21855cc129b4a5f"
|
110
|
+
}')
|
111
|
+
sg = SendGrid::API.new(api_key: ENV['SENDGRID_API_KEY'])
|
112
|
+
begin
|
113
|
+
response = sg.client.mail._("send").post(request_body: data)
|
114
|
+
rescue Exception => e
|
115
|
+
puts e.message
|
116
|
+
end
|
117
|
+
puts response.status_code
|
118
|
+
puts response.body
|
119
|
+
puts response.parsed_body
|
120
|
+
puts response.headers
|
121
|
+
```
|
122
|
+
|
123
|
+
<a name="legacy-templates"></a>
|
124
|
+
# Legacy Templates
|
125
|
+
|
126
|
+
For this example, we assume you have created a [legacy template](https://sendgrid.com/docs/User_Guide/Transactional_Templates/index.html). Following is the template content we used for testing.
|
127
|
+
|
128
|
+
Template ID (replace with your own):
|
129
|
+
|
130
|
+
```text
|
131
|
+
13b8f94f-bcae-4ec6-b752-70d6cb59f932
|
132
|
+
```
|
133
|
+
|
134
|
+
Email Subject:
|
135
|
+
|
136
|
+
```text
|
137
|
+
<%subject%>
|
138
|
+
```
|
139
|
+
|
140
|
+
Template Body:
|
141
|
+
|
142
|
+
```html
|
143
|
+
<html>
|
144
|
+
<head>
|
145
|
+
<title></title>
|
146
|
+
</head>
|
147
|
+
<body>
|
148
|
+
Hello -name-,
|
149
|
+
<br /><br/>
|
150
|
+
I'm glad you are trying out the template feature!
|
151
|
+
<br /><br/>
|
152
|
+
<%body%>
|
153
|
+
<br /><br/>
|
154
|
+
I hope you are having a great day in -city- :)
|
155
|
+
<br /><br/>
|
156
|
+
</body>
|
157
|
+
</html>
|
158
|
+
```
|
159
|
+
|
160
|
+
## With Mail Helper Class
|
161
|
+
|
162
|
+
```ruby
|
163
|
+
require 'sendgrid-ruby'
|
164
|
+
include SendGrid
|
165
|
+
|
166
|
+
mail = SendGrid::Mail.new
|
167
|
+
mail.from = Email.new(email: 'test@example.com')
|
168
|
+
mail.subject = 'I\'m replacing the subject tag'
|
169
|
+
personalization = Personalization.new
|
170
|
+
personalization.add_to(Email.new(email: 'test@example.com'))
|
171
|
+
personalization.add_substitution(Substitution.new(key: '-name-', value: 'Example User'))
|
172
|
+
personalization.add_substitution(Substitution.new(key: '-city-', value: 'Denver'))
|
173
|
+
mail.add_personalization(personalization)
|
174
|
+
mail.template_id = '13b8f94f-bcae-4ec6-b752-70d6cb59f932'
|
175
|
+
|
176
|
+
sg = SendGrid::API.new(api_key: ENV['SENDGRID_API_KEY'])
|
177
|
+
begin
|
178
|
+
response = sg.client.mail._("send").post(request_body: mail.to_json)
|
179
|
+
rescue Exception => e
|
180
|
+
puts e.message
|
181
|
+
end
|
182
|
+
puts response.status_code
|
183
|
+
puts response.body
|
184
|
+
puts response.parsed_body
|
185
|
+
puts response.headers
|
186
|
+
```
|
187
|
+
|
188
|
+
## Without Mail Helper Class
|
189
|
+
|
190
|
+
```ruby
|
191
|
+
require 'sendgrid-ruby'
|
192
|
+
include SendGrid
|
193
|
+
|
194
|
+
data = JSON.parse('{
|
195
|
+
"personalizations": [
|
196
|
+
{
|
197
|
+
"to": [
|
198
|
+
{
|
199
|
+
"email": "test@example.com"
|
200
|
+
}
|
201
|
+
],
|
202
|
+
"substitutions": {
|
203
|
+
"-name-": "Example User",
|
204
|
+
"-city-": "Denver"
|
205
|
+
},
|
206
|
+
"subject": "I\'m replacing the subject tag"
|
207
|
+
}
|
208
|
+
],
|
209
|
+
"from": {
|
210
|
+
"email": "test@example.com"
|
211
|
+
},
|
212
|
+
"template_id": "13b8f94f-bcae-4ec6-b752-70d6cb59f932"
|
213
|
+
}')
|
214
|
+
sg = SendGrid::API.new(api_key: ENV['SENDGRID_API_KEY'])
|
215
|
+
begin
|
216
|
+
response = sg.client.mail._("send").post(request_body: data)
|
217
|
+
rescue Exception => e
|
218
|
+
puts e.message
|
219
|
+
end
|
220
|
+
puts response.status_code
|
221
|
+
puts response.body
|
222
|
+
puts response.parsed_body
|
223
|
+
puts response.headers
|
224
|
+
```
|
225
|
+
|
226
|
+
## Adding Attachments
|
227
|
+
|
228
|
+
```ruby
|
229
|
+
attachment = Attachment.new
|
230
|
+
attachment.content = Base64.strict_encode64(File.open(fpath, 'rb').read)
|
231
|
+
attachment.type = 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet'
|
232
|
+
attachment.filename = fname
|
233
|
+
attachment.disposition = 'attachment'
|
234
|
+
attachment.content_id = 'Reports Sheet'
|
235
|
+
mail.add_attachment(attachment)
|
236
|
+
|
237
|
+
```
|
238
|
+
Attachments must be base64 encoded, using Base64's strict_encode64 where no line feeds are added.
|
239
|
+
|
240
|
+
<a name="domain-authentication"></a>
|
241
|
+
# How to Setup a Domain Authentication
|
242
|
+
|
243
|
+
You can find documentation for how to setup a domain authentication via the UI [here](https://sendgrid.com/docs/ui/account-and-settings/how-to-set-up-domain-authentication/) and via API [here](https://github.com/sendgrid/sendgrid-nodejs/blob/master/packages/client/USAGE.md#sender-authentication).
|
244
|
+
|
245
|
+
Find more information about all of SendGrid's authentication related documentation [here](https://sendgrid.com/docs/ui/account-and-settings/).
|
246
|
+
|
247
|
+
<a name="email-statistics"></a>
|
248
|
+
# How to View Email Statistics
|
249
|
+
|
250
|
+
You can find documentation for how to view your email statistics via the UI [here](https://app.sendgrid.com/statistics) and via API [here](https://github.com/sendgrid/sendgrid-ruby/blob/master/USAGE.md#stats).
|
251
|
+
|
252
|
+
Alternatively, we can post events to a URL of your choice via our [Event Webhook](https://sendgrid.com/docs/API_Reference/Webhooks/event.html) about events that occur as Twilio SendGrid processes your email.
|
253
|
+
|
254
|
+
You can also use the email statistics helper to make it easier to interact with the API.
|
255
|
+
|
256
|
+
```ruby
|
257
|
+
require 'sendgrid-ruby'
|
258
|
+
require 'date'
|
259
|
+
|
260
|
+
include SendGrid
|
261
|
+
|
262
|
+
sg_client = SendGrid::API.new(api_key: ENV['SENDGRID_API_KEY']).client
|
263
|
+
stats = SendGrid::EmailStats.new(sendgrid_client: sg_client)
|
264
|
+
|
265
|
+
# Fetch stats by day, between 2 dates
|
266
|
+
from = Date.new(2017, 10, 01)
|
267
|
+
to = Date.new(2017, 10, 12)
|
268
|
+
|
269
|
+
email_stats = stats.by_day(from, to)
|
270
|
+
|
271
|
+
email_stats.metrics
|
272
|
+
|
273
|
+
if !email_stats.error?
|
274
|
+
email_stats.metrics.each do |metric|
|
275
|
+
puts "Date - #{metric.date}"
|
276
|
+
puts "Number of Requests - #{metric.requests}"
|
277
|
+
puts "Bounces - #{metric.bounces}"
|
278
|
+
puts "Opens - #{metric.opens}"
|
279
|
+
puts "Clicks - #{metric.clicks}"
|
280
|
+
end
|
281
|
+
end
|
282
|
+
|
283
|
+
# Fetch stats by week, between 2 dates for a category
|
284
|
+
from = Date.new(2017, 10, 01)
|
285
|
+
to = Date.new(2017, 10, 12)
|
286
|
+
category = 'abcd'
|
287
|
+
|
288
|
+
email_stats = stats.by_week(from, to, category)
|
289
|
+
|
290
|
+
if !email_stats.error?
|
291
|
+
email_stats.metrics.each do |metric|
|
292
|
+
puts "Date - #{metric.date}"
|
293
|
+
puts "Number of Requests - #{metric.requests}"
|
294
|
+
puts "Bounces - #{metric.bounces}"
|
295
|
+
puts "Opens - #{metric.opens}"
|
296
|
+
puts "Clicks - #{metric.clicks}"
|
297
|
+
end
|
298
|
+
end
|
299
|
+
|
300
|
+
```
|
301
|
+
|
302
|
+
<a name="sms"></a>
|
303
|
+
# Send a SMS Message
|
304
|
+
|
305
|
+
Following are the steps to add Twilio SMS to your app:
|
306
|
+
|
307
|
+
## 1. Obtain a Free Twilio Account
|
308
|
+
|
309
|
+
Sign up for a free Twilio account [here](https://www.twilio.com/try-twilio?source=sendgrid-ruby).
|
310
|
+
|
311
|
+
## 2. Update Your Environment Variables
|
312
|
+
|
313
|
+
You can obtain your Account Sid and Auth Token from [twilio.com/console](https://twilio.com/console).
|
314
|
+
|
315
|
+
### Mac
|
316
|
+
|
317
|
+
```bash
|
318
|
+
echo "export TWILIO_ACCOUNT_SID='YOUR_TWILIO_ACCOUNT_SID'" > twilio.env
|
319
|
+
echo "export TWILIO_AUTH_TOKEN='YOUR_TWILIO_AUTH_TOKEN'" >> twilio.env
|
320
|
+
echo "twilio.env" >> .gitignore
|
321
|
+
source ./twilio.env
|
322
|
+
```
|
323
|
+
|
324
|
+
### Windows
|
325
|
+
|
326
|
+
Temporarily set the environment variable (accessible only during the current CLI session):
|
327
|
+
|
328
|
+
```bash
|
329
|
+
set TWILIO_ACCOUNT_SID=YOUR_TWILIO_ACCOUNT_SID
|
330
|
+
set TWILIO_AUTH_TOKEN=YOUR_TWILIO_AUTH_TOKEN
|
331
|
+
```
|
332
|
+
|
333
|
+
Permanently set the environment variable (accessible in all subsequent CLI sessions):
|
334
|
+
|
335
|
+
```bash
|
336
|
+
setx TWILIO_ACCOUNT_SID "YOUR_TWILIO_ACCOUNT_SID"
|
337
|
+
setx TWILIO_AUTH_TOKEN "YOUR_TWILIO_AUTH_TOKEN"
|
338
|
+
```
|
339
|
+
|
340
|
+
## 3. Install the Twilio Helper Library
|
341
|
+
|
342
|
+
To install using [Bundler][bundler] grab the latest stable version:
|
343
|
+
|
344
|
+
```ruby
|
345
|
+
gem 'twilio-ruby', '~> 5.23.1'
|
346
|
+
```
|
347
|
+
|
348
|
+
To manually install `twilio-ruby` via [Rubygems][rubygems] simply gem install:
|
349
|
+
|
350
|
+
```bash
|
351
|
+
gem install twilio-ruby -v 5.23.1
|
352
|
+
```
|
353
|
+
|
354
|
+
## 4. Setup Work
|
355
|
+
|
356
|
+
```ruby
|
357
|
+
require 'twilio-ruby'
|
358
|
+
|
359
|
+
# put your own credentials here
|
360
|
+
account_sid = ENV['TWILIO_ACCOUNT_SID']
|
361
|
+
auth_token = ENV['TWILIO_AUTH_TOKEN']
|
362
|
+
|
363
|
+
# set up a client to talk to the Twilio REST API
|
364
|
+
@client = Twilio::REST::Client.new account_sid, auth_token
|
365
|
+
```
|
366
|
+
|
367
|
+
## 5. Send an SMS
|
368
|
+
|
369
|
+
```ruby
|
370
|
+
@client.api.account.messages.create(
|
371
|
+
from: '+14159341234',
|
372
|
+
to: '+16105557069',
|
373
|
+
body: 'Hey there!'
|
374
|
+
)
|
375
|
+
```
|
376
|
+
|
377
|
+
For more information, please visit the [Twilio SMS Ruby documentation](https://www.twilio.com/docs/sms/quickstart/ruby).
|
data/config.ru
ADDED
data/docker/Dockerfile
ADDED
@@ -0,0 +1,12 @@
|
|
1
|
+
FROM ruby:2.4.2-jessie
|
2
|
+
|
3
|
+
# Clone sendgrid-ruby
|
4
|
+
WORKDIR /sources
|
5
|
+
RUN git clone https://github.com/sendgrid/sendgrid-ruby.git
|
6
|
+
|
7
|
+
# Bundle
|
8
|
+
WORKDIR /sources/sendgrid-ruby
|
9
|
+
RUN bundle install
|
10
|
+
|
11
|
+
# Install prism
|
12
|
+
RUN curl https://raw.githubusercontent.com/stoplightio/prism/master/install.sh | sh
|
data/docker/README.md
ADDED
@@ -0,0 +1,30 @@
|
|
1
|
+
# Docker image for sendgrid-ruby
|
2
|
+
|
3
|
+
## Quickstart
|
4
|
+
1. [Install Docker](https://docs.docker.com/engine/installation/) on your machine.
|
5
|
+
2. Run `docker run --rm -it sendgrid/sendgrid-ruby irb`.
|
6
|
+
3. Run `require './lib/sendgrid-ruby.rb'`.
|
7
|
+
|
8
|
+
## Poke around
|
9
|
+
|
10
|
+
If you would like to just poke around in the image and check some examples:
|
11
|
+
```sh
|
12
|
+
docker run --rm -it sendgrid/sendgrid-ruby bash
|
13
|
+
```
|
14
|
+
|
15
|
+
If you want to mount your fork or specific version of the gem:
|
16
|
+
```sh
|
17
|
+
docker run --rm -v /path/to/local/sendgrid-ruby:/sources/sendgrid-ruby -it sendgrid/sendgrid-ruby bash
|
18
|
+
```
|
19
|
+
|
20
|
+
## Running tests
|
21
|
+
|
22
|
+
If you would like to run the tests present in the image:
|
23
|
+
```sh
|
24
|
+
docker run --rm sendgrid/sendgrid-ruby rake
|
25
|
+
```
|
26
|
+
|
27
|
+
If you want to run tests on your fork or a specific version, mount the codebase onto the image:
|
28
|
+
```sh
|
29
|
+
docker run --rm -v /path/to/local/sendgrid-ruby:/sources/sendgrid-ruby sendgrid/sendgrid-ruby rake
|
30
|
+
```
|
@@ -0,0 +1,83 @@
|
|
1
|
+
require 'sendgrid-ruby'
|
2
|
+
|
3
|
+
|
4
|
+
sg = SendGrid::API.new(api_key: ENV['SENDGRID_API_KEY'])
|
5
|
+
|
6
|
+
|
7
|
+
##################################################
|
8
|
+
# Retrieve all recent access attempts #
|
9
|
+
# GET /access_settings/activity #
|
10
|
+
|
11
|
+
params = JSON.parse('{"limit": 1}')
|
12
|
+
response = sg.client.access_settings.activity.get(query_params: params)
|
13
|
+
puts response.status_code
|
14
|
+
puts response.body
|
15
|
+
puts response.headers
|
16
|
+
|
17
|
+
##################################################
|
18
|
+
# Add one or more IPs to the whitelist #
|
19
|
+
# POST /access_settings/whitelist #
|
20
|
+
|
21
|
+
data = JSON.parse('{
|
22
|
+
"ips": [
|
23
|
+
{
|
24
|
+
"ip": "192.168.1.1"
|
25
|
+
},
|
26
|
+
{
|
27
|
+
"ip": "192.*.*.*"
|
28
|
+
},
|
29
|
+
{
|
30
|
+
"ip": "192.168.1.3/32"
|
31
|
+
}
|
32
|
+
]
|
33
|
+
}')
|
34
|
+
response = sg.client.access_settings.whitelist.post(request_body: data)
|
35
|
+
puts response.status_code
|
36
|
+
puts response.body
|
37
|
+
puts response.headers
|
38
|
+
|
39
|
+
##################################################
|
40
|
+
# Retrieve a list of currently whitelisted IPs #
|
41
|
+
# GET /access_settings/whitelist #
|
42
|
+
|
43
|
+
response = sg.client.access_settings.whitelist.get()
|
44
|
+
puts response.status_code
|
45
|
+
puts response.body
|
46
|
+
puts response.headers
|
47
|
+
|
48
|
+
##################################################
|
49
|
+
# Remove one or more IPs from the whitelist #
|
50
|
+
# DELETE /access_settings/whitelist #
|
51
|
+
|
52
|
+
data = JSON.parse('{
|
53
|
+
"ids": [
|
54
|
+
1,
|
55
|
+
2,
|
56
|
+
3
|
57
|
+
]
|
58
|
+
}')
|
59
|
+
response = sg.client.access_settings.whitelist.delete(request_body: data)
|
60
|
+
puts response.status_code
|
61
|
+
puts response.body
|
62
|
+
puts response.headers
|
63
|
+
|
64
|
+
##################################################
|
65
|
+
# Retrieve a specific whitelisted IP #
|
66
|
+
# GET /access_settings/whitelist/{rule_id} #
|
67
|
+
|
68
|
+
rule_id = "test_url_param"
|
69
|
+
response = sg.client.access_settings.whitelist._(rule_id).get()
|
70
|
+
puts response.status_code
|
71
|
+
puts response.body
|
72
|
+
puts response.headers
|
73
|
+
|
74
|
+
##################################################
|
75
|
+
# Remove a specific IP from the whitelist #
|
76
|
+
# DELETE /access_settings/whitelist/{rule_id} #
|
77
|
+
|
78
|
+
rule_id = "test_url_param"
|
79
|
+
response = sg.client.access_settings.whitelist._(rule_id).delete()
|
80
|
+
puts response.status_code
|
81
|
+
puts response.body
|
82
|
+
puts response.headers
|
83
|
+
|
@@ -0,0 +1,62 @@
|
|
1
|
+
require 'sendgrid-ruby'
|
2
|
+
|
3
|
+
|
4
|
+
sg = SendGrid::API.new(api_key: ENV['SENDGRID_API_KEY'])
|
5
|
+
|
6
|
+
|
7
|
+
##################################################
|
8
|
+
# Create a new Alert #
|
9
|
+
# POST /alerts #
|
10
|
+
|
11
|
+
data = JSON.parse('{
|
12
|
+
"email_to": "example@example.com",
|
13
|
+
"frequency": "daily",
|
14
|
+
"type": "stats_notification"
|
15
|
+
}')
|
16
|
+
response = sg.client.alerts.post(request_body: data)
|
17
|
+
puts response.status_code
|
18
|
+
puts response.body
|
19
|
+
puts response.headers
|
20
|
+
|
21
|
+
##################################################
|
22
|
+
# Retrieve all alerts #
|
23
|
+
# GET /alerts #
|
24
|
+
|
25
|
+
response = sg.client.alerts.get()
|
26
|
+
puts response.status_code
|
27
|
+
puts response.body
|
28
|
+
puts response.headers
|
29
|
+
|
30
|
+
##################################################
|
31
|
+
# Update an alert #
|
32
|
+
# PATCH /alerts/{alert_id} #
|
33
|
+
|
34
|
+
data = JSON.parse('{
|
35
|
+
"email_to": "example@example.com"
|
36
|
+
}')
|
37
|
+
alert_id = "test_url_param"
|
38
|
+
response = sg.client.alerts._(alert_id).patch(request_body: data)
|
39
|
+
puts response.status_code
|
40
|
+
puts response.body
|
41
|
+
puts response.headers
|
42
|
+
|
43
|
+
##################################################
|
44
|
+
# Retrieve a specific alert #
|
45
|
+
# GET /alerts/{alert_id} #
|
46
|
+
|
47
|
+
alert_id = "test_url_param"
|
48
|
+
response = sg.client.alerts._(alert_id).get()
|
49
|
+
puts response.status_code
|
50
|
+
puts response.body
|
51
|
+
puts response.headers
|
52
|
+
|
53
|
+
##################################################
|
54
|
+
# Delete an alert #
|
55
|
+
# DELETE /alerts/{alert_id} #
|
56
|
+
|
57
|
+
alert_id = "test_url_param"
|
58
|
+
response = sg.client.alerts._(alert_id).delete()
|
59
|
+
puts response.status_code
|
60
|
+
puts response.body
|
61
|
+
puts response.headers
|
62
|
+
|
@@ -0,0 +1,84 @@
|
|
1
|
+
require 'sendgrid-ruby'
|
2
|
+
|
3
|
+
|
4
|
+
sg = SendGrid::API.new(api_key: ENV['SENDGRID_API_KEY'])
|
5
|
+
|
6
|
+
|
7
|
+
##################################################
|
8
|
+
# Create API keys #
|
9
|
+
# POST /api_keys #
|
10
|
+
|
11
|
+
data = JSON.parse('{
|
12
|
+
"name": "My API Key",
|
13
|
+
"sample": "data",
|
14
|
+
"scopes": [
|
15
|
+
"mail.send",
|
16
|
+
"alerts.create",
|
17
|
+
"alerts.read"
|
18
|
+
]
|
19
|
+
}')
|
20
|
+
response = sg.client.api_keys.post(request_body: data)
|
21
|
+
puts response.status_code
|
22
|
+
puts response.body
|
23
|
+
puts response.headers
|
24
|
+
|
25
|
+
##################################################
|
26
|
+
# Retrieve all API Keys belonging to the authenticated user #
|
27
|
+
# GET /api_keys #
|
28
|
+
|
29
|
+
params = JSON.parse('{"limit": 1}')
|
30
|
+
response = sg.client.api_keys.get(query_params: params)
|
31
|
+
puts response.status_code
|
32
|
+
puts response.body
|
33
|
+
puts response.headers
|
34
|
+
|
35
|
+
##################################################
|
36
|
+
# Update the name & scopes of an API Key #
|
37
|
+
# PUT /api_keys/{api_key_id} #
|
38
|
+
|
39
|
+
data = JSON.parse('{
|
40
|
+
"name": "A New Hope",
|
41
|
+
"scopes": [
|
42
|
+
"user.profile.read",
|
43
|
+
"user.profile.update"
|
44
|
+
]
|
45
|
+
}')
|
46
|
+
api_key_id = "test_url_param"
|
47
|
+
response = sg.client.api_keys._(api_key_id).put(request_body: data)
|
48
|
+
puts response.status_code
|
49
|
+
puts response.body
|
50
|
+
puts response.headers
|
51
|
+
|
52
|
+
##################################################
|
53
|
+
# Update API keys #
|
54
|
+
# PATCH /api_keys/{api_key_id} #
|
55
|
+
|
56
|
+
data = JSON.parse('{
|
57
|
+
"name": "A New Hope"
|
58
|
+
}')
|
59
|
+
api_key_id = "test_url_param"
|
60
|
+
response = sg.client.api_keys._(api_key_id).patch(request_body: data)
|
61
|
+
puts response.status_code
|
62
|
+
puts response.body
|
63
|
+
puts response.headers
|
64
|
+
|
65
|
+
##################################################
|
66
|
+
# Retrieve an existing API Key #
|
67
|
+
# GET /api_keys/{api_key_id} #
|
68
|
+
|
69
|
+
api_key_id = "test_url_param"
|
70
|
+
response = sg.client.api_keys._(api_key_id).get()
|
71
|
+
puts response.status_code
|
72
|
+
puts response.body
|
73
|
+
puts response.headers
|
74
|
+
|
75
|
+
##################################################
|
76
|
+
# Delete API keys #
|
77
|
+
# DELETE /api_keys/{api_key_id} #
|
78
|
+
|
79
|
+
api_key_id = "test_url_param"
|
80
|
+
response = sg.client.api_keys._(api_key_id).delete()
|
81
|
+
puts response.status_code
|
82
|
+
puts response.body
|
83
|
+
puts response.headers
|
84
|
+
|