Permalink
Browse files

Created "common" module that holds common functionality accross sever…

…al modules. Genericized EmailDoesNotExist error to accomodate common module.
  • Loading branch information...
1 parent d548c54 commit 7c20194703a87220de951005408d5c890adcad7f @jamesBrennan jamesBrennan committed Sep 10, 2010
View
@@ -1,12 +1,13 @@
require 'httparty'
require 'sendgrid_toolkit/abstract_sendgrid_client'
+require 'sendgrid_toolkit/common'
require 'sendgrid_toolkit/sendgrid_error'
require 'sendgrid_toolkit/statistics'
-require 'sendgrid_toolkit/bounces'
+require 'sendgrid_toolkit/unsubscribes'
require 'sendgrid_toolkit/spam_reports'
+require 'sendgrid_toolkit/bounces'
require 'sendgrid_toolkit/invalid_emails'
-require 'sendgrid_toolkit/unsubscribes'
module SendgridToolkit
BASE_URI = "sendgrid.com/api"
@@ -1,27 +1,7 @@
module SendgridToolkit
class Bounces < AbstractSendgridClient
-
- def retrieve(options = {})
- response = api_post('bounces', '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('bounces', 'delete', options)
- raise BounceEmailDoesNotExist if response['message'].include?('does not exist')
- response
- end
-
+ include SendgridToolkit::Common
end
end
@@ -0,0 +1,31 @@
+module SendgridToolkit
+
+ module Common
+
+ def retrieve(options = {})
+ response = api_post(module_name, '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(module_name, 'delete', options)
+ raise EmailDoesNotExist if response['message'].include?('does not exist')
+ response
+ end
+
+ def module_name
+ self.class.to_s.split("::").last.downcase
+ end
+
+ end
+
+end
@@ -1,27 +1,7 @@
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
-
+ include SendgridToolkit::Common
end
end
@@ -4,7 +4,5 @@ class NoAPIKeySpecified < StandardError; end
class NoAPIUserSpecified < StandardError; end
class UnsubscribeEmailAlreadyExists < StandardError; end
class UnsubscribeEmailDoesNotExist < StandardError; end
- class BounceEmailDoesNotExist < StandardError; end
- class SpamReportEmailDoesNotExist < StandardError; end
- class InvalidEmailDoesNotExist < StandardError; end
+ class EmailDoesNotExist < StandardError; end
end
@@ -1,27 +1,7 @@
module SendgridToolkit
class SpamReports < AbstractSendgridClient
-
- def retrieve(options = {})
- response = api_post('spamreports', '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('spamreports', 'delete', options)
- raise SpamReportEmailDoesNotExist if response['message'].include?('does not exist')
- response
- end
-
+ include SendgridToolkit::Common
end
end
@@ -39,7 +39,7 @@
FakeWeb.register_uri(:post, %r|https://sendgrid\.com/api/bounces\.delete\.json\?.*email=.+|, :body => '{"message":"Email does not exist"}')
lambda {
@obj.delete :email => "user@domain.com"
- }.should raise_error SendgridToolkit::BounceEmailDoesNotExist
+ }.should raise_error SendgridToolkit::EmailDoesNotExist
end
end
@@ -0,0 +1,16 @@
+require File.expand_path("#{File.dirname(__FILE__)}/../../helper")
+
+describe SendgridToolkit::Common do
+
+ before do
+ class FakeClass
+ include SendgridToolkit::Common
+ end
+ @fake_class = FakeClass.new
+ end
+
+ it "creates a module_name method that returns the class name downcased" do
+ @fake_class.module_name.should == "fakeclass"
+ end
+
+end
@@ -43,7 +43,7 @@
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
+ }.should raise_error SendgridToolkit::EmailDoesNotExist
end
end
@@ -38,7 +38,7 @@
FakeWeb.register_uri(:post, %r|https://sendgrid\.com/api/spamreports\.delete\.json\?.*email=.+|, :body => '{"message":"Email does not exist"}')
lambda {
@obj.delete :email => "user@domain.com"
- }.should raise_error SendgridToolkit::SpamReportEmailDoesNotExist
+ }.should raise_error SendgridToolkit::EmailDoesNotExist
end
end

0 comments on commit 7c20194

Please sign in to comment.