sendgrid-ruby 1.0.5 → 1.1.5
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/.gitignore +0 -1
- data/CHANGELOG.md +10 -3
- data/README.md +62 -3
- data/lib/sendgrid-ruby.rb +3 -0
- data/lib/sendgrid/client.rb +5 -5
- data/lib/sendgrid/mail.rb +16 -7
- data/lib/sendgrid/recipient.rb +29 -0
- data/lib/sendgrid/template.rb +26 -0
- data/lib/sendgrid/template_mailer.rb +59 -0
- data/lib/sendgrid/version.rb +1 -1
- data/spec/lib/sendgrid/client_spec.rb +4 -0
- data/spec/lib/sendgrid/mail_spec.rb +28 -0
- data/spec/lib/sendgrid/recipient_spec.rb +91 -0
- data/spec/lib/sendgrid/template_mailer_spec.rb +86 -0
- data/spec/lib/sendgrid/template_spec.rb +61 -0
- data/spec/lib/sendgrid_spec.rb +1 -1
- metadata +11 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 3c556cc58a72b04d8db231eb81396415aa3d5007
|
4
|
+
data.tar.gz: b86fde7d2356ecf19acc374aac5f429f0b425ea2
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: b3138df821abe297098687b31936d15d50d49e712abae0fe2a34701e3a450f6526dacccc95166c8538bc3350a08f4f9a7894e809da2f4ab2da771576d98a3f84
|
7
|
+
data.tar.gz: 2127a04422ddd3a3c4061977c7b9f0ff394c150e4154c862a81d7d649cbd96e706d9fc6e741434d46760e6ff4ec518a04d0c87d44b2d1fb522696db1577ce2e9
|
data/.gitignore
CHANGED
data/CHANGELOG.md
CHANGED
@@ -1,14 +1,21 @@
|
|
1
1
|
# Change Log
|
2
2
|
All notable changes to this project will be documented in this file.
|
3
3
|
|
4
|
-
## [1.
|
4
|
+
## [1.1.5] - 2015-10-28 ##
|
5
|
+
## Added
|
6
|
+
Support for [Templates](https://github.com/sendgrid/sendgrid-ruby#working-with-templates) via [#28](https://github.com/sendgrid/sendgrid-ruby/pull/28)
|
7
|
+
|
8
|
+
Thanks [Jake](https://github.com/yez)!
|
9
|
+
|
10
|
+
|
11
|
+
## [1.0.5] - 2015-10-21 ##
|
5
12
|
### Fixed
|
6
13
|
Remove puts from mail.rb [#29](https://github.com/sendgrid/sendgrid-ruby/pull/29)
|
7
14
|
|
8
|
-
## [1.0.4] - 2015-10-06
|
15
|
+
## [1.0.4] - 2015-10-06 ##
|
9
16
|
### Added
|
10
17
|
Inline content support
|
11
18
|
|
12
|
-
## [1.0.3] - 2015-10-01
|
19
|
+
## [1.0.3] - 2015-10-01 ##
|
13
20
|
### Fixed
|
14
21
|
Payload 'to' attribute fix for smtpapi
|
data/README.md
CHANGED
@@ -106,11 +106,11 @@ params = {
|
|
106
106
|
:reply_to,
|
107
107
|
:date,
|
108
108
|
:smtpapi,
|
109
|
-
:attachments
|
109
|
+
:attachments,
|
110
|
+
:template
|
110
111
|
}
|
111
112
|
```
|
112
113
|
|
113
|
-
|
114
114
|
#### Setting Params
|
115
115
|
|
116
116
|
Params can be set in the usual Ruby ways, including a block or a hash.
|
@@ -187,9 +187,67 @@ mail = SendGrid::Mail.new
|
|
187
187
|
mail.html = '<html><body>Stuff in here, yo!</body></html>'
|
188
188
|
```
|
189
189
|
|
190
|
+
#### :template
|
190
191
|
|
191
|
-
|
192
|
+
The **:template** param allows us to specify a template object for this email to use. The initialized `Template` will automatically be included in the `smtpapi` header and passed to SendGrid.
|
193
|
+
|
194
|
+
```ruby
|
195
|
+
template = SendGrid::Template.new('MY_TEMPLATE_ID')
|
196
|
+
mail.template = template
|
197
|
+
```
|
198
|
+
|
199
|
+
## Working with Templates
|
200
|
+
|
201
|
+
Another easy way to use the [SendGrid Templating](https://sendgrid.com/docs/API_Reference/Web_API_v3/Template_Engine/index.html) system is with the `Recipient`, `Mail`, `Template`, and `TemplateMailer` objects.
|
202
|
+
|
203
|
+
Create some `Recipients`
|
204
|
+
|
205
|
+
```ruby
|
206
|
+
users = User.where(email: ['first@gmail.com', 'second@gmail.com'])
|
207
|
+
|
208
|
+
recipients = []
|
209
|
+
|
210
|
+
users.each do |user|
|
211
|
+
recipient = SendGrid::Recipient.new(user.email)
|
212
|
+
recipient.add_substitution('first_name', user.first_name)
|
213
|
+
recipient.add_substitution('city', user.city)
|
192
214
|
|
215
|
+
recipients << recipient
|
216
|
+
end
|
217
|
+
```
|
218
|
+
|
219
|
+
Create a `Template`
|
220
|
+
|
221
|
+
```ruby
|
222
|
+
template = SendGrid::Template.new('MY_TEMPLATE_ID')
|
223
|
+
```
|
224
|
+
|
225
|
+
Create a `Client`
|
226
|
+
|
227
|
+
```ruby
|
228
|
+
client = SendGrid::Client.new(api_user: my_user, api_key: my_key)
|
229
|
+
```
|
230
|
+
|
231
|
+
Initialize mail defaults and create the `TemplateMailer`
|
232
|
+
|
233
|
+
```ruby
|
234
|
+
mail_defaults = {
|
235
|
+
from: 'admin@email.com',
|
236
|
+
html: '<h1>I like email</h1>',
|
237
|
+
text: 'I like email',
|
238
|
+
subject: 'Email is great',
|
239
|
+
}
|
240
|
+
|
241
|
+
mailer = SendGrid::TemplateMailer.new(client, template, recipients)
|
242
|
+
```
|
243
|
+
|
244
|
+
Mail it!
|
245
|
+
|
246
|
+
```ruby
|
247
|
+
mailer.mail(mail_defaults)
|
248
|
+
```
|
249
|
+
|
250
|
+
## Using SendGrid's X-SMTPAPI Header
|
193
251
|
|
194
252
|
<blockquote>
|
195
253
|
To utilize the X-SMTPAPI header, we have directly integrated the <a href="https://github.com/SendGridJP/smtpapi-ruby">SendGridJP/smtpapi-ruby</a> gem.
|
@@ -233,3 +291,4 @@ mail.smtpapi = header
|
|
233
291
|
5. Create a new Pull Request
|
234
292
|
|
235
293
|
***Hit up [@rbin](http://twitter.com/rbin) or [@sendgrid](http://twitter.com/sendgrid) on Twitter with any issues.***
|
294
|
+
|
data/lib/sendgrid-ruby.rb
CHANGED
@@ -1,5 +1,8 @@
|
|
1
1
|
require_relative 'sendgrid/client'
|
2
2
|
require_relative 'sendgrid/exceptions'
|
3
|
+
require_relative 'sendgrid/recipient'
|
4
|
+
require_relative 'sendgrid/template'
|
5
|
+
require_relative 'sendgrid/template_mailer'
|
3
6
|
require_relative 'sendgrid/mail'
|
4
7
|
require_relative 'sendgrid/response'
|
5
8
|
require_relative 'sendgrid/version'
|
data/lib/sendgrid/client.rb
CHANGED
@@ -3,7 +3,7 @@ require 'faraday'
|
|
3
3
|
module SendGrid
|
4
4
|
class Client
|
5
5
|
attr_accessor :api_user, :api_key, :protocol, :host, :port, :url, :endpoint,
|
6
|
-
:user_agent
|
6
|
+
:user_agent, :template
|
7
7
|
attr_writer :adapter, :conn, :raise_exceptions
|
8
8
|
|
9
9
|
def initialize(params = {})
|
@@ -25,7 +25,7 @@ module SendGrid
|
|
25
25
|
res = conn.post do |req|
|
26
26
|
payload = mail.to_h
|
27
27
|
req.url(endpoint)
|
28
|
-
|
28
|
+
|
29
29
|
# Check if using username + password or API key
|
30
30
|
if api_user
|
31
31
|
# Username + password
|
@@ -34,12 +34,12 @@ module SendGrid
|
|
34
34
|
# API key
|
35
35
|
req.headers['Authorization'] = "Bearer #{api_key}"
|
36
36
|
end
|
37
|
-
|
37
|
+
|
38
38
|
req.body = payload
|
39
39
|
end
|
40
|
-
|
40
|
+
|
41
41
|
fail SendGrid::Exception, res.body if raise_exceptions? && res.status != 200
|
42
|
-
|
42
|
+
|
43
43
|
SendGrid::Response.new(code: res.status, headers: res.headers, body: res.body)
|
44
44
|
end
|
45
45
|
|
data/lib/sendgrid/mail.rb
CHANGED
@@ -5,12 +5,13 @@ require 'mimemagic'
|
|
5
5
|
module SendGrid
|
6
6
|
class Mail
|
7
7
|
attr_accessor :to, :to_name, :from, :from_name, :subject, :text, :html, :cc,
|
8
|
-
:bcc, :reply_to, :date, :smtpapi, :attachments, :content
|
8
|
+
:bcc, :reply_to, :date, :smtpapi, :attachments, :content, :template
|
9
9
|
|
10
10
|
def initialize(params = {})
|
11
11
|
params.each do |k, v|
|
12
12
|
send(:"#{k}=", v) unless v.nil?
|
13
13
|
end
|
14
|
+
|
14
15
|
yield self if block_given?
|
15
16
|
end
|
16
17
|
|
@@ -103,11 +104,11 @@ module SendGrid
|
|
103
104
|
def attachments
|
104
105
|
@attachments ||= []
|
105
106
|
end
|
106
|
-
|
107
|
+
|
107
108
|
def contents
|
108
109
|
@contents ||= []
|
109
110
|
end
|
110
|
-
|
111
|
+
|
111
112
|
def add_content(path, cid)
|
112
113
|
mime_type = MimeMagic.by_path(path)
|
113
114
|
file = Faraday::UploadIO.new(path, mime_type)
|
@@ -119,6 +120,14 @@ module SendGrid
|
|
119
120
|
@smtpapi ||= Smtpapi::Header.new
|
120
121
|
end
|
121
122
|
|
123
|
+
def smtpapi_json
|
124
|
+
if !template.nil? && template.is_a?(Template)
|
125
|
+
template.add_to_smtpapi(smtpapi)
|
126
|
+
end
|
127
|
+
|
128
|
+
smtpapi.to_json
|
129
|
+
end
|
130
|
+
|
122
131
|
# rubocop:disable Style/HashSyntax
|
123
132
|
def to_h
|
124
133
|
payload = {
|
@@ -133,7 +142,7 @@ module SendGrid
|
|
133
142
|
:bcc => bcc,
|
134
143
|
:text => text,
|
135
144
|
:html => html,
|
136
|
-
:'x-smtpapi' =>
|
145
|
+
:'x-smtpapi' => smtpapi_json,
|
137
146
|
:content => ({":default"=>"0"} unless contents.empty?),
|
138
147
|
:files => ({":default"=>"0"} unless attachments.empty? and contents.empty?)
|
139
148
|
# If I don't define a default value, I get a Nil error when
|
@@ -144,7 +153,7 @@ module SendGrid
|
|
144
153
|
payload.delete(:'x-smtpapi') if payload[:'x-smtpapi'] == '{}'
|
145
154
|
|
146
155
|
payload[:to] = payload[:from] if payload[:to].nil? and not smtpapi.to.empty?
|
147
|
-
|
156
|
+
|
148
157
|
unless attachments.empty?
|
149
158
|
attachments.each do |file|
|
150
159
|
payload[:files][file[:name]] = file[:file]
|
@@ -153,7 +162,7 @@ module SendGrid
|
|
153
162
|
payload[:files].delete(":default")
|
154
163
|
end
|
155
164
|
end
|
156
|
-
|
165
|
+
|
157
166
|
unless contents.empty?
|
158
167
|
contents.each do |content|
|
159
168
|
payload[:content][content[:name]] = content[:cid]
|
@@ -163,7 +172,7 @@ module SendGrid
|
|
163
172
|
payload[:content].delete(":default")
|
164
173
|
end
|
165
174
|
end
|
166
|
-
|
175
|
+
|
167
176
|
payload
|
168
177
|
end
|
169
178
|
# rubocop:enable Style/HashSyntax
|
@@ -0,0 +1,29 @@
|
|
1
|
+
require 'smtpapi'
|
2
|
+
|
3
|
+
module SendGrid
|
4
|
+
class Recipient
|
5
|
+
class NoAddress < StandardError; end
|
6
|
+
|
7
|
+
attr_reader :address, :substitutions
|
8
|
+
|
9
|
+
def initialize(address)
|
10
|
+
@address = address
|
11
|
+
@substitutions = {}
|
12
|
+
|
13
|
+
raise NoAddress, 'Recipient address cannot be nil' if @address.nil?
|
14
|
+
end
|
15
|
+
|
16
|
+
def add_substitution(key, value)
|
17
|
+
substitutions[key] = value
|
18
|
+
end
|
19
|
+
|
20
|
+
def add_to_smtpapi(smtpapi)
|
21
|
+
smtpapi.add_to(@address)
|
22
|
+
|
23
|
+
@substitutions.each do |key, value|
|
24
|
+
existing = smtpapi.sub[key] || []
|
25
|
+
smtpapi.add_substitution(key, existing + [value])
|
26
|
+
end
|
27
|
+
end
|
28
|
+
end
|
29
|
+
end
|
@@ -0,0 +1,26 @@
|
|
1
|
+
require 'smtpapi'
|
2
|
+
|
3
|
+
module SendGrid
|
4
|
+
class Template
|
5
|
+
attr_reader :id, :recipients
|
6
|
+
|
7
|
+
def initialize(id)
|
8
|
+
@id = id
|
9
|
+
@recipients = []
|
10
|
+
end
|
11
|
+
|
12
|
+
def add_recipient(recipient)
|
13
|
+
recipients << recipient
|
14
|
+
end
|
15
|
+
|
16
|
+
def add_to_smtpapi(smtpapi)
|
17
|
+
return if smtpapi.nil?
|
18
|
+
|
19
|
+
smtpapi.tap do |api|
|
20
|
+
api.add_filter(:templates, :enable, 1)
|
21
|
+
api.add_filter(:templates, :template_id, id)
|
22
|
+
recipients.each { |r| r.add_to_smtpapi(smtpapi) }
|
23
|
+
end
|
24
|
+
end
|
25
|
+
end
|
26
|
+
end
|
@@ -0,0 +1,59 @@
|
|
1
|
+
module SendGrid
|
2
|
+
class InvalidClient < StandardError; end
|
3
|
+
class InvalidTemplate < StandardError; end
|
4
|
+
class InvalidRecipients < StandardError; end
|
5
|
+
|
6
|
+
class TemplateMailer
|
7
|
+
|
8
|
+
# This class is responsible for coordinating the responsibilities
|
9
|
+
# of various models in the gem.
|
10
|
+
# It makes use of the Recipient, Template and Mail models to create
|
11
|
+
# a single work flow, an example might look like:
|
12
|
+
#
|
13
|
+
# users = User.where(email: ['first@gmail.com', 'second@gmail.com'])
|
14
|
+
#
|
15
|
+
# recipients = []
|
16
|
+
#
|
17
|
+
# users.each do |user|
|
18
|
+
# recipient = SendGrid::Recipient.new(user.email)
|
19
|
+
# recipient.add_substitution('first_name', user.first_name)
|
20
|
+
# recipient.add_substitution('city', user.city)
|
21
|
+
#
|
22
|
+
# recipients << recipient
|
23
|
+
# end
|
24
|
+
#
|
25
|
+
# template = SendGrid::Template.new('MY_TEMPLATE_ID')
|
26
|
+
#
|
27
|
+
# client = SendGrid::Client.new(api_user: my_user, api_key: my_key)
|
28
|
+
#
|
29
|
+
# mail_defaults = {
|
30
|
+
# from: 'admin@email.com',
|
31
|
+
# html: '<h1>I like email</h1>',
|
32
|
+
# text: 'I like email',
|
33
|
+
# subject: 'Email is great',
|
34
|
+
# }
|
35
|
+
#
|
36
|
+
# mailer = SendGrid::TemplateMailer.new(client, template, recipients)
|
37
|
+
# mailer.mail(mail_defaults)
|
38
|
+
def initialize(client, template, recipients = [])
|
39
|
+
@client = client
|
40
|
+
@template = template
|
41
|
+
@recipients = recipients
|
42
|
+
|
43
|
+
raise InvalidClient, 'Client must be present' if @client.nil?
|
44
|
+
raise InvalidTemplate, 'Template must be present' if @template.nil?
|
45
|
+
raise InvalidRecipients, 'Recipients may not be empty' if @recipients.empty?
|
46
|
+
|
47
|
+
@recipients.each do |recipient|
|
48
|
+
@template.add_recipient(recipient)
|
49
|
+
end
|
50
|
+
end
|
51
|
+
|
52
|
+
def mail(params = {})
|
53
|
+
mail = Mail.new(params)
|
54
|
+
|
55
|
+
mail.template = @template
|
56
|
+
@client.send(mail.to_h)
|
57
|
+
end
|
58
|
+
end
|
59
|
+
end
|
data/lib/sendgrid/version.rb
CHANGED
@@ -23,6 +23,10 @@ describe 'SendGrid::Client' do
|
|
23
23
|
expect(SendGrid::Client.new.endpoint).to eq('/api/mail.send.json')
|
24
24
|
end
|
25
25
|
|
26
|
+
it 'accepts a block' do
|
27
|
+
expect { |b| SendGrid::Client.new(&b) }.to yield_control
|
28
|
+
end
|
29
|
+
|
26
30
|
describe ':send' do
|
27
31
|
it 'should make a request to sendgrid' do
|
28
32
|
stub_request(:any, 'https://api.sendgrid.com/api/mail.send.json')
|
@@ -120,4 +120,32 @@ describe 'SendGrid::Mail' do
|
|
120
120
|
expect(@mail.reply_to).to eq('foo@example.com')
|
121
121
|
end
|
122
122
|
end
|
123
|
+
|
124
|
+
describe 'smtpapi_json' do
|
125
|
+
before do
|
126
|
+
@mail.template = template
|
127
|
+
end
|
128
|
+
|
129
|
+
context 'a template has been set' do
|
130
|
+
let(:template) { SendGrid::Template.new(anything) }
|
131
|
+
|
132
|
+
it 'adds the template to the smtpapi header' do
|
133
|
+
expect(@mail.template).to receive(:add_to_smtpapi).with(@mail.smtpapi)
|
134
|
+
expect(@mail.smtpapi).to receive(:to_json)
|
135
|
+
|
136
|
+
@mail.to_h
|
137
|
+
end
|
138
|
+
end
|
139
|
+
|
140
|
+
context 'no template has been set' do
|
141
|
+
let(:template) { nil }
|
142
|
+
|
143
|
+
it 'does not add anything to the smtpapi header' do
|
144
|
+
expect_any_instance_of(SendGrid::Template).to_not receive(:add_to_smtpapi)
|
145
|
+
expect(@mail.smtpapi).to receive(:to_json)
|
146
|
+
|
147
|
+
@mail.to_h
|
148
|
+
end
|
149
|
+
end
|
150
|
+
end
|
123
151
|
end
|
@@ -0,0 +1,91 @@
|
|
1
|
+
require_relative '../../../lib/sendgrid/recipient'
|
2
|
+
|
3
|
+
module SendGrid
|
4
|
+
describe Recipient do
|
5
|
+
subject { described_class.new(anything) }
|
6
|
+
|
7
|
+
describe '#initialize' do
|
8
|
+
it 'sets the address instance var' do
|
9
|
+
expect(subject.instance_variable_get(:@address)).to_not be_nil
|
10
|
+
end
|
11
|
+
|
12
|
+
it 'sets substitutions to an empty hash' do
|
13
|
+
expect(subject.instance_variable_get(:@substitutions)).to eq({})
|
14
|
+
end
|
15
|
+
|
16
|
+
context 'initialized with nil' do
|
17
|
+
it 'raises an error' do
|
18
|
+
expect do
|
19
|
+
described_class.new(nil)
|
20
|
+
end.to raise_error(Recipient::NoAddress, 'Recipient address cannot be nil')
|
21
|
+
end
|
22
|
+
end
|
23
|
+
end
|
24
|
+
|
25
|
+
describe '#add_substitution' do
|
26
|
+
it 'adds the key and value to the substitutions hash' do
|
27
|
+
subject.add_substitution(:foo, :bar)
|
28
|
+
expect(subject.substitutions).to have_key(:foo)
|
29
|
+
expect(subject.substitutions[:foo]).to eq(:bar)
|
30
|
+
end
|
31
|
+
|
32
|
+
context 'the same substiution key already exists' do
|
33
|
+
before do
|
34
|
+
subject.add_substitution(:foo, :bar)
|
35
|
+
end
|
36
|
+
|
37
|
+
it 'replaces the value' do
|
38
|
+
subject.add_substitution(:foo, :baz)
|
39
|
+
expect(subject.substitutions).to have_key(:foo)
|
40
|
+
expect(subject.substitutions[:foo]).to eq(:baz)
|
41
|
+
end
|
42
|
+
end
|
43
|
+
end
|
44
|
+
|
45
|
+
describe '#add_to_smtpapi' do
|
46
|
+
let(:substitutions) { { foo: :bar, baz: :qux } }
|
47
|
+
let(:smtp_api) { Smtpapi::Header.new }
|
48
|
+
before do
|
49
|
+
substitutions.each do |key, value|
|
50
|
+
subject.add_substitution(key, value)
|
51
|
+
end
|
52
|
+
end
|
53
|
+
|
54
|
+
it 'adds the address' do
|
55
|
+
expect(smtp_api).to receive(:add_to)
|
56
|
+
subject.add_to_smtpapi(smtp_api)
|
57
|
+
end
|
58
|
+
|
59
|
+
it 'calls add_substitution as many times as there are substitution keys' do
|
60
|
+
substitutions.each do |key, value|
|
61
|
+
expect(smtp_api).to receive(:add_substitution).with(key, [value])
|
62
|
+
end
|
63
|
+
|
64
|
+
subject.add_to_smtpapi(smtp_api)
|
65
|
+
end
|
66
|
+
|
67
|
+
context 'a substitution for the same key already exists' do
|
68
|
+
let(:substitutions) { { foo: :bar } }
|
69
|
+
let(:added_value) { [:bar, :rab] }
|
70
|
+
|
71
|
+
before do
|
72
|
+
smtp_api.add_substitution(:foo, [:rab])
|
73
|
+
end
|
74
|
+
|
75
|
+
it 'adds to it' do
|
76
|
+
expect(smtp_api).to receive(:add_substitution).with(:foo, array_including(added_value))
|
77
|
+
subject.add_to_smtpapi(smtp_api)
|
78
|
+
end
|
79
|
+
end
|
80
|
+
|
81
|
+
context 'substitutions is empty' do
|
82
|
+
let(:substitutions) { {} }
|
83
|
+
|
84
|
+
it 'does nothing' do
|
85
|
+
expect(smtp_api).to_not receive(:add_substitution)
|
86
|
+
subject.add_to_smtpapi(smtp_api)
|
87
|
+
end
|
88
|
+
end
|
89
|
+
end
|
90
|
+
end
|
91
|
+
end
|
@@ -0,0 +1,86 @@
|
|
1
|
+
require_relative '../../../lib/sendgrid/template_mailer'
|
2
|
+
|
3
|
+
module SendGrid
|
4
|
+
describe TemplateMailer do
|
5
|
+
let(:client) { anything }
|
6
|
+
let(:template) { Template.new(anything) }
|
7
|
+
let(:recipients) { [Recipient.new(anything)] }
|
8
|
+
|
9
|
+
describe '#initialize' do
|
10
|
+
let(:client) { anything }
|
11
|
+
let(:template) { Template.new(anything) }
|
12
|
+
let(:recipients) { [anything] }
|
13
|
+
|
14
|
+
subject { described_class.new(client, template, recipients) }
|
15
|
+
|
16
|
+
it 'sets the instance variables' do
|
17
|
+
expect(subject.instance_variable_get(:@client)).to_not be_nil
|
18
|
+
expect(subject.instance_variable_get(:@template)).to_not be_nil
|
19
|
+
expect(subject.instance_variable_get(:@recipients)).to_not be_nil
|
20
|
+
end
|
21
|
+
|
22
|
+
context 'nil variables' do
|
23
|
+
context 'template is nil' do
|
24
|
+
let(:template) { nil }
|
25
|
+
|
26
|
+
it 'raises error' do
|
27
|
+
expect do
|
28
|
+
subject
|
29
|
+
end.to raise_error(InvalidTemplate, 'Template must be present')
|
30
|
+
end
|
31
|
+
end
|
32
|
+
|
33
|
+
context 'client is nil' do
|
34
|
+
let(:client) { nil }
|
35
|
+
|
36
|
+
it 'raises error' do
|
37
|
+
expect do
|
38
|
+
subject
|
39
|
+
end.to raise_error(InvalidClient, 'Client must be present')
|
40
|
+
end
|
41
|
+
end
|
42
|
+
end
|
43
|
+
|
44
|
+
context 'recipients' do
|
45
|
+
let(:first_recipient) { Recipient.new('someone@anything.com') }
|
46
|
+
let(:second_recipient) { Recipient.new('test@test.com') }
|
47
|
+
let(:recipients) { [first_recipient, second_recipient] }
|
48
|
+
|
49
|
+
it 'adds them to the template' do
|
50
|
+
expect(template).to receive(:add_recipient).with(first_recipient)
|
51
|
+
expect(template).to receive(:add_recipient).with(second_recipient)
|
52
|
+
|
53
|
+
subject
|
54
|
+
end
|
55
|
+
end
|
56
|
+
end
|
57
|
+
|
58
|
+
describe '#mail' do
|
59
|
+
subject { described_class.new(client, template, recipients) }
|
60
|
+
|
61
|
+
let(:mail_params) { {} }
|
62
|
+
let(:mail_to_h) { '' }
|
63
|
+
|
64
|
+
before do
|
65
|
+
allow(subject).to receive(:mail_params) { mail_params }
|
66
|
+
allow_any_instance_of(Mail).to receive(:to_h) { mail_to_h }
|
67
|
+
allow(client).to receive(:send)
|
68
|
+
end
|
69
|
+
|
70
|
+
it 'creates a new mail object' do
|
71
|
+
expect(Mail).to receive(:new).with(mail_params).and_call_original
|
72
|
+
subject.mail
|
73
|
+
end
|
74
|
+
|
75
|
+
it 'adds the template to the mail object' do
|
76
|
+
expect_any_instance_of(Mail).to receive(:template=).with(template)
|
77
|
+
subject.mail
|
78
|
+
end
|
79
|
+
|
80
|
+
it 'calls send on the client with the mail object' do
|
81
|
+
expect(client).to receive(:send).with(mail_to_h)
|
82
|
+
subject.mail
|
83
|
+
end
|
84
|
+
end
|
85
|
+
end
|
86
|
+
end
|
@@ -0,0 +1,61 @@
|
|
1
|
+
require_relative '../../../lib/sendgrid/template'
|
2
|
+
|
3
|
+
module SendGrid
|
4
|
+
describe Template do
|
5
|
+
let(:id) { anything }
|
6
|
+
subject { described_class.new(id) }
|
7
|
+
|
8
|
+
describe '#initialize' do
|
9
|
+
it 'sets the id instance var' do
|
10
|
+
expect(subject.instance_variable_get(:@id)).to_not be_nil
|
11
|
+
end
|
12
|
+
end
|
13
|
+
|
14
|
+
describe '#add_to_smtpapi' do
|
15
|
+
let(:id) { rand(8999) }
|
16
|
+
let(:smtpapi) { Smtpapi::Header.new }
|
17
|
+
|
18
|
+
it 'adds enabled and the templates id' do
|
19
|
+
expect(smtpapi).to receive(:add_filter).with(:templates, :enable, 1)
|
20
|
+
expect(smtpapi).to receive(:add_filter).with(:templates, :template_id, id)
|
21
|
+
subject.add_to_smtpapi(smtpapi)
|
22
|
+
end
|
23
|
+
|
24
|
+
context 'smtpapi is nil' do
|
25
|
+
it 'does not error' do
|
26
|
+
expect do
|
27
|
+
subject.add_to_smtpapi(nil)
|
28
|
+
end.to_not raise_error
|
29
|
+
end
|
30
|
+
end
|
31
|
+
|
32
|
+
context 'with recipients' do
|
33
|
+
let(:substitution_key) { :foo }
|
34
|
+
let(:substitution_value) { :bar }
|
35
|
+
let(:recipients) do
|
36
|
+
[].tap do |recipients|
|
37
|
+
3.times.each do
|
38
|
+
r = Recipient.new("test+#{ rand(100) }@example.com")
|
39
|
+
r.add_substitution(substitution_key, substitution_value)
|
40
|
+
recipients << r
|
41
|
+
end
|
42
|
+
end
|
43
|
+
end
|
44
|
+
|
45
|
+
before do
|
46
|
+
recipients.each do |r|
|
47
|
+
subject.add_recipient(r)
|
48
|
+
end
|
49
|
+
end
|
50
|
+
|
51
|
+
it 'calls the recipients call to add to smtpapi' do
|
52
|
+
recipients.each do |recipient|
|
53
|
+
expect(recipient).to receive(:add_to_smtpapi).with(smtpapi)
|
54
|
+
end
|
55
|
+
|
56
|
+
subject.add_to_smtpapi(smtpapi)
|
57
|
+
end
|
58
|
+
end
|
59
|
+
end
|
60
|
+
end
|
61
|
+
end
|
data/spec/lib/sendgrid_spec.rb
CHANGED
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: sendgrid-ruby
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 1.
|
4
|
+
version: 1.1.5
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Robin Johnson
|
@@ -9,7 +9,7 @@ authors:
|
|
9
9
|
autorequire:
|
10
10
|
bindir: bin
|
11
11
|
cert_chain: []
|
12
|
-
date: 2015-10-
|
12
|
+
date: 2015-10-28 00:00:00.000000000 Z
|
13
13
|
dependencies:
|
14
14
|
- !ruby/object:Gem::Dependency
|
15
15
|
name: smtpapi
|
@@ -215,11 +215,17 @@ files:
|
|
215
215
|
- lib/sendgrid/client.rb
|
216
216
|
- lib/sendgrid/exceptions.rb
|
217
217
|
- lib/sendgrid/mail.rb
|
218
|
+
- lib/sendgrid/recipient.rb
|
218
219
|
- lib/sendgrid/response.rb
|
220
|
+
- lib/sendgrid/template.rb
|
221
|
+
- lib/sendgrid/template_mailer.rb
|
219
222
|
- lib/sendgrid/version.rb
|
220
223
|
- sendgrid-ruby.gemspec
|
221
224
|
- spec/lib/sendgrid/client_spec.rb
|
222
225
|
- spec/lib/sendgrid/mail_spec.rb
|
226
|
+
- spec/lib/sendgrid/recipient_spec.rb
|
227
|
+
- spec/lib/sendgrid/template_mailer_spec.rb
|
228
|
+
- spec/lib/sendgrid/template_spec.rb
|
223
229
|
- spec/lib/sendgrid_spec.rb
|
224
230
|
- spec/spec_helper.rb
|
225
231
|
homepage: http://github.com/sendgrid/sendgrid-ruby
|
@@ -249,5 +255,8 @@ summary: Official SendGrid Gem
|
|
249
255
|
test_files:
|
250
256
|
- spec/lib/sendgrid/client_spec.rb
|
251
257
|
- spec/lib/sendgrid/mail_spec.rb
|
258
|
+
- spec/lib/sendgrid/recipient_spec.rb
|
259
|
+
- spec/lib/sendgrid/template_mailer_spec.rb
|
260
|
+
- spec/lib/sendgrid/template_spec.rb
|
252
261
|
- spec/lib/sendgrid_spec.rb
|
253
262
|
- spec/spec_helper.rb
|