sendgrid_toolkit 0.1.1 → 1.0.0
Sign up to get free protection for your applications and to get access to all the features.
- data/.gitignore +2 -1
- data/README.md +278 -0
- data/VERSION +1 -1
- data/lib/sendgrid_toolkit.rb +4 -0
- data/lib/sendgrid_toolkit/bounces.rb +7 -0
- data/lib/sendgrid_toolkit/common.rb +31 -0
- data/lib/sendgrid_toolkit/invalid_emails.rb +7 -0
- data/lib/sendgrid_toolkit/sendgrid_error.rb +1 -0
- data/lib/sendgrid_toolkit/spam_reports.rb +7 -0
- data/sendgrid_toolkit.gemspec +18 -6
- data/spec/lib/sendgrid_toolkit/bounces_spec.rb +46 -0
- data/spec/lib/sendgrid_toolkit/common_spec.rb +16 -0
- data/spec/lib/sendgrid_toolkit/invalid_emails_spec.rb +50 -0
- data/spec/lib/sendgrid_toolkit/spam_reports_spec.rb +45 -0
- metadata +24 -7
- data/README.rdoc +0 -103
data/.gitignore
CHANGED
data/README.md
ADDED
@@ -0,0 +1,278 @@
|
|
1
|
+
SendgridToolkit
|
2
|
+
===============
|
3
|
+
|
4
|
+
SendgridToolkit is a Ruby wrapper for the Sendgrid Web API
|
5
|
+
|
6
|
+
Supported Modules
|
7
|
+
-----------------
|
8
|
+
|
9
|
+
SendgridToolkit provides a class for interacting with each of the following Sendgrid API modules:
|
10
|
+
|
11
|
+
- Bounces
|
12
|
+
- InvalidEmails
|
13
|
+
- SpamReports
|
14
|
+
- Statistics
|
15
|
+
- Unsubscribes
|
16
|
+
|
17
|
+
Consuming the Sendgrid API is as simple as instantiating a class and making the call:
|
18
|
+
|
19
|
+
unsubscribes = SendgridToolkit::Unsubscribes.new("username", "api_key")
|
20
|
+
unsubscribes.add :email => "email@to.unsubscribe"
|
21
|
+
|
22
|
+
Two lines, and voila!
|
23
|
+
|
24
|
+
Common actions are documented below under each module. For more details, visit the [Sendgrid Web API Documentation][2]
|
25
|
+
|
26
|
+
Contributing
|
27
|
+
------------
|
28
|
+
|
29
|
+
When submitting patches, please be sure you include rspec tests!
|
30
|
+
|
31
|
+
Big thanks to [James Brennan][1] for refactoring old code and writing the Bounces, InvalidEmails and SpamReports modules.
|
32
|
+
|
33
|
+
Bounces Module
|
34
|
+
--------------
|
35
|
+
The bounces module provides access to all of your bounces.
|
36
|
+
|
37
|
+
- - -
|
38
|
+
|
39
|
+
Instantiate the Bounces object:
|
40
|
+
|
41
|
+
bounces = SendgridToolkit::Bounces.new(api_user, api_key)
|
42
|
+
|
43
|
+
- - -
|
44
|
+
|
45
|
+
Retrieve bounces:
|
46
|
+
|
47
|
+
all_bounces = bounces.retrieve
|
48
|
+
|
49
|
+
`all_bounces` will be an array of hashes, each of which contains the following keys:
|
50
|
+
|
51
|
+
- `email`: the recipient's email address
|
52
|
+
- `status`: the status of email
|
53
|
+
- `reason`: the reason for the bounce, as specified by the recipient's email server
|
54
|
+
|
55
|
+
- - -
|
56
|
+
|
57
|
+
Get the timestamp of each bounce:
|
58
|
+
|
59
|
+
all_bounces = bounces.retrieve_with_timestamps
|
60
|
+
|
61
|
+
Each hash in `all_bounces` will now contain a `created` key, which holds a Ruby Time object.
|
62
|
+
|
63
|
+
- - -
|
64
|
+
|
65
|
+
If you believe an email address will no longer bounce and would like Sendgrid to deliver future emails to that address, you may delete the Bounce entry:
|
66
|
+
|
67
|
+
bounces.delete :email => "email@address.of.bounce.to.delete"
|
68
|
+
|
69
|
+
InvalidEmails Module
|
70
|
+
--------------------
|
71
|
+
|
72
|
+
The InvalidEmails module provides access to all of your email addresses.
|
73
|
+
|
74
|
+
- - -
|
75
|
+
|
76
|
+
Instantiate the InvalidEmails object:
|
77
|
+
|
78
|
+
invalid_emails_obj = SendgridToolkit::InvalidEmails.new(api_user, api_key)
|
79
|
+
|
80
|
+
- - -
|
81
|
+
|
82
|
+
Retrieve invalid emails:
|
83
|
+
|
84
|
+
invalid_emails = invalid_emails_obj.retrieve
|
85
|
+
|
86
|
+
`invalid_emails` will be an array of hashes, each of which contains the following keys:
|
87
|
+
|
88
|
+
- `email`: the recipient's email address
|
89
|
+
- `reason`: the reason the email address is believed to be invalid
|
90
|
+
|
91
|
+
- - -
|
92
|
+
|
93
|
+
Get the timestamp that each email was determined to be invalid:
|
94
|
+
|
95
|
+
invalid_emails = invalid_emails_obj.retrieve_with_timestamps
|
96
|
+
|
97
|
+
Each hash in `invalid_emails` will now contain a `created` key, which holds a Ruby Time object.
|
98
|
+
|
99
|
+
- - -
|
100
|
+
|
101
|
+
If you believe a once-invalid email address is now valid and would like Sendgrid to deliver future emails to that address, you may delete the InvalidEmail entry:
|
102
|
+
|
103
|
+
invalid_emails.delete :email => "email@address.of.invalid.to.delete"
|
104
|
+
|
105
|
+
SpamReports Module
|
106
|
+
------------------
|
107
|
+
|
108
|
+
The SpamReports module provides access to all email addresses that have reported you for spam.
|
109
|
+
|
110
|
+
- - -
|
111
|
+
|
112
|
+
Instantiate the SpamReports object:
|
113
|
+
|
114
|
+
spam_reports_obj = SendgridToolkit::SpamReports.new(api_user, api_key)
|
115
|
+
|
116
|
+
- - -
|
117
|
+
|
118
|
+
Retrieve spam reports:
|
119
|
+
|
120
|
+
spam_reports = spam_reports_obj.retrieve
|
121
|
+
|
122
|
+
`spam_reports` will be an array of hashes, each of which contains an `email` key, indicating the email address that reported you for spam.
|
123
|
+
|
124
|
+
- - -
|
125
|
+
|
126
|
+
Get the timestamp of each spam report:
|
127
|
+
|
128
|
+
spam_reports = spam_reports_obj.retrieve_with_timestamps
|
129
|
+
|
130
|
+
Each hash in `spam_reports` will now contain a `created` key, which holds a Ruby Time object.
|
131
|
+
|
132
|
+
- - -
|
133
|
+
|
134
|
+
If you believe a user will no longer consider your content to be spam, you may delete the SpamReport entry:
|
135
|
+
|
136
|
+
spam_reports.delete :email => "email@address.that.called.you.a.spammer"
|
137
|
+
|
138
|
+
Statistics Module
|
139
|
+
-----------------
|
140
|
+
The statistics module provides access to all of your email statistics.
|
141
|
+
|
142
|
+
- - -
|
143
|
+
|
144
|
+
Instantiate the Statistics object:
|
145
|
+
|
146
|
+
statistics = SendgridToolkit::Statistics.new(api_user, api_key)
|
147
|
+
|
148
|
+
- - -
|
149
|
+
|
150
|
+
Retrieve statistics:
|
151
|
+
|
152
|
+
stats = statistics.retrieve
|
153
|
+
|
154
|
+
`stats` will be an array of hashes, each of which contains the following keys:
|
155
|
+
|
156
|
+
- `date`: The date to which the statistics in this hash refer to
|
157
|
+
- `requests`: The number of emails you sent
|
158
|
+
- `bounces`: The number of users who opened your email but did not click on your links
|
159
|
+
- `clicks`: The number of users who clicked on a link in your email
|
160
|
+
- `opens`: The number of users who opened your email
|
161
|
+
- `spamreports`: The number of users who have reported your emails as spam
|
162
|
+
|
163
|
+
`stats` may also contain some keys that Sendgrid does not officially document, such as: `delivered`, `invalid_email`, `repeat_bounces`,`+repeat_spamreports`, `repeat_unsubscribes` and `unsubscribes`
|
164
|
+
|
165
|
+
- - -
|
166
|
+
|
167
|
+
To narrow your retrieval to the past 5 days:
|
168
|
+
|
169
|
+
stats = statistics.retrieve :days => 5
|
170
|
+
|
171
|
+
To narrow your retrieval to emails within the last month but before one week ago:
|
172
|
+
|
173
|
+
stats = statistics.retrieve :start_date => 1.month.ago, :end_date => 1.week.ago
|
174
|
+
|
175
|
+
To narrow your search to a particular category (applicable only if you use this Sendgrid feature):
|
176
|
+
|
177
|
+
stats = statistics.retrieve :category => "NameOfYourCategory"
|
178
|
+
|
179
|
+
Note: You may combine a category query with other options, i.e.:
|
180
|
+
|
181
|
+
stats = statistics.retrieve :category => "NameOfYourCategory", :days => 5
|
182
|
+
|
183
|
+
- - -
|
184
|
+
|
185
|
+
Receive your all-time statistics:
|
186
|
+
|
187
|
+
stats = statistics.retrieve_aggregate
|
188
|
+
|
189
|
+
`stats` will be a single hash containing all of the aforementioned keys except `date`.
|
190
|
+
|
191
|
+
- - -
|
192
|
+
|
193
|
+
If you use Sendgrid's category feature, you can list your categories:
|
194
|
+
|
195
|
+
cats = statistics.list_categories
|
196
|
+
|
197
|
+
`cats` is an array of hashes, each of which has a `category` key that holds the name of a category.
|
198
|
+
|
199
|
+
Unsubscribes Module
|
200
|
+
-------------------
|
201
|
+
The unsubscribes module manages your list of unsubscribed email addresses.
|
202
|
+
|
203
|
+
- - -
|
204
|
+
|
205
|
+
Instantiate the Unsubscribes object:
|
206
|
+
|
207
|
+
unsubscribes = SendgridToolkit::Unsubscribes.new(api_user, api_key)
|
208
|
+
|
209
|
+
- - -
|
210
|
+
|
211
|
+
List everybody who has unsubscribed from your emails with:
|
212
|
+
|
213
|
+
listing = unsubscribes.retrieve
|
214
|
+
`listing` will be an array of hashes, each of which has an `email` key.
|
215
|
+
|
216
|
+
- - -
|
217
|
+
|
218
|
+
Get the timestamp when each user unsubscribed:
|
219
|
+
|
220
|
+
listing = unsubscribes.retrieve_with_timestamps
|
221
|
+
|
222
|
+
Each hash in `listing` will now contain a `created` key, which holds a Ruby Time object.
|
223
|
+
|
224
|
+
- - -
|
225
|
+
|
226
|
+
Unsubscribe a user from your sendgrid email:
|
227
|
+
|
228
|
+
unsubscribes.add :email => "email@to.unsubscribe"
|
229
|
+
|
230
|
+
SendgridToolkit will throw `UnsubscribeEmailAlreadyExists` if the email you specified is already on the list
|
231
|
+
|
232
|
+
- - -
|
233
|
+
|
234
|
+
Remove a user from your unsubscribe list:
|
235
|
+
|
236
|
+
unsubscribes.delete :email => "email@that_is.unsubscribed"
|
237
|
+
|
238
|
+
SendgridToolkit will throw `UnsubscribeEmailDoesNotExist` if the email you specified is not on the list
|
239
|
+
|
240
|
+
|
241
|
+
A note about authentication
|
242
|
+
---------------------------
|
243
|
+
|
244
|
+
Each class is initialized with `api_user` and `api_key` parameters. `api_user` is your sendgrid account email address, and `api_key` is your sendgrid password.
|
245
|
+
|
246
|
+
If you don't supply `api_user` or `api_key`, SendgridToolkit will look for the `SMTP_USERNAME` or `SMTP_PASSWORD` environment variables. If they are not found, SendgridToolkit will throw `NoAPIKeySpecified` or `NoAPIUserSpecified`, depending on what you omitted.
|
247
|
+
|
248
|
+
If authentication fails during an API request, SendgridToolkit throws `AuthenticationFailed`.
|
249
|
+
|
250
|
+
|
251
|
+
In Case You're Curious...
|
252
|
+
-------------------------
|
253
|
+
|
254
|
+
API requests are made and responses are received in JSON. All requests are made as POSTs unless noted otherwise (Sendgrid's examples are via GET, but they support POST)
|
255
|
+
|
256
|
+
Each class takes a final options parameter in the form of a hash. You may use this parameter to pass additional options to the Sendgrid API. For example, let's say you are using the unsubscribes function:
|
257
|
+
|
258
|
+
unsubscribes = SendgridToolkit::Unsubscribes.new(api_user, api_key)
|
259
|
+
listing = unsubscribes.retrieve
|
260
|
+
|
261
|
+
If Sendgrid were to add a `only_verified` option to this API call, you could call:
|
262
|
+
|
263
|
+
listing = unsubscribes.retrieve :only_verified => true
|
264
|
+
|
265
|
+
to make use of it.
|
266
|
+
|
267
|
+
Testing
|
268
|
+
-------
|
269
|
+
|
270
|
+
In addition to unit tests, SendgridToolkit comes with a limited suite of "webconnect" tests that will actually hit Sendgrid's servers and perform various actions for purposes of real-world testing. In order to use these tests, you must:
|
271
|
+
|
272
|
+
1. Create a test account with sendgrid and store the credentials in `TEST_SMTP_USERNAME` and `TEST_SMTP_PASSWORD` environment variables. This is so that actions are performed on a test account and not your real Sendgrid account. If you forget, don't worry -- the tests will fail but they will **not** fall back on the account that uses `SMTP_USERNAME` and `SMTP_PASSWORD`.
|
273
|
+
2. Change "xit" it "it" on the tests you wish to run.
|
274
|
+
|
275
|
+
Running "spec spec" out of the box will run the standard suite of tests (all network access is stubbed out).
|
276
|
+
|
277
|
+
[1]: http://github.com/jamesBrennan
|
278
|
+
[2]: http://wiki.sendgrid.com/doku.php?id=web_api
|
data/VERSION
CHANGED
@@ -1 +1 @@
|
|
1
|
-
0.
|
1
|
+
1.0.0
|
data/lib/sendgrid_toolkit.rb
CHANGED
@@ -1,9 +1,13 @@
|
|
1
1
|
require 'httparty'
|
2
2
|
|
3
3
|
require 'sendgrid_toolkit/abstract_sendgrid_client'
|
4
|
+
require 'sendgrid_toolkit/common'
|
4
5
|
require 'sendgrid_toolkit/sendgrid_error'
|
5
6
|
require 'sendgrid_toolkit/statistics'
|
6
7
|
require 'sendgrid_toolkit/unsubscribes'
|
8
|
+
require 'sendgrid_toolkit/spam_reports'
|
9
|
+
require 'sendgrid_toolkit/bounces'
|
10
|
+
require 'sendgrid_toolkit/invalid_emails'
|
7
11
|
|
8
12
|
module SendgridToolkit
|
9
13
|
BASE_URI = "sendgrid.com/api"
|
@@ -0,0 +1,31 @@
|
|
1
|
+
module SendgridToolkit
|
2
|
+
|
3
|
+
module Common
|
4
|
+
|
5
|
+
def retrieve(options = {})
|
6
|
+
response = api_post(module_name, 'get', options)
|
7
|
+
response
|
8
|
+
end
|
9
|
+
|
10
|
+
def retrieve_with_timestamps(options = {})
|
11
|
+
options.merge! :date => 1
|
12
|
+
response = retrieve options
|
13
|
+
response.each do |unsubscribe|
|
14
|
+
unsubscribe['created'] = Time.parse(unsubscribe['created']) if unsubscribe.has_key?('created')
|
15
|
+
end
|
16
|
+
response
|
17
|
+
end
|
18
|
+
|
19
|
+
def delete(options = {})
|
20
|
+
response = api_post(module_name, 'delete', options)
|
21
|
+
raise EmailDoesNotExist if response['message'].include?('does not exist')
|
22
|
+
response
|
23
|
+
end
|
24
|
+
|
25
|
+
def module_name
|
26
|
+
self.class.to_s.split("::").last.downcase
|
27
|
+
end
|
28
|
+
|
29
|
+
end
|
30
|
+
|
31
|
+
end
|
data/sendgrid_toolkit.gemspec
CHANGED
@@ -5,29 +5,37 @@
|
|
5
5
|
|
6
6
|
Gem::Specification.new do |s|
|
7
7
|
s.name = %q{sendgrid_toolkit}
|
8
|
-
s.version = "0.
|
8
|
+
s.version = "1.0.0"
|
9
9
|
|
10
10
|
s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
|
11
11
|
s.authors = ["Robby Grossman"]
|
12
|
-
s.date = %q{2010-
|
12
|
+
s.date = %q{2010-09-10}
|
13
13
|
s.description = %q{A Ruby wrapper and utility library for communicating with the Sendgrid API}
|
14
14
|
s.email = %q{robby@freerobby.com}
|
15
15
|
s.extra_rdoc_files = [
|
16
|
-
"README.
|
16
|
+
"README.md"
|
17
17
|
]
|
18
18
|
s.files = [
|
19
19
|
".gitignore",
|
20
|
-
"README.
|
20
|
+
"README.md",
|
21
21
|
"Rakefile",
|
22
22
|
"VERSION",
|
23
23
|
"lib/sendgrid_toolkit.rb",
|
24
24
|
"lib/sendgrid_toolkit/abstract_sendgrid_client.rb",
|
25
|
+
"lib/sendgrid_toolkit/bounces.rb",
|
26
|
+
"lib/sendgrid_toolkit/common.rb",
|
27
|
+
"lib/sendgrid_toolkit/invalid_emails.rb",
|
25
28
|
"lib/sendgrid_toolkit/sendgrid_error.rb",
|
29
|
+
"lib/sendgrid_toolkit/spam_reports.rb",
|
26
30
|
"lib/sendgrid_toolkit/statistics.rb",
|
27
31
|
"lib/sendgrid_toolkit/unsubscribes.rb",
|
28
32
|
"sendgrid_toolkit.gemspec",
|
29
33
|
"spec/helper.rb",
|
30
34
|
"spec/lib/sendgrid_toolkit/abstract_sendgrid_client_spec.rb",
|
35
|
+
"spec/lib/sendgrid_toolkit/bounces_spec.rb",
|
36
|
+
"spec/lib/sendgrid_toolkit/common_spec.rb",
|
37
|
+
"spec/lib/sendgrid_toolkit/invalid_emails_spec.rb",
|
38
|
+
"spec/lib/sendgrid_toolkit/spam_reports_spec.rb",
|
31
39
|
"spec/lib/sendgrid_toolkit/statistics_spec.rb",
|
32
40
|
"spec/lib/sendgrid_toolkit/unsubscribes_spec.rb",
|
33
41
|
"spec/lib/sendgrid_toolkit_spec.rb",
|
@@ -36,11 +44,15 @@ Gem::Specification.new do |s|
|
|
36
44
|
s.homepage = %q{http://github.com/freerobby/sendgrid_toolkit}
|
37
45
|
s.rdoc_options = ["--charset=UTF-8"]
|
38
46
|
s.require_paths = ["lib"]
|
39
|
-
s.rubygems_version = %q{1.3.
|
47
|
+
s.rubygems_version = %q{1.3.7}
|
40
48
|
s.summary = %q{A Ruby wrapper and utility library for communicating with the Sendgrid API}
|
41
49
|
s.test_files = [
|
42
50
|
"spec/helper.rb",
|
43
51
|
"spec/lib/sendgrid_toolkit/abstract_sendgrid_client_spec.rb",
|
52
|
+
"spec/lib/sendgrid_toolkit/bounces_spec.rb",
|
53
|
+
"spec/lib/sendgrid_toolkit/common_spec.rb",
|
54
|
+
"spec/lib/sendgrid_toolkit/invalid_emails_spec.rb",
|
55
|
+
"spec/lib/sendgrid_toolkit/spam_reports_spec.rb",
|
44
56
|
"spec/lib/sendgrid_toolkit/statistics_spec.rb",
|
45
57
|
"spec/lib/sendgrid_toolkit/unsubscribes_spec.rb",
|
46
58
|
"spec/lib/sendgrid_toolkit_spec.rb",
|
@@ -51,7 +63,7 @@ Gem::Specification.new do |s|
|
|
51
63
|
current_version = Gem::Specification::CURRENT_SPECIFICATION_VERSION
|
52
64
|
s.specification_version = 3
|
53
65
|
|
54
|
-
if Gem::Version.new(Gem::
|
66
|
+
if Gem::Version.new(Gem::VERSION) >= Gem::Version.new('1.2.0') then
|
55
67
|
else
|
56
68
|
end
|
57
69
|
else
|
@@ -0,0 +1,46 @@
|
|
1
|
+
require File.expand_path("#{File.dirname(__FILE__)}/../../helper")
|
2
|
+
|
3
|
+
describe SendgridToolkit::Bounces do
|
4
|
+
before do
|
5
|
+
FakeWeb.clean_registry
|
6
|
+
@obj = SendgridToolkit::Bounces.new("fakeuser", "fakepass")
|
7
|
+
end
|
8
|
+
|
9
|
+
describe "#retrieve" do
|
10
|
+
it "returns array of bounced emails" do
|
11
|
+
FakeWeb.register_uri(:post, %r|https://sendgrid\.com/api/bounces\.get\.json\?|, :body => '[{"email":"email1@domain.com","status":"5.1.1","reason":"host [127.0.0.1] said: 550 5.1.1 unknown or illegal user: email1@domain.com"},{"email":"email2@domain2.com","status":"5.1.1","reason":"host [127.0.0.1] said: 550 5.1.1 unknown or illegal user: email2@domain2.com"}]')
|
12
|
+
bounces = @obj.retrieve
|
13
|
+
bounces[0]['email'].should == "email1@domain.com"
|
14
|
+
bounces[0]['status'].should == "5.1.1"
|
15
|
+
bounces[0]['reason'].should == "host [127.0.0.1] said: 550 5.1.1 unknown or illegal user: email1@domain.com"
|
16
|
+
end
|
17
|
+
end
|
18
|
+
|
19
|
+
describe "#retrieve_with_timestamps" do
|
20
|
+
it "parses timestamps" do
|
21
|
+
FakeWeb.register_uri(:post, %r|https://sendgrid\.com/api/bounces\.get\.json\?.*date=1|, :body => '[{"email":"email1@domain.com","status":"5.1.1","reason":"host [127.0.0.1] said: 550 5.1.1 unknown or illegal user: email1@domain.com","created":"2009-06-01 19:41:39"},{"email":"email2@domain2.com","status":"5.1.1","reason":"host [127.0.0.1] said: 550 5.1.1 unknown or illegal user: email2@domain2.com","created":"2009-06-12 19:41:39"}]')
|
22
|
+
bounces = @obj.retrieve_with_timestamps
|
23
|
+
0.upto(1) do |index|
|
24
|
+
bounces[index]['created'].kind_of?(Time).should == true
|
25
|
+
end
|
26
|
+
bounces[0]['created'].asctime.should == "Mon Jun 1 19:41:39 2009"
|
27
|
+
bounces[1]['created'].asctime.should == "Fri Jun 12 19:41:39 2009"
|
28
|
+
end
|
29
|
+
end
|
30
|
+
|
31
|
+
describe "#delete" do
|
32
|
+
it "raises no errors on success" do
|
33
|
+
FakeWeb.register_uri(:post, %r|https://sendgrid\.com/api/bounces\.delete\.json\?.*email=.+|, :body => '{"message":"success"}')
|
34
|
+
lambda {
|
35
|
+
@obj.delete :email => "user@domain.com"
|
36
|
+
}.should_not raise_error
|
37
|
+
end
|
38
|
+
it "raises error when email address does not exist" do
|
39
|
+
FakeWeb.register_uri(:post, %r|https://sendgrid\.com/api/bounces\.delete\.json\?.*email=.+|, :body => '{"message":"Email does not exist"}')
|
40
|
+
lambda {
|
41
|
+
@obj.delete :email => "user@domain.com"
|
42
|
+
}.should raise_error SendgridToolkit::EmailDoesNotExist
|
43
|
+
end
|
44
|
+
end
|
45
|
+
|
46
|
+
end
|
@@ -0,0 +1,16 @@
|
|
1
|
+
require File.expand_path("#{File.dirname(__FILE__)}/../../helper")
|
2
|
+
|
3
|
+
describe SendgridToolkit::Common do
|
4
|
+
|
5
|
+
before do
|
6
|
+
class FakeClass
|
7
|
+
include SendgridToolkit::Common
|
8
|
+
end
|
9
|
+
@fake_class = FakeClass.new
|
10
|
+
end
|
11
|
+
|
12
|
+
it "creates a module_name method that returns the class name downcased" do
|
13
|
+
@fake_class.module_name.should == "fakeclass"
|
14
|
+
end
|
15
|
+
|
16
|
+
end
|
@@ -0,0 +1,50 @@
|
|
1
|
+
require File.expand_path("#{File.dirname(__FILE__)}/../../helper")
|
2
|
+
|
3
|
+
describe SendgridToolkit::InvalidEmails do
|
4
|
+
before do
|
5
|
+
FakeWeb.clean_registry
|
6
|
+
@obj = SendgridToolkit::InvalidEmails.new("fakeuser", "fakepass")
|
7
|
+
end
|
8
|
+
|
9
|
+
describe "#retrieve" do
|
10
|
+
it "returns array of invalid emails" do
|
11
|
+
FakeWeb.register_uri(:post, %r|https://sendgrid\.com/api/invalidemails\.get\.json\?|, :body => '[{"email":"isaac@hotmail.comm","reason":"Mail domain mentioned in email address is unknown"},{"email":"isaac@hotmail","reason":"Bad Syntax"},{"email":"isaac@example.com","reason":"Known bad domain"}]')
|
12
|
+
invalid_emails = @obj.retrieve
|
13
|
+
invalid_emails[0]['email'].should == "isaac@hotmail.comm"
|
14
|
+
invalid_emails[0]['reason'].should == "Mail domain mentioned in email address is unknown"
|
15
|
+
invalid_emails[1]['email'].should == "isaac@hotmail"
|
16
|
+
invalid_emails[1]['reason'].should == "Bad Syntax"
|
17
|
+
invalid_emails[2]['email'].should == "isaac@example.com"
|
18
|
+
invalid_emails[2]['reason'].should == "Known bad domain"
|
19
|
+
end
|
20
|
+
end
|
21
|
+
|
22
|
+
describe "#retrieve_with_timestamps" do
|
23
|
+
it "parses timestamps" do
|
24
|
+
FakeWeb.register_uri(:post, %r|https://sendgrid\.com/api/invalidemails\.get\.json\?.*date=1|, :body => '[{"email":"isaac@hotmail.comm","reason":"Mail domain mentioned in email address is unknown","created":"2009-06-01 19:41:39"},{"email":"isaac@hotmail","reason":"Bad Syntax","created":"2009-06-12 19:41:39"},{"email":"isaac@example.com","reason":"Known bad domain","created":"2009-06-13 09:40:01"}]')
|
25
|
+
invalid_emails = @obj.retrieve_with_timestamps
|
26
|
+
0.upto(2) do |index|
|
27
|
+
invalid_emails[index]['created'].kind_of?(Time).should == true
|
28
|
+
end
|
29
|
+
invalid_emails[0]['created'].asctime.should == "Mon Jun 1 19:41:39 2009"
|
30
|
+
invalid_emails[1]['created'].asctime.should == "Fri Jun 12 19:41:39 2009"
|
31
|
+
invalid_emails[2]['created'].asctime.should == "Sat Jun 13 09:40:01 2009"
|
32
|
+
end
|
33
|
+
end
|
34
|
+
|
35
|
+
describe "#delete" do
|
36
|
+
it "raises no errors on success" do
|
37
|
+
FakeWeb.register_uri(:post, %r|https://sendgrid\.com/api/invalidemails\.delete\.json\?.*email=.+|, :body => '{"message":"success"}')
|
38
|
+
lambda {
|
39
|
+
@obj.delete :email => "user@domain.com"
|
40
|
+
}.should_not raise_error
|
41
|
+
end
|
42
|
+
it "raises error when email address does not exist" do
|
43
|
+
FakeWeb.register_uri(:post, %r|https://sendgrid\.com/api/invalidemails\.delete\.json\?.*email=.+|, :body => '{"message":"Email does not exist"}')
|
44
|
+
lambda {
|
45
|
+
@obj.delete :email => "user@domain.com"
|
46
|
+
}.should raise_error SendgridToolkit::EmailDoesNotExist
|
47
|
+
end
|
48
|
+
end
|
49
|
+
|
50
|
+
end
|
@@ -0,0 +1,45 @@
|
|
1
|
+
require File.expand_path("#{File.dirname(__FILE__)}/../../helper")
|
2
|
+
|
3
|
+
describe SendgridToolkit::SpamReports do
|
4
|
+
before do
|
5
|
+
FakeWeb.clean_registry
|
6
|
+
@obj = SendgridToolkit::SpamReports.new("fakeuser", "fakepass")
|
7
|
+
end
|
8
|
+
|
9
|
+
describe "#retrieve" do
|
10
|
+
it "returns array of bounced emails" do
|
11
|
+
FakeWeb.register_uri(:post, %r|https://sendgrid\.com/api/spamreports\.get\.json\?|, :body => '[{"email":"email1@domain.com"},{"email":"email2@domain2.com"}]')
|
12
|
+
bounces = @obj.retrieve
|
13
|
+
bounces[0]['email'].should == "email1@domain.com"
|
14
|
+
bounces[1]['email'].should == "email2@domain2.com"
|
15
|
+
end
|
16
|
+
end
|
17
|
+
|
18
|
+
describe "#retrieve_with_timestamps" do
|
19
|
+
it "parses timestamps" do
|
20
|
+
FakeWeb.register_uri(:post, %r|https://sendgrid\.com/api/spamreports\.get\.json\?.*date=1|, :body => '[{"email":"email1@domain.com","created":"2009-06-01 19:41:39"},{"email":"email2@domain2.com","created":"2009-06-12 19:41:39"}]')
|
21
|
+
bounces = @obj.retrieve_with_timestamps
|
22
|
+
0.upto(1) do |index|
|
23
|
+
bounces[index]['created'].kind_of?(Time).should == true
|
24
|
+
end
|
25
|
+
bounces[0]['created'].asctime.should == "Mon Jun 1 19:41:39 2009"
|
26
|
+
bounces[1]['created'].asctime.should == "Fri Jun 12 19:41:39 2009"
|
27
|
+
end
|
28
|
+
end
|
29
|
+
|
30
|
+
describe "#delete" do
|
31
|
+
it "raises no errors on success" do
|
32
|
+
FakeWeb.register_uri(:post, %r|https://sendgrid\.com/api/spamreports\.delete\.json\?.*email=.+|, :body => '{"message":"success"}')
|
33
|
+
lambda {
|
34
|
+
@obj.delete :email => "user@domain.com"
|
35
|
+
}.should_not raise_error
|
36
|
+
end
|
37
|
+
it "raises error when email address does not exist" do
|
38
|
+
FakeWeb.register_uri(:post, %r|https://sendgrid\.com/api/spamreports\.delete\.json\?.*email=.+|, :body => '{"message":"Email does not exist"}')
|
39
|
+
lambda {
|
40
|
+
@obj.delete :email => "user@domain.com"
|
41
|
+
}.should raise_error SendgridToolkit::EmailDoesNotExist
|
42
|
+
end
|
43
|
+
end
|
44
|
+
|
45
|
+
end
|
metadata
CHANGED
@@ -1,12 +1,13 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: sendgrid_toolkit
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
+
hash: 23
|
4
5
|
prerelease: false
|
5
6
|
segments:
|
6
|
-
- 0
|
7
|
-
- 1
|
8
7
|
- 1
|
9
|
-
|
8
|
+
- 0
|
9
|
+
- 0
|
10
|
+
version: 1.0.0
|
10
11
|
platform: ruby
|
11
12
|
authors:
|
12
13
|
- Robby Grossman
|
@@ -14,7 +15,7 @@ autorequire:
|
|
14
15
|
bindir: bin
|
15
16
|
cert_chain: []
|
16
17
|
|
17
|
-
date: 2010-
|
18
|
+
date: 2010-09-10 00:00:00 -04:00
|
18
19
|
default_executable:
|
19
20
|
dependencies: []
|
20
21
|
|
@@ -25,20 +26,28 @@ executables: []
|
|
25
26
|
extensions: []
|
26
27
|
|
27
28
|
extra_rdoc_files:
|
28
|
-
- README.
|
29
|
+
- README.md
|
29
30
|
files:
|
30
31
|
- .gitignore
|
31
|
-
- README.
|
32
|
+
- README.md
|
32
33
|
- Rakefile
|
33
34
|
- VERSION
|
34
35
|
- lib/sendgrid_toolkit.rb
|
35
36
|
- lib/sendgrid_toolkit/abstract_sendgrid_client.rb
|
37
|
+
- lib/sendgrid_toolkit/bounces.rb
|
38
|
+
- lib/sendgrid_toolkit/common.rb
|
39
|
+
- lib/sendgrid_toolkit/invalid_emails.rb
|
36
40
|
- lib/sendgrid_toolkit/sendgrid_error.rb
|
41
|
+
- lib/sendgrid_toolkit/spam_reports.rb
|
37
42
|
- lib/sendgrid_toolkit/statistics.rb
|
38
43
|
- lib/sendgrid_toolkit/unsubscribes.rb
|
39
44
|
- sendgrid_toolkit.gemspec
|
40
45
|
- spec/helper.rb
|
41
46
|
- spec/lib/sendgrid_toolkit/abstract_sendgrid_client_spec.rb
|
47
|
+
- spec/lib/sendgrid_toolkit/bounces_spec.rb
|
48
|
+
- spec/lib/sendgrid_toolkit/common_spec.rb
|
49
|
+
- spec/lib/sendgrid_toolkit/invalid_emails_spec.rb
|
50
|
+
- spec/lib/sendgrid_toolkit/spam_reports_spec.rb
|
42
51
|
- spec/lib/sendgrid_toolkit/statistics_spec.rb
|
43
52
|
- spec/lib/sendgrid_toolkit/unsubscribes_spec.rb
|
44
53
|
- spec/lib/sendgrid_toolkit_spec.rb
|
@@ -53,29 +62,37 @@ rdoc_options:
|
|
53
62
|
require_paths:
|
54
63
|
- lib
|
55
64
|
required_ruby_version: !ruby/object:Gem::Requirement
|
65
|
+
none: false
|
56
66
|
requirements:
|
57
67
|
- - ">="
|
58
68
|
- !ruby/object:Gem::Version
|
69
|
+
hash: 3
|
59
70
|
segments:
|
60
71
|
- 0
|
61
72
|
version: "0"
|
62
73
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
74
|
+
none: false
|
63
75
|
requirements:
|
64
76
|
- - ">="
|
65
77
|
- !ruby/object:Gem::Version
|
78
|
+
hash: 3
|
66
79
|
segments:
|
67
80
|
- 0
|
68
81
|
version: "0"
|
69
82
|
requirements: []
|
70
83
|
|
71
84
|
rubyforge_project:
|
72
|
-
rubygems_version: 1.3.
|
85
|
+
rubygems_version: 1.3.7
|
73
86
|
signing_key:
|
74
87
|
specification_version: 3
|
75
88
|
summary: A Ruby wrapper and utility library for communicating with the Sendgrid API
|
76
89
|
test_files:
|
77
90
|
- spec/helper.rb
|
78
91
|
- spec/lib/sendgrid_toolkit/abstract_sendgrid_client_spec.rb
|
92
|
+
- spec/lib/sendgrid_toolkit/bounces_spec.rb
|
93
|
+
- spec/lib/sendgrid_toolkit/common_spec.rb
|
94
|
+
- spec/lib/sendgrid_toolkit/invalid_emails_spec.rb
|
95
|
+
- spec/lib/sendgrid_toolkit/spam_reports_spec.rb
|
79
96
|
- spec/lib/sendgrid_toolkit/statistics_spec.rb
|
80
97
|
- spec/lib/sendgrid_toolkit/unsubscribes_spec.rb
|
81
98
|
- spec/lib/sendgrid_toolkit_spec.rb
|
data/README.rdoc
DELETED
@@ -1,103 +0,0 @@
|
|
1
|
-
= SendgridToolkit - a Ruby wrapper for the Sendgrid Web API
|
2
|
-
|
3
|
-
The Statistics and Unsubscribe APIs are fully supported.
|
4
|
-
|
5
|
-
This allows you to:
|
6
|
-
* manage your unsubscribed users
|
7
|
-
* view API-supported email statistics.
|
8
|
-
|
9
|
-
Support for Sendgrid's other Web APIs is coming soon.
|
10
|
-
|
11
|
-
SendgridToolkit provides one class for each Web API module.
|
12
|
-
|
13
|
-
=== A note about authentication
|
14
|
-
Each class is initialized with +api_user+ and +api_key+ parameters. +api_user+ is your sendgrid account email address, and +api_key+ is your sendgrid password.
|
15
|
-
|
16
|
-
If you don't supply +api_user+ or +api_key+, SendgridToolkit will look for the SMTP_USERNAME or SMTP_PASSWORD environment variables. If they are not found, SendgridToolkit will throw +NoAPIKeySpecified+ or +NoAPIUserSpecified+, depending on what you omitted.
|
17
|
-
|
18
|
-
If authentication fails during an API request, SendgridToolkit throws AuthenticationFailed.
|
19
|
-
|
20
|
-
== Unsubscribes Class
|
21
|
-
|
22
|
-
Instantiate the Unsubscribes object:
|
23
|
-
unsubscribes = SendgridToolkit::Unsubscribes.new(api_user, api_key)
|
24
|
-
|
25
|
-
=== Listing Unsubscribes
|
26
|
-
You can see everybody who has unsubscribed from your emails with:
|
27
|
-
listing = unsubscribes.retrieve
|
28
|
-
+listing+ will be an array of hashes, each of which has an +email+ key.
|
29
|
-
|
30
|
-
If you want the timestamp for when each user unsubscribed, use:
|
31
|
-
listing = unsubscribes.retrieve_with_timestamps
|
32
|
-
Each hash in +listing+ will now contain a +created+ key, which holds a Ruby Time object.
|
33
|
-
|
34
|
-
=== Adding Unsubscribes
|
35
|
-
To manually unsubscribe a user from your sendgrid emails, use:
|
36
|
-
unsubscribes.add :email => "email@to.unsubscribe"
|
37
|
-
|
38
|
-
SendgridToolkit will throw +UnsubscribeEmailAlreadyExists+ if the email you specified is already on the list
|
39
|
-
|
40
|
-
=== Deleting Unsubscribes
|
41
|
-
To remove a user from your unsubscribe list, use:
|
42
|
-
unsubscribes.delete :email => "email@that_is.unsubscribed"
|
43
|
-
|
44
|
-
SendgridToolkit will throw +UnsubscribeEmailDoesNotExist+ if the email you specified is not on the list
|
45
|
-
|
46
|
-
== Statistics Class
|
47
|
-
|
48
|
-
Instantiate the Statistics object:
|
49
|
-
statistics = SendgridToolkit::Statistics.new(api_user, api_key)
|
50
|
-
|
51
|
-
=== Retrieve statistics
|
52
|
-
To retrieve statistics, use:
|
53
|
-
stats = statistics.retrieve
|
54
|
-
+stats+ will be an array of hashes, each of which contains the following keys:
|
55
|
-
* +date+: The date to which the statistics in this hash refer to
|
56
|
-
* +requests+: The number of emails you sent
|
57
|
-
* +bounces+: The number of users who opened your email but did not click on your links
|
58
|
-
* +clicks+: The number of users who clicked on a link in your email
|
59
|
-
* +opens+: The number of users who opened your email
|
60
|
-
* +spamreports+: The number of users who have reported your emails as spam
|
61
|
-
+stats+ may also contain some keys that Sendgrid does not officially document, such as: +delivered+, +invalid_email+, +repeat_bounces+, +repeat_spamreports+, +repeat_unsubscribes+ and +unsubscribes+
|
62
|
-
|
63
|
-
To narrow your retrieval to the past 5 days, you would use:
|
64
|
-
stats = statistics.retrieve :days => 5
|
65
|
-
|
66
|
-
To narrow your retrieval to emails within the last month but before one week ago, you would use:
|
67
|
-
stats = statistics.retrieve :start_date => 1.month.ago, :end_date => 1.week.ago
|
68
|
-
|
69
|
-
To narrow your search to a particular category (applicable only if you use this Sendgrid feature), you would use:
|
70
|
-
stats = statistics.retrieve :category => "NameOfYourCategory"
|
71
|
-
Note: You may combine a category query with other options, i.e.:
|
72
|
-
stats = statistics.retrieve :category => "NameOfYourCategory", :days => 5
|
73
|
-
|
74
|
-
=== Retrieve aggregate (all-time) statistics
|
75
|
-
To receive your all-time statistics, use:
|
76
|
-
stats = statistics.retrieve_aggregate
|
77
|
-
+stats+ will be a single hash containing all of the aforementioned keys except +date+.
|
78
|
-
|
79
|
-
=== List your categories
|
80
|
-
If you use Sendgrid's category feature, you can get a listing of your categories with:
|
81
|
-
cats = statistics.list_categories
|
82
|
-
+cats+ is an array of hashes, each of which has a +category+ key that holds the name of a category.
|
83
|
-
|
84
|
-
= Behind the Scenes
|
85
|
-
Things you may want to know:
|
86
|
-
|
87
|
-
1. API requests are made and responses are received in JSON.
|
88
|
-
|
89
|
-
2. All requests are made as POSTs unless noted otherwise (Sendgrid's examples are via GET, but they support POST)
|
90
|
-
|
91
|
-
3. Each class takes a final options parameter in the form of a hash. You may use this parameter to pass additional options to the Sendgrid API. For example, let's say you are using the unsubscribes function:
|
92
|
-
unsubscribes = SendgridToolkit::Unsubscribes.new(api_user, api_key)
|
93
|
-
listing = unsubscribes.retrieve
|
94
|
-
If Sendgrid were to add a +only_verified+ option to this API call, you could call:
|
95
|
-
listing = unsubscribes.retrieve :only_verified => true
|
96
|
-
to make use of it.
|
97
|
-
|
98
|
-
= Testing
|
99
|
-
In addition to unit tests, SendgridToolkit comes with a suite of "webconnect" tests that will actually hit Sendgrid's servers and perform various actions for purposes of real-world testing. In order to use these tests, you must:
|
100
|
-
1. Create a test account with sendgrid and store the credentials in TEST_SMTP_USERNAME and TEST_SMTP_PASSWORD environment variables. This is so that actions are performed on a test account and not your real sendgrid account. If you forget, don't worry -- the tests will fail but they will not fall back on the account that uses SMTP_USERNAME and SMTP_PASSWORD.
|
101
|
-
2. Change "xit" it "it" on the tests you wish to run.
|
102
|
-
|
103
|
-
Running "spec spec" out of the box will run the standard suite of tests (all network access is stubbed out).
|