sendgrid-ruby 1.1.6 → 6.0.4
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- 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/lib/sendgrid/recipient.rb
DELETED
|
@@ -1,29 +0,0 @@
|
|
|
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
|
data/lib/sendgrid/response.rb
DELETED
|
@@ -1,14 +0,0 @@
|
|
|
1
|
-
require 'json'
|
|
2
|
-
|
|
3
|
-
module SendGrid
|
|
4
|
-
class Response
|
|
5
|
-
attr_reader :code, :headers, :body, :raw_body
|
|
6
|
-
|
|
7
|
-
def initialize(params)
|
|
8
|
-
@code = params[:code]
|
|
9
|
-
@headers = params[:headers]
|
|
10
|
-
@body = JSON.parse(params[:body])
|
|
11
|
-
@raw_body = params[:body]
|
|
12
|
-
end
|
|
13
|
-
end
|
|
14
|
-
end
|
data/lib/sendgrid/template.rb
DELETED
|
@@ -1,26 +0,0 @@
|
|
|
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
|
|
@@ -1,59 +0,0 @@
|
|
|
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
|
|
@@ -1,87 +0,0 @@
|
|
|
1
|
-
require 'json'
|
|
2
|
-
require 'spec_helper'
|
|
3
|
-
require 'webmock/rspec'
|
|
4
|
-
|
|
5
|
-
describe 'SendGrid::Client' do
|
|
6
|
-
it 'should accept a username and password' do
|
|
7
|
-
expect(SendGrid::Client.new(api_user: 'test', api_key: 'test')).to be_an_instance_of(SendGrid::Client)
|
|
8
|
-
end
|
|
9
|
-
|
|
10
|
-
it 'should accept an api key' do
|
|
11
|
-
expect(SendGrid::Client.new(api_key: 'sendgrid_123')).to be_an_instance_of(SendGrid::Client)
|
|
12
|
-
end
|
|
13
|
-
|
|
14
|
-
it 'should build the default url' do
|
|
15
|
-
expect(SendGrid::Client.new.url).to eq('https://api.sendgrid.com')
|
|
16
|
-
end
|
|
17
|
-
|
|
18
|
-
it 'should build a custom url' do
|
|
19
|
-
expect(SendGrid::Client.new(port: 3000, host: 'foo.sendgrid.com', protocol: 'tacos').url).to eq('tacos://foo.sendgrid.com:3000')
|
|
20
|
-
end
|
|
21
|
-
|
|
22
|
-
it 'should use the default endpoint' do
|
|
23
|
-
expect(SendGrid::Client.new.endpoint).to eq('/api/mail.send.json')
|
|
24
|
-
end
|
|
25
|
-
|
|
26
|
-
it 'accepts a block' do
|
|
27
|
-
expect { |b| SendGrid::Client.new(&b) }.to yield_control
|
|
28
|
-
end
|
|
29
|
-
|
|
30
|
-
describe ':send' do
|
|
31
|
-
it 'should make a request to sendgrid' do
|
|
32
|
-
stub_request(:any, 'https://api.sendgrid.com/api/mail.send.json')
|
|
33
|
-
.to_return(body: {message: 'success'}.to_json, status: 200, headers: {'X-TEST' => 'yes'})
|
|
34
|
-
|
|
35
|
-
client = SendGrid::Client.new(api_key: 'abc123')
|
|
36
|
-
mail = SendGrid::Mail.new
|
|
37
|
-
res = client.send(mail)
|
|
38
|
-
expect(res.code).to eq(200)
|
|
39
|
-
end
|
|
40
|
-
|
|
41
|
-
it 'should have an auth header when using an api key' do
|
|
42
|
-
stub_request(:any, 'https://api.sendgrid.com/api/mail.send.json')
|
|
43
|
-
.to_return(body: {message: 'success'}.to_json, status: 200, headers: {'X-TEST' => 'yes'})
|
|
44
|
-
|
|
45
|
-
client = SendGrid::Client.new(api_key: 'abc123')
|
|
46
|
-
mail = SendGrid::Mail.new
|
|
47
|
-
|
|
48
|
-
client.send(mail)
|
|
49
|
-
|
|
50
|
-
expect(WebMock).to have_requested(:post, 'https://api.sendgrid.com/api/mail.send.json')
|
|
51
|
-
.with(headers: {'Authorization' => 'Bearer abc123'})
|
|
52
|
-
end
|
|
53
|
-
|
|
54
|
-
it 'should have a username + password when using them' do
|
|
55
|
-
stub_request(:any, 'https://api.sendgrid.com/api/mail.send.json')
|
|
56
|
-
.to_return(body: {message: 'success'}.to_json, status: 200, headers: {'X-TEST' => 'yes'})
|
|
57
|
-
|
|
58
|
-
client = SendGrid::Client.new(api_user: 'foobar', api_key: 'abc123')
|
|
59
|
-
mail = SendGrid::Mail.new
|
|
60
|
-
|
|
61
|
-
res = client.send(mail)
|
|
62
|
-
|
|
63
|
-
expect(WebMock).to have_requested(:post, 'https://api.sendgrid.com/api/mail.send.json')
|
|
64
|
-
.with(body: 'api_key=abc123&api_user=foobar')
|
|
65
|
-
end
|
|
66
|
-
|
|
67
|
-
it 'should raise a SendGrid::Exception if status is not 200' do
|
|
68
|
-
stub_request(:any, 'https://api.sendgrid.com/api/mail.send.json')
|
|
69
|
-
.to_return(body: {message: 'error', errors: ['Bad username / password']}.to_json, status: 400, headers: {'X-TEST' => 'yes'})
|
|
70
|
-
|
|
71
|
-
client = SendGrid::Client.new(api_user: 'foobar', api_key: 'abc123')
|
|
72
|
-
mail = SendGrid::Mail.new
|
|
73
|
-
|
|
74
|
-
expect {client.send(mail)}.to raise_error(SendGrid::Exception)
|
|
75
|
-
end
|
|
76
|
-
|
|
77
|
-
it 'should not raise a SendGrid::Exception if raise_exceptions is disabled' do
|
|
78
|
-
stub_request(:any, 'https://api.sendgrid.com/api/mail.send.json')
|
|
79
|
-
.to_return(body: {message: 'error', errors: ['Bad username / password']}.to_json, status: 400, headers: {'X-TEST' => 'yes'})
|
|
80
|
-
|
|
81
|
-
client = SendGrid::Client.new(api_user: 'foobar', api_key: 'abc123', raise_exceptions: false)
|
|
82
|
-
mail = SendGrid::Mail.new
|
|
83
|
-
|
|
84
|
-
expect {client.send(mail)}.not_to raise_error
|
|
85
|
-
end
|
|
86
|
-
end
|
|
87
|
-
end
|
|
@@ -1,151 +0,0 @@
|
|
|
1
|
-
require 'spec_helper'
|
|
2
|
-
|
|
3
|
-
describe 'SendGrid::Mail' do
|
|
4
|
-
before(:each) do
|
|
5
|
-
@mail = SendGrid::Mail.new
|
|
6
|
-
end
|
|
7
|
-
|
|
8
|
-
it 'should create an instance' do
|
|
9
|
-
expect(SendGrid::Mail.new).to be_an_instance_of(SendGrid::Mail)
|
|
10
|
-
end
|
|
11
|
-
|
|
12
|
-
describe 'add_to_name' do
|
|
13
|
-
it 'should accept a name' do
|
|
14
|
-
@mail.add_to_name('Frank Foo')
|
|
15
|
-
expect(@mail.to_name).to eq(['Frank Foo'])
|
|
16
|
-
end
|
|
17
|
-
|
|
18
|
-
it 'should accept an array of names' do
|
|
19
|
-
@mail.add_to_name(['Frank Foo', 'Bob Bar'])
|
|
20
|
-
expect(@mail.to_name).to eq(['Frank Foo', 'Bob Bar'])
|
|
21
|
-
end
|
|
22
|
-
end
|
|
23
|
-
|
|
24
|
-
describe 'add_to' do
|
|
25
|
-
it 'should accept an email' do
|
|
26
|
-
@mail.add_to('foo@example.com')
|
|
27
|
-
expect(@mail.to).to eq(['foo@example.com'])
|
|
28
|
-
|
|
29
|
-
@mail.add_to('bar@example.com')
|
|
30
|
-
expect(@mail.to).to eq(['foo@example.com', 'bar@example.com'])
|
|
31
|
-
end
|
|
32
|
-
|
|
33
|
-
it 'should accept an email and name' do
|
|
34
|
-
@mail.add_to('foo@example.com', 'Frank Foo')
|
|
35
|
-
expect(@mail.to).to eq(['foo@example.com'])
|
|
36
|
-
expect(@mail.to_name).to eq(['Frank Foo'])
|
|
37
|
-
|
|
38
|
-
@mail.add_to('bar@example.com', 'Bob Bar')
|
|
39
|
-
expect(@mail.to).to eq(['foo@example.com', 'bar@example.com'])
|
|
40
|
-
expect(@mail.to_name).to eq(['Frank Foo', 'Bob Bar'])
|
|
41
|
-
end
|
|
42
|
-
|
|
43
|
-
it 'should accept an array of emails' do
|
|
44
|
-
@mail.add_to(['foo@example.com', 'bar@example.com'])
|
|
45
|
-
expect(@mail.to).to eq(['foo@example.com', 'bar@example.com'])
|
|
46
|
-
end
|
|
47
|
-
|
|
48
|
-
it 'should accept an array of emails and names' do
|
|
49
|
-
@mail.add_to(['foo@example.com', 'bar@example.com'], ['Frank Foo', 'Bob Bar'])
|
|
50
|
-
expect(@mail.to).to eq(['foo@example.com', 'bar@example.com'])
|
|
51
|
-
expect(@mail.to_name).to eq(['Frank Foo', 'Bob Bar'])
|
|
52
|
-
end
|
|
53
|
-
end
|
|
54
|
-
|
|
55
|
-
describe 'add_cc' do
|
|
56
|
-
it 'should accept an email' do
|
|
57
|
-
@mail.add_cc('foo@example.com')
|
|
58
|
-
expect(@mail.cc).to eq(['foo@example.com'])
|
|
59
|
-
end
|
|
60
|
-
|
|
61
|
-
it 'should accept an email and a name' do
|
|
62
|
-
@mail.add_cc('foo@example.com', 'Frank Foo')
|
|
63
|
-
expect(@mail.cc).to eq(['foo@example.com'])
|
|
64
|
-
expect(@mail.cc_name).to eq(['Frank Foo'])
|
|
65
|
-
end
|
|
66
|
-
|
|
67
|
-
it 'should accept an array of emails' do
|
|
68
|
-
@mail.add_cc(['foo@example.com', 'bar@example.com'])
|
|
69
|
-
expect(@mail.cc).to eq(['foo@example.com', 'bar@example.com'])
|
|
70
|
-
end
|
|
71
|
-
|
|
72
|
-
it 'should accept an array of emails and names' do
|
|
73
|
-
@mail.add_cc(['foo@example.com', 'bar@example.com'], ['Frank Foo', 'Bob Bar'])
|
|
74
|
-
expect(@mail.cc).to eq(['foo@example.com', 'bar@example.com'])
|
|
75
|
-
expect(@mail.cc_name).to eq(['Frank Foo', 'Bob Bar'])
|
|
76
|
-
end
|
|
77
|
-
end
|
|
78
|
-
|
|
79
|
-
describe 'add_bcc' do
|
|
80
|
-
it 'should accept an email' do
|
|
81
|
-
@mail.add_bcc('foo@example.com')
|
|
82
|
-
expect(@mail.bcc).to eq(['foo@example.com'])
|
|
83
|
-
end
|
|
84
|
-
|
|
85
|
-
it 'should accept an email and a name' do
|
|
86
|
-
@mail.add_bcc('foo@example.com', 'Frank Foo')
|
|
87
|
-
expect(@mail.bcc).to eq(['foo@example.com'])
|
|
88
|
-
expect(@mail.bcc_name).to eq(['Frank Foo'])
|
|
89
|
-
end
|
|
90
|
-
|
|
91
|
-
it 'should accept an array of emails' do
|
|
92
|
-
@mail.add_bcc(['foo@example.com', 'bar@example.com'])
|
|
93
|
-
expect(@mail.bcc).to eq(['foo@example.com', 'bar@example.com'])
|
|
94
|
-
end
|
|
95
|
-
|
|
96
|
-
it 'should accept an array of emails and names' do
|
|
97
|
-
@mail.add_bcc(['foo@example.com', 'bar@example.com'], ['Frank Foo', 'Bob Bar'])
|
|
98
|
-
expect(@mail.bcc).to eq(['foo@example.com', 'bar@example.com'])
|
|
99
|
-
expect(@mail.bcc_name).to eq(['Frank Foo', 'Bob Bar'])
|
|
100
|
-
end
|
|
101
|
-
end
|
|
102
|
-
|
|
103
|
-
describe 'to' do
|
|
104
|
-
it 'should be set' do
|
|
105
|
-
@mail.to = 'foo@example.com'
|
|
106
|
-
expect(@mail.to).to eq('foo@example.com')
|
|
107
|
-
end
|
|
108
|
-
end
|
|
109
|
-
|
|
110
|
-
describe 'from_name' do
|
|
111
|
-
it 'should be set' do
|
|
112
|
-
@mail.from_name = 'Frank Foo'
|
|
113
|
-
expect(@mail.from_name).to eq('Frank Foo')
|
|
114
|
-
end
|
|
115
|
-
end
|
|
116
|
-
|
|
117
|
-
describe 'reply_to' do
|
|
118
|
-
it 'should be set' do
|
|
119
|
-
@mail.reply_to = 'foo@example.com'
|
|
120
|
-
expect(@mail.reply_to).to eq('foo@example.com')
|
|
121
|
-
end
|
|
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
|
|
151
|
-
end
|
|
@@ -1,91 +0,0 @@
|
|
|
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
|
|
@@ -1,86 +0,0 @@
|
|
|
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
|
|
@@ -1,61 +0,0 @@
|
|
|
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
|