forked from freerobby/sendgrid_toolkit
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
James Brennan
committed
Sep 10, 2010
1 parent
81fdde1
commit d548c54
Showing
5 changed files
with
81 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,3 @@ | ||
pkg/ | ||
doc/ | ||
doc/ | ||
.idea/* |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
module SendgridToolkit | ||
|
||
class InvalidEmails < AbstractSendgridClient | ||
|
||
def retrieve(options = {}) | ||
response = api_post('invalidemails', 'get', options) | ||
response | ||
end | ||
|
||
def retrieve_with_timestamps(options = {}) | ||
options.merge! :date => 1 | ||
response = retrieve options | ||
response.each do |unsubscribe| | ||
unsubscribe['created'] = Time.parse(unsubscribe['created']) if unsubscribe.has_key?('created') | ||
end | ||
response | ||
end | ||
|
||
def delete(options = {}) | ||
response = api_post('invalidemails', 'delete', options) | ||
raise InvalidEmailDoesNotExist if response['message'].include?('does not exist') | ||
response | ||
end | ||
|
||
end | ||
|
||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
require File.expand_path("#{File.dirname(__FILE__)}/../../helper") | ||
|
||
describe SendgridToolkit::InvalidEmails do | ||
before do | ||
FakeWeb.clean_registry | ||
@obj = SendgridToolkit::InvalidEmails.new("fakeuser", "fakepass") | ||
end | ||
|
||
describe "#retrieve" do | ||
it "returns array of invalid emails" do | ||
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"}]') | ||
invalid_emails = @obj.retrieve | ||
invalid_emails[0]['email'].should == "isaac@hotmail.comm" | ||
invalid_emails[0]['reason'].should == "Mail domain mentioned in email address is unknown" | ||
invalid_emails[1]['email'].should == "isaac@hotmail" | ||
invalid_emails[1]['reason'].should == "Bad Syntax" | ||
invalid_emails[2]['email'].should == "isaac@example.com" | ||
invalid_emails[2]['reason'].should == "Known bad domain" | ||
end | ||
end | ||
|
||
describe "#retrieve_with_timestamps" do | ||
it "parses timestamps" do | ||
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"}]') | ||
invalid_emails = @obj.retrieve_with_timestamps | ||
0.upto(2) do |index| | ||
invalid_emails[index]['created'].kind_of?(Time).should == true | ||
end | ||
invalid_emails[0]['created'].asctime.should == "Mon Jun 1 19:41:39 2009" | ||
invalid_emails[1]['created'].asctime.should == "Fri Jun 12 19:41:39 2009" | ||
invalid_emails[2]['created'].asctime.should == "Sat Jun 13 09:40:01 2009" | ||
end | ||
end | ||
|
||
describe "#delete" do | ||
it "raises no errors on success" do | ||
FakeWeb.register_uri(:post, %r|https://sendgrid\.com/api/invalidemails\.delete\.json\?.*email=.+|, :body => '{"message":"success"}') | ||
lambda { | ||
@obj.delete :email => "user@domain.com" | ||
}.should_not raise_error | ||
end | ||
it "raises error when email address does not exist" do | ||
FakeWeb.register_uri(:post, %r|https://sendgrid\.com/api/invalidemails\.delete\.json\?.*email=.+|, :body => '{"message":"Email does not exist"}') | ||
lambda { | ||
@obj.delete :email => "user@domain.com" | ||
}.should raise_error SendgridToolkit::InvalidEmailDoesNotExist | ||
end | ||
end | ||
|
||
end |