sendgrid-ruby 1.1.6 → 3.0.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (62) hide show
  1. checksums.yaml +4 -4
  2. data/.github/ISSUE_TEMPLATE +17 -0
  3. data/.travis.yml +1 -6
  4. data/CHANGELOG.md +6 -0
  5. data/CONTRIBUTING.md +202 -0
  6. data/Gemfile +3 -5
  7. data/LICENSE.txt +1 -1
  8. data/README.md +74 -248
  9. data/Rakefile +8 -6
  10. data/USAGE.md +4645 -0
  11. data/examples/accesssettings/accesssettings.rb +89 -0
  12. data/examples/apikeys/apikeys.rb +95 -0
  13. data/examples/asm/asm.rb +169 -0
  14. data/examples/browsers/browsers.rb +29 -0
  15. data/examples/campaigns/campaigns.rb +166 -0
  16. data/examples/categories/categories.rb +49 -0
  17. data/examples/clients/clients.rb +40 -0
  18. data/examples/contactdb/contactdb.rb +398 -0
  19. data/examples/devices/devices.rb +29 -0
  20. data/examples/geo/geo.rb +29 -0
  21. data/examples/helpers/mail/example.rb +130 -0
  22. data/examples/ips/ips.rb +167 -0
  23. data/examples/mail/mail.rb +191 -0
  24. data/examples/mailboxproviders/mailboxproviders.rb +29 -0
  25. data/examples/mailsettings/mailsettings.rb +232 -0
  26. data/examples/partnersettings/partnersettings.rb +52 -0
  27. data/examples/scopes/scopes.rb +28 -0
  28. data/examples/stats/stats.rb +29 -0
  29. data/examples/subusers/subusers.rb +182 -0
  30. data/examples/suppression/suppression.rb +186 -0
  31. data/examples/templates/templates.rb +142 -0
  32. data/examples/trackingsettings/trackingsettings.rb +123 -0
  33. data/examples/user/user.rb +256 -0
  34. data/examples/whitelabel/whitelabel.rb +323 -0
  35. data/lib/helpers/mail/README.md +14 -0
  36. data/lib/helpers/mail/mail.rb +999 -0
  37. data/lib/sendgrid-ruby.rb +33 -8
  38. data/sendgrid-ruby.gemspec +5 -17
  39. data/test/helpers/mail/test_mail.rb +122 -0
  40. data/test/test_sendgrid-ruby.rb +2016 -0
  41. metadata +42 -190
  42. data/.env_sample +0 -3
  43. data/.rspec +0 -2
  44. data/.rubocop.yml +0 -30
  45. data/FETCH_HEAD +0 -0
  46. data/Guardfile +0 -10
  47. data/example.rb +0 -41
  48. data/lib/sendgrid/client.rb +0 -62
  49. data/lib/sendgrid/exceptions.rb +0 -7
  50. data/lib/sendgrid/mail.rb +0 -182
  51. data/lib/sendgrid/recipient.rb +0 -29
  52. data/lib/sendgrid/response.rb +0 -14
  53. data/lib/sendgrid/template.rb +0 -26
  54. data/lib/sendgrid/template_mailer.rb +0 -59
  55. data/lib/sendgrid/version.rb +0 -3
  56. data/spec/lib/sendgrid/client_spec.rb +0 -87
  57. data/spec/lib/sendgrid/mail_spec.rb +0 -151
  58. data/spec/lib/sendgrid/recipient_spec.rb +0 -91
  59. data/spec/lib/sendgrid/template_mailer_spec.rb +0 -86
  60. data/spec/lib/sendgrid/template_spec.rb +0 -61
  61. data/spec/lib/sendgrid_spec.rb +0 -7
  62. data/spec/spec_helper.rb +0 -1
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,3 +0,0 @@
1
- module SendGrid
2
- VERSION = '1.1.6'
3
- 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