Permalink
Browse files

Added invalid_emails module

  • Loading branch information...
1 parent 81fdde1 commit d548c54bdf3dc8f4cc2c2624b1c281a5e1016548 @jamesBrennan jamesBrennan committed Sep 10, 2010
View
@@ -1,2 +1,3 @@
pkg/
-doc/
+doc/
+.idea/*
View
@@ -5,6 +5,7 @@
require 'sendgrid_toolkit/statistics'
require 'sendgrid_toolkit/bounces'
require 'sendgrid_toolkit/spam_reports'
+require 'sendgrid_toolkit/invalid_emails'
require 'sendgrid_toolkit/unsubscribes'
module SendgridToolkit
@@ -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
@@ -6,4 +6,5 @@ class UnsubscribeEmailAlreadyExists < StandardError; end
class UnsubscribeEmailDoesNotExist < StandardError; end
class BounceEmailDoesNotExist < StandardError; end
class SpamReportEmailDoesNotExist < StandardError; end
+ class InvalidEmailDoesNotExist < StandardError; end
end
@@ -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

0 comments on commit d548c54

Please sign in to comment.