sendgrid-ruby 1.1.6 → 6.6.2

Sign up to get free protection for your applications and to get access to all the features.
Files changed (160) hide show
  1. checksums.yaml +5 -5
  2. data/.env_sample +1 -3
  3. data/.github/ISSUE_TEMPLATE/config.yml +10 -0
  4. data/.github/workflows/test-and-deploy.yml +120 -0
  5. data/.gitignore +3 -0
  6. data/.rubocop.yml +5 -27
  7. data/.rubocop_todo.yml +127 -0
  8. data/CHANGELOG.md +417 -1
  9. data/CODE_OF_CONDUCT.md +73 -0
  10. data/CONTRIBUTING.md +202 -0
  11. data/Dockerfile +14 -0
  12. data/FIRST_TIMERS.md +79 -0
  13. data/Gemfile +4 -4
  14. data/ISSUE_TEMPLATE.md +30 -0
  15. data/LICENSE +21 -0
  16. data/Makefile +14 -0
  17. data/PULL_REQUEST_TEMPLATE.md +31 -0
  18. data/README.md +153 -229
  19. data/Rakefile +9 -3
  20. data/TROUBLESHOOTING.md +151 -0
  21. data/UPGRADE.md +5 -0
  22. data/USAGE.md +5144 -0
  23. data/config.ru +4 -0
  24. data/examples/accesssettings/accesssettings.rb +80 -0
  25. data/examples/alerts/alerts.rb +59 -0
  26. data/examples/apikeys/apikeys.rb +81 -0
  27. data/examples/asm/asm.rb +170 -0
  28. data/examples/browsers/browsers.rb +13 -0
  29. data/examples/campaigns/campaigns.rb +150 -0
  30. data/examples/categories/categories.rb +33 -0
  31. data/examples/clients/clients.rb +24 -0
  32. data/examples/contactdb/contactdb.rb +392 -0
  33. data/examples/devices/devices.rb +13 -0
  34. data/examples/emailactivity/emailactivity.rb +52 -0
  35. data/examples/geo/geo.rb +13 -0
  36. data/examples/helpers/eventwebhook/example.rb +16 -0
  37. data/examples/helpers/mail/example.rb +158 -0
  38. data/examples/helpers/settings/example.rb +23 -0
  39. data/examples/helpers/stats/example.rb +42 -0
  40. data/examples/ips/ips.rb +164 -0
  41. data/examples/mail/mail.rb +170 -0
  42. data/examples/mailboxproviders/mailboxproviders.rb +13 -0
  43. data/examples/mailsettings/mailsettings.rb +216 -0
  44. data/examples/partnersettings/partnersettings.rb +36 -0
  45. data/examples/scopes/scopes.rb +59 -0
  46. data/examples/senderauthentication/senderauthentication.rb +307 -0
  47. data/examples/senders/senders.rb +95 -0
  48. data/examples/stats/stats.rb +13 -0
  49. data/examples/subusers/subusers.rb +166 -0
  50. data/examples/suppression/suppression.rb +198 -0
  51. data/examples/templates/templates.rb +127 -0
  52. data/examples/trackingsettings/trackingsettings.rb +107 -0
  53. data/examples/user/user.rb +290 -0
  54. data/gemfiles/Sinatra_1.gemfile +6 -0
  55. data/gemfiles/Sinatra_2.gemfile +6 -0
  56. data/lib/rack/sendgrid_webhook_verification.rb +55 -0
  57. data/lib/sendgrid/base_interface.rb +40 -0
  58. data/lib/sendgrid/helpers/eventwebhook/eventwebhook.rb +50 -0
  59. data/lib/sendgrid/helpers/inbound/README.md +98 -0
  60. data/lib/sendgrid/helpers/inbound/app.rb +32 -0
  61. data/lib/sendgrid/helpers/inbound/config.yml +26 -0
  62. data/lib/sendgrid/helpers/inbound/public/index.html +10 -0
  63. data/lib/sendgrid/helpers/inbound/sample_data/default_data.txt +58 -0
  64. data/lib/sendgrid/helpers/inbound/sample_data/raw_data.txt +57 -0
  65. data/lib/sendgrid/helpers/inbound/sample_data/raw_data_with_attachments.txt +298 -0
  66. data/lib/sendgrid/helpers/inbound/send.rb +26 -0
  67. data/lib/sendgrid/helpers/ip_management/ip_management.rb +17 -0
  68. data/lib/sendgrid/helpers/mail/README.md +14 -0
  69. data/lib/sendgrid/helpers/mail/asm.rb +19 -0
  70. data/lib/sendgrid/helpers/mail/attachment.rb +55 -0
  71. data/lib/sendgrid/helpers/mail/bcc_settings.rb +19 -0
  72. data/lib/sendgrid/helpers/mail/bypass_list_management.rb +31 -0
  73. data/lib/sendgrid/helpers/mail/category.rb +18 -0
  74. data/lib/sendgrid/helpers/mail/click_tracking.rb +19 -0
  75. data/lib/sendgrid/helpers/mail/content.rb +19 -0
  76. data/lib/sendgrid/helpers/mail/custom_arg.rb +18 -0
  77. data/lib/sendgrid/helpers/mail/email.rb +32 -0
  78. data/lib/sendgrid/helpers/mail/footer.rb +21 -0
  79. data/lib/sendgrid/helpers/mail/ganalytics.rb +28 -0
  80. data/lib/sendgrid/helpers/mail/header.rb +18 -0
  81. data/lib/sendgrid/helpers/mail/mail.rb +122 -0
  82. data/lib/sendgrid/helpers/mail/mail_settings.rb +45 -0
  83. data/lib/sendgrid/helpers/mail/open_tracking.rb +19 -0
  84. data/lib/sendgrid/helpers/mail/personalization.rb +93 -0
  85. data/lib/sendgrid/helpers/mail/section.rb +18 -0
  86. data/lib/sendgrid/helpers/mail/spam_check.rb +21 -0
  87. data/lib/sendgrid/helpers/mail/subscription_tracking.rb +23 -0
  88. data/lib/sendgrid/helpers/mail/substitution.rb +18 -0
  89. data/lib/sendgrid/helpers/mail/tracking_settings.rb +39 -0
  90. data/lib/sendgrid/helpers/permissions/scope.rb +28 -0
  91. data/lib/sendgrid/helpers/permissions/scopes.yml +309 -0
  92. data/lib/sendgrid/helpers/settings/README.md +14 -0
  93. data/lib/sendgrid/helpers/settings/mail_settings_dto.rb +13 -0
  94. data/lib/sendgrid/helpers/settings/partner_settings_dto.rb +13 -0
  95. data/lib/sendgrid/helpers/settings/settings.rb +28 -0
  96. data/lib/sendgrid/helpers/settings/tracking_settings_dto.rb +22 -0
  97. data/lib/sendgrid/helpers/settings/user_settings_dto.rb +13 -0
  98. data/lib/sendgrid/helpers/stats/email_stats.rb +46 -0
  99. data/lib/sendgrid/helpers/stats/metrics.rb +33 -0
  100. data/lib/sendgrid/helpers/stats/stats_response.rb +29 -0
  101. data/lib/sendgrid/sendgrid.rb +21 -0
  102. data/lib/sendgrid/twilio_email.rb +21 -0
  103. data/lib/sendgrid/version.rb +1 -1
  104. data/lib/sendgrid-ruby.rb +32 -7
  105. data/mail_helper_v3.md +390 -0
  106. data/sendgrid-ruby.gemspec +16 -20
  107. data/spec/fixtures/event_webhook.rb +22 -0
  108. data/spec/rack/sendgrid_webhook_verification_spec.rb +142 -0
  109. data/spec/sendgrid/helpers/eventwebhook/eventwebhook_spec.rb +105 -0
  110. data/spec/sendgrid/helpers/ip_management/ip_management_spec.rb +12 -0
  111. data/spec/sendgrid/helpers/settings/mail_settings_dto_spec.rb +32 -0
  112. data/spec/sendgrid/helpers/settings/partner_settings_dto_spec.rb +24 -0
  113. data/spec/sendgrid/helpers/settings/settings_spec.rb +25 -0
  114. data/spec/sendgrid/helpers/settings/tracking_settings_dto_spec.rb +27 -0
  115. data/spec/sendgrid/helpers/settings/user_settings_dto_spec.rb +24 -0
  116. data/spec/sendgrid/helpers/stats/email_stats_spec.rb +111 -0
  117. data/spec/sendgrid/helpers/stats/metrics_spec.rb +45 -0
  118. data/spec/sendgrid/helpers/stats/stats_response_spec.rb +75 -0
  119. data/spec/sendgrid/sendgrid_spec.rb +11 -0
  120. data/spec/sendgrid/twilio_email_spec.rb +11 -0
  121. data/spec/spec_helper.rb +12 -1
  122. data/static/img/github-fork.png +0 -0
  123. data/static/img/github-sign-up.png +0 -0
  124. data/test/sendgrid/helpers/mail/test_attachment.rb +33 -0
  125. data/test/sendgrid/helpers/mail/test_category.rb +25 -0
  126. data/test/sendgrid/helpers/mail/test_email.rb +41 -0
  127. data/test/sendgrid/helpers/mail/test_mail.rb +260 -0
  128. data/test/sendgrid/helpers/mail/test_personalizations.rb +214 -0
  129. data/test/sendgrid/permissions/test_scopes.rb +36 -0
  130. data/test/sendgrid/test_sendgrid-ruby.rb +2729 -0
  131. data/twilio_sendgrid_logo.png +0 -0
  132. data/use-cases/README.md +17 -0
  133. data/use-cases/domain-authentication.md +5 -0
  134. data/use-cases/email-statistics.md +52 -0
  135. data/use-cases/legacy-templates.md +98 -0
  136. data/use-cases/personalizations.md +34 -0
  137. data/use-cases/sms.md +39 -0
  138. data/use-cases/transactional-templates.md +111 -0
  139. data/use-cases/twilio-email.md +13 -0
  140. data/use-cases/twilio-setup.md +54 -0
  141. metadata +187 -112
  142. data/.rspec +0 -2
  143. data/.travis.yml +0 -20
  144. data/FETCH_HEAD +0 -0
  145. data/Guardfile +0 -10
  146. data/LICENSE.txt +0 -22
  147. data/example.rb +0 -41
  148. data/lib/sendgrid/client.rb +0 -62
  149. data/lib/sendgrid/exceptions.rb +0 -7
  150. data/lib/sendgrid/mail.rb +0 -182
  151. data/lib/sendgrid/recipient.rb +0 -29
  152. data/lib/sendgrid/response.rb +0 -14
  153. data/lib/sendgrid/template.rb +0 -26
  154. data/lib/sendgrid/template_mailer.rb +0 -59
  155. data/spec/lib/sendgrid/client_spec.rb +0 -87
  156. data/spec/lib/sendgrid/mail_spec.rb +0 -151
  157. data/spec/lib/sendgrid/recipient_spec.rb +0 -91
  158. data/spec/lib/sendgrid/template_mailer_spec.rb +0 -86
  159. data/spec/lib/sendgrid/template_spec.rb +0 -61
  160. data/spec/lib/sendgrid_spec.rb +0 -7
