sendgrid-api 0.0.2 → 0.0.3
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.
- data/.gitignore +4 -2
- data/.yardopts +6 -0
- data/Gemfile +2 -0
- data/Gemfile.lock +5 -1
- data/README.md +200 -12
- data/Rakefile +3 -0
- data/lib/sendgrid/api/client.rb +16 -0
- data/lib/sendgrid/api/entities/category.rb +13 -0
- data/lib/sendgrid/api/entities/email.rb +13 -0
- data/lib/sendgrid/api/entities/entity.rb +2 -2
- data/lib/sendgrid/api/entities/list.rb +30 -0
- data/lib/sendgrid/api/entities/marketing_email.rb +20 -0
- data/lib/sendgrid/api/entities/response_insert.rb +23 -0
- data/lib/sendgrid/api/entities/response_remove.rb +23 -0
- data/lib/sendgrid/api/entities/schedule.rb +13 -0
- data/lib/sendgrid/api/entities/sender_address.rb +13 -0
- data/lib/sendgrid/api/newsletter/categories.rb +74 -0
- data/lib/sendgrid/api/newsletter/emails.rb +69 -0
- data/lib/sendgrid/api/newsletter/lists.rb +64 -0
- data/lib/sendgrid/api/newsletter/marketing_emails.rb +72 -0
- data/lib/sendgrid/api/newsletter/recipients.rb +55 -0
- data/lib/sendgrid/api/newsletter/schedule.rb +70 -0
- data/lib/sendgrid/api/newsletter/sender_addresses.rb +80 -0
- data/lib/sendgrid/api/newsletter/utils.rb +34 -0
- data/lib/sendgrid/api/rest/errors/error.rb +9 -3
- data/lib/sendgrid/api/rest/resource.rb +3 -1
- data/lib/sendgrid/api/version.rb +1 -1
- data/lib/sendgrid/api/web/mail.rb +44 -0
- data/lib/sendgrid/api/web/profile.rb +3 -3
- data/lib/sendgrid/api/web/stats.rb +3 -3
- data/spec/fixtures/categories.json +11 -0
- data/spec/fixtures/emails/email.json +6 -0
- data/spec/fixtures/emails/emails.json +10 -0
- data/spec/fixtures/errors/already_exists.json +3 -0
- data/spec/fixtures/errors/bad_request.json +6 -0
- data/spec/fixtures/errors/database_error.json +3 -0
- data/spec/fixtures/errors/does_not_exist.json +3 -0
- data/spec/fixtures/{forbidden.json → errors/forbidden.json} +0 -0
- data/spec/fixtures/errors/invalid_fields.json +3 -0
- data/spec/fixtures/errors/not_scheduled.json +3 -0
- data/spec/fixtures/errors/unauthorized.json +6 -0
- data/spec/fixtures/lists/list.json +5 -0
- data/spec/fixtures/lists/lists.json +11 -0
- data/spec/fixtures/marketing_emails/marketing_email.json +19 -0
- data/spec/fixtures/marketing_emails/marketing_emails.json +10 -0
- data/spec/fixtures/recipients.json +8 -0
- data/spec/fixtures/schedule.json +3 -0
- data/spec/fixtures/sender_addresses/sender_address.json +11 -0
- data/spec/fixtures/sender_addresses/sender_addresses.json +11 -0
- data/spec/sendgrid/api/client_spec.rb +16 -0
- data/spec/sendgrid/api/entities/category_spec.rb +14 -0
- data/spec/sendgrid/api/entities/email_spec.rb +15 -0
- data/spec/sendgrid/api/entities/list_spec.rb +34 -0
- data/spec/sendgrid/api/entities/marketing_email_spec.rb +31 -0
- data/spec/sendgrid/api/entities/response_insert_spec.rb +28 -0
- data/spec/sendgrid/api/entities/response_remove_spec.rb +28 -0
- data/spec/sendgrid/api/entities/schedule_spec.rb +14 -0
- data/spec/sendgrid/api/entities/sender_address_spec.rb +21 -0
- data/spec/sendgrid/api/newsletter/categories_spec.rb +247 -0
- data/spec/sendgrid/api/newsletter/emails_spec.rb +265 -0
- data/spec/sendgrid/api/newsletter/lists_spec.rb +307 -0
- data/spec/sendgrid/api/newsletter/marketing_emails_spec.rb +306 -0
- data/spec/sendgrid/api/newsletter/recipients_spec.rb +252 -0
- data/spec/sendgrid/api/newsletter/schedule_spec.rb +263 -0
- data/spec/sendgrid/api/newsletter/sender_addresses_spec.rb +300 -0
- data/spec/sendgrid/api/rest/errors/error_spec.rb +40 -16
- data/spec/sendgrid/api/rest/resource_spec.rb +2 -0
- data/spec/sendgrid/api/web/mail_spec.rb +111 -0
- data/spec/sendgrid/api/web/profile_spec.rb +13 -29
- data/spec/sendgrid/api/web/stats_spec.rb +9 -15
- data/spec/support/helpers.rb +8 -0
- data/spec/support/mock.rb +6 -2
- data/spec/support/online.rb +114 -0
- data/spec/support/shared_examples.rb +93 -0
- metadata +96 -10
- data/spec/fixtures/unauthorized.json +0 -6
data/.gitignore
CHANGED
data/.yardopts
ADDED
data/Gemfile
CHANGED
data/Gemfile.lock
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
PATH
|
2
2
|
remote: .
|
3
3
|
specs:
|
4
|
-
sendgrid-api (0.0.
|
4
|
+
sendgrid-api (0.0.3)
|
5
5
|
faraday (~> 0.8.8)
|
6
6
|
json (~> 1.8.0)
|
7
7
|
|
@@ -25,6 +25,7 @@ GEM
|
|
25
25
|
multi_json (1.8.0)
|
26
26
|
multipart-post (1.2.0)
|
27
27
|
rake (10.1.0)
|
28
|
+
redcarpet (3.0.0)
|
28
29
|
rest-client (1.6.7)
|
29
30
|
mime-types (>= 1.16)
|
30
31
|
rspec (2.14.1)
|
@@ -47,6 +48,7 @@ GEM
|
|
47
48
|
webmock (1.15.0)
|
48
49
|
addressable (>= 2.2.7)
|
49
50
|
crack (>= 0.3.2)
|
51
|
+
yard (0.8.7.2)
|
50
52
|
|
51
53
|
PLATFORMS
|
52
54
|
ruby
|
@@ -55,7 +57,9 @@ DEPENDENCIES
|
|
55
57
|
bundler (~> 1.3)
|
56
58
|
coveralls (~> 0.7.0)
|
57
59
|
rake
|
60
|
+
redcarpet
|
58
61
|
rspec (~> 2.14.1)
|
59
62
|
sendgrid-api!
|
60
63
|
simplecov (~> 0.7.1)
|
61
64
|
webmock (~> 1.15.0)
|
65
|
+
yard
|
data/README.md
CHANGED
@@ -12,8 +12,6 @@ A Ruby interface to the SendGrid API.
|
|
12
12
|
|
13
13
|
## API Coverage
|
14
14
|
|
15
|
-
The SendGrid API is being covered on demand. The next APIs to be supported are the complete [Marketing Email API](http://sendgrid.com/docs/API_Reference/Marketing_Emails_API/index.html) and [Mail](http://sendgrid.com/docs/API_Reference/Web_API/mail.html) (Web API).
|
16
|
-
|
17
15
|
Check which SendGrid APIs are currently being covered by this gem:
|
18
16
|
|
19
17
|
[https://github.com/renatosnrg/sendgrid-api/wiki/SendGrid-API-Coverage][coverage]
|
@@ -24,15 +22,21 @@ Check which SendGrid APIs are currently being covered by this gem:
|
|
24
22
|
|
25
23
|
Add this line to your application's Gemfile:
|
26
24
|
|
27
|
-
|
25
|
+
```ruby
|
26
|
+
gem 'sendgrid-api'
|
27
|
+
```
|
28
28
|
|
29
29
|
And then execute:
|
30
30
|
|
31
|
-
|
31
|
+
```bash
|
32
|
+
$ bundle
|
33
|
+
```
|
32
34
|
|
33
35
|
Or install it yourself as:
|
34
36
|
|
35
|
-
|
37
|
+
```bash
|
38
|
+
$ gem install sendgrid-api
|
39
|
+
```
|
36
40
|
|
37
41
|
## Configuration
|
38
42
|
|
@@ -40,28 +44,212 @@ Or install it yourself as:
|
|
40
44
|
client = Sendgrid::API::Client.new('YOUR_USER', 'YOUR_KEY')
|
41
45
|
```
|
42
46
|
|
43
|
-
##
|
47
|
+
## Web API
|
44
48
|
|
45
|
-
|
49
|
+
### Profile
|
46
50
|
|
47
51
|
```ruby
|
52
|
+
# create a profile object
|
53
|
+
profile = Sendgrid::API::Entities::Profile.new(:first_name => 'Your first name',
|
54
|
+
:last_name => 'Your last name')
|
55
|
+
# get your profile
|
48
56
|
profile = client.profile.get
|
57
|
+
# modify your profile
|
58
|
+
response = client.profile.set(profile)
|
49
59
|
```
|
50
60
|
|
51
|
-
|
61
|
+
### Mail
|
52
62
|
|
53
63
|
```ruby
|
54
|
-
|
55
|
-
|
56
|
-
|
64
|
+
# send basic text email
|
65
|
+
response = client.mail.send(:to => 'johndoe@example.com',
|
66
|
+
:from => 'brian@example.com',
|
67
|
+
:subject => 'test using sendgrid api',
|
68
|
+
:text => 'this is a test')
|
69
|
+
# set SMTP API headers
|
70
|
+
x_smtpapi = {:category => ['sendgrid-api test']}
|
71
|
+
response = client.mail.send(:to => 'johndoe@example.com',
|
72
|
+
:from => 'brian@example.com',
|
73
|
+
:subject => 'test using sendgrid api',
|
74
|
+
:text => 'this is a test',
|
75
|
+
:x_smtpapi => x_smtpapi)
|
76
|
+
# send files attached
|
77
|
+
text = StringIO.new("This is my file content")
|
78
|
+
file = Faraday::UploadIO.new(text, 'plain/text', 'sample.txt')
|
79
|
+
response = client.mail.send(:to => 'johndoe@example.com',
|
80
|
+
:from => 'brian@example.com',
|
81
|
+
:subject => 'test using sendgrid api',
|
82
|
+
:text => 'this is a test',
|
83
|
+
:files => {'sample.txt' => file})
|
57
84
|
```
|
58
85
|
|
59
|
-
|
86
|
+
### Statistics
|
60
87
|
|
61
88
|
```ruby
|
89
|
+
# get advanced statistics
|
62
90
|
stats = client.stats.advanced(:start_date => '2013-01-01', :data_type => 'global')
|
63
91
|
```
|
64
92
|
|
93
|
+
## Marketing Email API
|
94
|
+
|
95
|
+
### Lists
|
96
|
+
|
97
|
+
```ruby
|
98
|
+
# create a list object
|
99
|
+
list = Sendgrid::API::Entities::List.new(:list => 'sendgrid-api list test')
|
100
|
+
# add a new list
|
101
|
+
response = client.lists.add(list)
|
102
|
+
# edit an existing list
|
103
|
+
response = client.lists.edit(list, 'new name')
|
104
|
+
# get an existing list
|
105
|
+
selected_list = client.lists.get(list)
|
106
|
+
# get all lists
|
107
|
+
all_lists = client.lists.get
|
108
|
+
# delete a list
|
109
|
+
response = client.lists.delete(list)
|
110
|
+
```
|
111
|
+
|
112
|
+
### Emails
|
113
|
+
|
114
|
+
```ruby
|
115
|
+
# create email objects
|
116
|
+
email1 = Sendgrid::API::Entities::Email.new(
|
117
|
+
:email => 'johndoe@example.com',
|
118
|
+
:name => 'John Doe'
|
119
|
+
)
|
120
|
+
email2 = Sendgrid::API::Entities::Email.new(
|
121
|
+
:email => 'brian@example.com',
|
122
|
+
:name => 'Brian'
|
123
|
+
)
|
124
|
+
emails = [email1, email2]
|
125
|
+
listname = 'sendgrid-api list test'
|
126
|
+
# add emails to a list
|
127
|
+
response = client.emails.add(listname, emails)
|
128
|
+
# get all emails from a list
|
129
|
+
all_emails = client.emails.get(listname)
|
130
|
+
# get a specific email from a list
|
131
|
+
selected_emails = client.emails.get(listname, email1)
|
132
|
+
# delete an email from a list
|
133
|
+
response = client.emails.delete(listname, email1)
|
134
|
+
```
|
135
|
+
|
136
|
+
### Sender Adresses
|
137
|
+
|
138
|
+
```ruby
|
139
|
+
# create sender address object
|
140
|
+
address = Sendgrid::API::Entities::SenderAddress.new(
|
141
|
+
:identity => 'sendgrid-api sender address test',
|
142
|
+
:name => 'Sendgrid',
|
143
|
+
:email => 'contact@sendgrid.com',
|
144
|
+
:address => '1065 N Pacificenter Drive, Suite 425',
|
145
|
+
:city => 'Anaheim',
|
146
|
+
:state => 'CA',
|
147
|
+
:zip => '92806',
|
148
|
+
:country => 'US'
|
149
|
+
)
|
150
|
+
# add a sender address
|
151
|
+
response = client.sender_addresses.add(address)
|
152
|
+
# edit a sender address
|
153
|
+
address.city = 'Pleasanton'
|
154
|
+
response = client.sender_addresses.edit(address, 'new identity')
|
155
|
+
# get all sender addresses
|
156
|
+
addresses = client.sender_addresses.list
|
157
|
+
# get a sender address
|
158
|
+
address = client.sender_addresses.get(address)
|
159
|
+
# delete a sender address
|
160
|
+
response = client.sender_addresses.delete(address)
|
161
|
+
```
|
162
|
+
|
163
|
+
### Marketing Emails
|
164
|
+
|
165
|
+
```ruby
|
166
|
+
# create marketing email object
|
167
|
+
marketing_email = Sendgrid::API::Entities::MarketingEmail.new(
|
168
|
+
:identity => 'sendgrid-api sender address test',
|
169
|
+
:name => 'sendgrid-api marketing email test',
|
170
|
+
:subject => 'My Marketing Email Test',
|
171
|
+
:text => 'My text',
|
172
|
+
:html => 'My HTML'
|
173
|
+
)
|
174
|
+
# add a marketing email
|
175
|
+
response = client.marketing_emails.add(marketing_email)
|
176
|
+
# edit a marketing email
|
177
|
+
marketing_email.html = 'My new HTML'
|
178
|
+
response = client.marketing_emails.edit(marketing_email, 'new name')
|
179
|
+
# get a marketing email
|
180
|
+
newsletter = client.marketing_emails.get(marketing_email)
|
181
|
+
# get all marketing emails
|
182
|
+
newsletters = client.marketing_emails.list
|
183
|
+
# delete a marketing email
|
184
|
+
response = client.marketing_emails.delete(marketing_email)
|
185
|
+
```
|
186
|
+
|
187
|
+
### Categories
|
188
|
+
|
189
|
+
```ruby
|
190
|
+
# create category object
|
191
|
+
category = Sendgrid::API::Entities::Category.new(:category => 'sendgrid-api test')
|
192
|
+
marketing_email = 'sendgrid-api marketing email test'
|
193
|
+
# add category
|
194
|
+
response = client.categories.create(category)
|
195
|
+
# add category to a marketing email
|
196
|
+
response = client.categories.add(marketing_email, category)
|
197
|
+
# remove category from a marketing email
|
198
|
+
response = client.categories.remove(marketing_email, category)
|
199
|
+
# get all categories
|
200
|
+
categories = client.categories.list
|
201
|
+
```
|
202
|
+
|
203
|
+
### Recipients
|
204
|
+
|
205
|
+
```ruby
|
206
|
+
listname = 'sendgrid-api list test'
|
207
|
+
marketing_email = 'sendgrid-api marketing email test'
|
208
|
+
# assign a recipient list to a marketing email
|
209
|
+
response = client.recipients.add(listname, marketing_email)
|
210
|
+
# get all lists assigned to a marketing email
|
211
|
+
lists = client.recipients.get(marketing_email)
|
212
|
+
# delete an assigned list from a marketing email
|
213
|
+
response = client.recipients.delete(listname, marketing_email)
|
214
|
+
```
|
215
|
+
|
216
|
+
### Schedule
|
217
|
+
|
218
|
+
```ruby
|
219
|
+
marketing_email = 'sendgrid-api marketing email test'
|
220
|
+
# schedule a delivery time for a marketing email
|
221
|
+
response = client.schedule.add(marketing_email) # imediately
|
222
|
+
response = client.schedule.add(marketing_email, :after => 20) # 20 minutes from now
|
223
|
+
response = client.schedule.add(marketing_email, :at => (Time.now + 10*60)) # 10 minutes from now
|
224
|
+
# retrieve the scheduled delivery time for a marketing email
|
225
|
+
schedule = client.schedule.get(marketing_email)
|
226
|
+
# cancel a scheduled delivery time for a marketing email
|
227
|
+
response = client.schedule.delete(marketing_email)
|
228
|
+
```
|
229
|
+
|
230
|
+
## Tests
|
231
|
+
|
232
|
+
This gem has offline and online tests written using RSpec. Offline tests use Webmock to stub HTTP requests, while online tests performs the requests to Sendgrid to ensure all the calls and responses are correctly.
|
233
|
+
|
234
|
+
The online tests are written in a way that they don't change the state of the objects already existing on SendGrid account. It means if something is added, it's removed at the end of the test. If something is changed, it's changed back after the test finishes. If it's necessary to remove something, it's added before the test starts.
|
235
|
+
|
236
|
+
Use the SENDGRID_USER and SENDGRID_KEY environment variables to authenticate the online tests.
|
237
|
+
|
238
|
+
To run all tests (both offline and online):
|
239
|
+
```bash
|
240
|
+
$ ALL=1 SENDGRID_USER=your_user SENDGRID_KEY=your_key bundle exec rake spec
|
241
|
+
```
|
242
|
+
|
243
|
+
To run only online tests:
|
244
|
+
```bash
|
245
|
+
$ SENDGRID_USER=your_user SENDGRID_KEY=your_key bundle exec rake spec[online]
|
246
|
+
```
|
247
|
+
|
248
|
+
To run only offline tests (default):
|
249
|
+
```bash
|
250
|
+
$ bundle exec rake spec
|
251
|
+
```
|
252
|
+
|
65
253
|
## Contributing
|
66
254
|
|
67
255
|
If you want to contribute to cover more APIs or improve something already implemented, follow these steps:
|
data/Rakefile
CHANGED
@@ -1,9 +1,12 @@
|
|
1
1
|
require "bundler/gem_tasks"
|
2
2
|
require "rspec/core/rake_task"
|
3
|
+
require "yard"
|
3
4
|
|
4
5
|
RSpec::Core::RakeTask.new(:spec, :tag) do |t, task_args|
|
5
6
|
t.rspec_opts = "--tag #{task_args[:tag]}" if task_args[:tag]
|
6
7
|
end
|
7
8
|
|
9
|
+
YARD::Rake::YardocTask.new
|
10
|
+
|
8
11
|
task :default => :spec
|
9
12
|
task :test => :spec
|
data/lib/sendgrid/api/client.rb
CHANGED
@@ -1,6 +1,14 @@
|
|
1
1
|
require 'sendgrid/api/rest/resource'
|
2
2
|
require 'sendgrid/api/web/profile'
|
3
3
|
require 'sendgrid/api/web/stats'
|
4
|
+
require 'sendgrid/api/web/mail'
|
5
|
+
require 'sendgrid/api/newsletter/lists'
|
6
|
+
require 'sendgrid/api/newsletter/emails'
|
7
|
+
require 'sendgrid/api/newsletter/sender_addresses'
|
8
|
+
require 'sendgrid/api/newsletter/categories'
|
9
|
+
require 'sendgrid/api/newsletter/marketing_emails'
|
10
|
+
require 'sendgrid/api/newsletter/recipients'
|
11
|
+
require 'sendgrid/api/newsletter/schedule'
|
4
12
|
|
5
13
|
module Sendgrid
|
6
14
|
module API
|
@@ -8,6 +16,14 @@ module Sendgrid
|
|
8
16
|
|
9
17
|
include Web::Profile
|
10
18
|
include Web::Stats
|
19
|
+
include Web::Mail
|
20
|
+
include Newsletter::Lists
|
21
|
+
include Newsletter::Emails
|
22
|
+
include Newsletter::SenderAddresses
|
23
|
+
include Newsletter::Categories
|
24
|
+
include Newsletter::MarketingEmails
|
25
|
+
include Newsletter::Recipients
|
26
|
+
include Newsletter::Schedule
|
11
27
|
|
12
28
|
attr_reader :user, :key
|
13
29
|
|
@@ -0,0 +1,30 @@
|
|
1
|
+
require 'sendgrid/api/entities/entity'
|
2
|
+
|
3
|
+
module Sendgrid
|
4
|
+
module API
|
5
|
+
module Entities
|
6
|
+
class List < Entity
|
7
|
+
|
8
|
+
attribute :list
|
9
|
+
|
10
|
+
class << self
|
11
|
+
|
12
|
+
# Convert the object to an Entities::List.
|
13
|
+
#
|
14
|
+
# @param object [String, Entities::List] A list name or Entities::List object
|
15
|
+
# @return [Entities::List] An Entities::List object
|
16
|
+
def from_object(object)
|
17
|
+
case object
|
18
|
+
when ::String
|
19
|
+
new(:list => object)
|
20
|
+
when self
|
21
|
+
object
|
22
|
+
end
|
23
|
+
end
|
24
|
+
|
25
|
+
end
|
26
|
+
|
27
|
+
end
|
28
|
+
end
|
29
|
+
end
|
30
|
+
end
|
@@ -0,0 +1,20 @@
|
|
1
|
+
require 'sendgrid/api/entities/entity'
|
2
|
+
|
3
|
+
module Sendgrid
|
4
|
+
module API
|
5
|
+
module Entities
|
6
|
+
class MarketingEmail < Entity
|
7
|
+
|
8
|
+
# required attributes to create
|
9
|
+
attribute :identity, :name, :subject, :text, :html
|
10
|
+
|
11
|
+
# other attributes
|
12
|
+
attribute :can_edit, :content_preview, :date_schedule,
|
13
|
+
:is_deleted, :is_split, :is_winner, :newsletter_id,
|
14
|
+
:nl_type, :timezone_id, :total_recipients,
|
15
|
+
:type, :winner_sending_time
|
16
|
+
|
17
|
+
end
|
18
|
+
end
|
19
|
+
end
|
20
|
+
end
|