lib/apidiesel/api.rb in apidiesel-0.9 vs lib/apidiesel/api.rb in apidiesel-0.10
- old
+ new
@@ -24,22 +24,12 @@
# # Registers endpoints Action1 and Action2
# MyApi::Api.register_actions
#
class Api
class << self
- def request_handlers
- @request_handlers ||= []
- end
+ include Handlers
- def response_handlers
- @response_handlers ||= []
- end
-
- def exception_handlers
- @exception_handlers ||= []
- end
-
def config(key = nil, value = nil)
@config ||= {}
if key && value
@config[key] = value
@@ -72,22 +62,22 @@
else
config[:http_method]
end
end
- # Registers a handler for requests and/or responses
+ # Combined getter/setter for the HTTP Basic Auth
#
- # @param [Class] klass
-
- def use(klass, *args, &block)
- request_handler = "#{klass.name}::RequestHandler".safe_constantize
- response_handler = "#{klass.name}::ResponseHandler".safe_constantize
- exception_handler = "#{klass.name}::ExceptionHandler".safe_constantize
-
- request_handlers << request_handler.new(*args, &block) if request_handler
- response_handlers << response_handler.new(*args, &block) if response_handler
- exception_handlers << exception_handler.new(*args, &block) if exception_handler
+ # Falls back to the Api setting if blank.
+ #
+ # @param [String] value
+ def http_basic_auth(username = nil, password = nil)
+ if username && password
+ config[:http_basic_username] = username
+ config[:http_basic_password] = password
+ else
+ return config[:http_basic_username], config[:http_basic_password]
+ end
end
# Registers the individual API endpoint definitions
def register_actions
namespace = "#{self.name.deconstantize}::Actions".safe_constantize
@@ -118,35 +108,44 @@
protected
def execute_request(action_klass, *args)
request = action_klass.new(self).build_request(*args)
- self.class.request_handlers.each do |handler|
+ request_handlers =
+ action_klass.request_handlers.any? ? action_klass.request_handlers : self.class.request_handlers
+
+ response_handlers =
+ action_klass.response_handlers.any? ? action_klass.response_handlers : self.class.response_handlers
+
+ request_handlers.each do |handler|
request = handler.run(request, @config)
break if request.response_body.present?
end
unless request.response_body.present?
raise "All request handlers failed to deliver a response"
end
- self.class.response_handlers.each do |handler|
+ response_handlers.each do |handler|
request = handler.run(request, @config)
end
response_handler_klasses =
- self.class.response_handlers.collect { |handler| handler.class.name.to_s.demodulize }
+ response_handlers.collect { |handler| handler.class.name.split('::')[-2] }
# Execute the actions' `responds_with` block automatically, unless
# the handler has been included manually in order to control the
# order in which the handlers are run
unless response_handler_klasses.include?('ActionResponseProcessor')
request.process_response
end
request
rescue => e
- self.class.exception_handlers.each do |handler|
+ exception_handlers =
+ action_klass.exception_handlers.any? ? action_klass.exception_handlers : self.class.exception_handlers
+
+ exception_handlers.each do |handler|
request = handler.run(e, request, @config)
end
raise e
end