data/lib/sendgrid/mail.rb DELETED
@@ -1,182 +0,0 @@
1
- require 'json'
2
- require 'smtpapi'
3
- require 'mimemagic'
4
-
5
- module SendGrid
6
- class Mail
7
- attr_accessor :to, :to_name, :from, :from_name, :subject, :text, :html, :cc, :cc_name,
8
- :bcc, :bcc_name, :reply_to, :date, :smtpapi, :attachments, :content, :template
9
-
10
- def initialize(params = {})
11
- params.each do |k, v|
12
- send(:"#{k}=", v) unless v.nil?
13
- end
14
-
15
- yield self if block_given?
16
- end
17
-
18
- def add_to(email, name = nil)
19
- if email.is_a?(Array)
20
- to.concat(email)
21
- else
22
- to << email
23
- end
24
- add_to_name(name) if name
25
- end
26
-
27
- def to
28
- @to ||= []
29
- end
30
-
31
- def to_name
32
- @to_name ||= []
33
- end
34
-
35
- def add_to_name(name)
36
- if name.is_a?(Array)
37
- to_name.concat(name)
38
- else
39
- to_name << name
40
- end
41
- end
42
-
43
- def cc
44
- @cc ||= []
45
- end
46
-
47
- def cc_name
48
- @cc_name ||= []
49
- end
50
-
51
- def add_cc(email, name = nil)
52
- if email.is_a?(Array)
53
- cc.concat(email)
54
- else
55
- cc << email
56
- end
57
- add_cc_name(name) if name
58
- end
59
-
60
- def add_cc_name(name)
61
- if name.is_a?(Array)
62
- cc_name.concat(name)
63
- else
64
- cc_name << name
65
- end
66
- end
67
-
68
- def bcc
69
- @bcc ||= []
70
- end
71
-
72
- def bcc_name
73
- @bcc_name ||= []
74
- end
75
-
76
- def add_bcc(email, name = nil)
77
- if email.is_a?(Array)
78
- bcc.concat(email)
79
- else
80
- bcc << email
81
- end
82
- add_bcc_name(name) if name
83
- end
84
-
85
- def add_bcc_name(name)
86
- if name.is_a?(Array)
87
- bcc_name.concat(name)
88
- else
89
- bcc_name << name
90
- end
91
- end
92
-
93
- def add_attachment(path, name = nil)
94
- mime_type = MimeMagic.by_path(path)
95
- file = Faraday::UploadIO.new(path, mime_type)
96
- name ||= File.basename(file)
97
- attachments << {file: file, name: name}
98
- end
99
-
100
- def headers
101
- @headers ||= {}
102
- end
103
-
104
- def attachments
105
- @attachments ||= []
106
- end
107
-
108
- def contents
109
- @contents ||= []
110
- end
111
-
112
- def add_content(path, cid)
113
- mime_type = MimeMagic.by_path(path)
114
- file = Faraday::UploadIO.new(path, mime_type)
115
- name ||= File.basename(file)
116
- contents << {file: file, cid: cid, name: name}
117
- end
118
-
119
- def smtpapi
120
- @smtpapi ||= Smtpapi::Header.new
121
- end
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
-
131
- # rubocop:disable Style/HashSyntax
132
- def to_h
133
- payload = {
134
- :from => from,
135
- :fromname => from_name,
136
- :subject => subject,
137
- :to => to,
138
- :toname => to_name,
139
- :date => date,
140
- :replyto => reply_to,
141
- :cc => cc,
142
- :ccname => cc_name,
143
- :bcc => bcc,
144
- :bccname => bcc_name,
145
- :text => text,
146
- :html => html,
147
- :'x-smtpapi' => smtpapi_json,
148
- :content => ({":default"=>"0"} unless contents.empty?),
149
- :files => ({":default"=>"0"} unless attachments.empty? and contents.empty?)
150
- # If I don't define a default value, I get a Nil error when
151
- # in attachments.each do |file|
152
- #:files => ({} unless attachments.empty?)
153
- }.reject {|_, v| v.nil? || v.empty?}
154
-
155
- payload.delete(:'x-smtpapi') if payload[:'x-smtpapi'] == '{}'
156
-
157
- payload[:to] = payload[:from] if payload[:to].nil? and not smtpapi.to.empty?
158
-
159
- unless attachments.empty?
160
- attachments.each do |file|
161
- payload[:files][file[:name]] = file[:file]
162
- end
163
- if payload[:files].has_key?(":default")
164
- payload[:files].delete(":default")
165
- end
166
- end
167
-
168
- unless contents.empty?
169
- contents.each do |content|
170
- payload[:content][content[:name]] = content[:cid]
171
- payload[:files][content[:name]] = content[:file]
172
- end
173
- if payload[:content].has_key?(":default")
174
- payload[:content].delete(":default")
175
- end
176
- end
177
-
178
- payload
179
- end
180
- # rubocop:enable Style/HashSyntax
181
- end
182
- end
@@ -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
@@ -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
@@ -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