diff options
author | Azul <azul@riseup.net> | 2016-08-29 09:41:12 +0200 |
---|---|---|
committer | Azul <azul@riseup.net> | 2016-08-29 09:41:12 +0200 |
commit | 58d687f927aabc8aa2fdfc46132cb71706bdde46 (patch) | |
tree | 5324fe4255437b1e1c050e1b2e087d3023ad22b5 /lib/nickserver/dispatcher.rb | |
parent | 16d77cc03068b19791e704abfc2eb14f8cd141f0 (diff) |
refactor: split up ResponseHandler
Now we have a Dispatcher and two ResponseHandlers that have the same interface.
Moving towards a Chain of Responsibility pattern.
Diffstat (limited to 'lib/nickserver/dispatcher.rb')
-rw-r--r-- | lib/nickserver/dispatcher.rb | 46 |
1 files changed, 46 insertions, 0 deletions
diff --git a/lib/nickserver/dispatcher.rb b/lib/nickserver/dispatcher.rb new file mode 100644 index 0000000..b818b98 --- /dev/null +++ b/lib/nickserver/dispatcher.rb @@ -0,0 +1,46 @@ +require 'nickserver/request' +require 'nickserver/request_handlers/email_handler' +require 'nickserver/request_handlers/fingerprint_handler' + +module Nickserver + class Dispatcher + + def initialize(responder) + @responder = responder + end + + def respond_to(params, headers) + request = Nickserver::Request.new params, headers + response = handle request + send_response response.status, response.content + end + + protected + + def handle(request) + handler = handler_for_request request + handler.call request + rescue RuntimeError => exc + puts "Error: #{exc}" + puts exc.backtrace + ErrorResponse.new(exc.to_s) + end + + def handler_for_request(request) + if request.email + RequestHandlers::EmailHandler.new + elsif request.fingerprint + RequestHandlers::FingerprintHandler.new + else + Proc.new { Nickserver::Response.new(404, "Not Found\n") } + end + end + + def send_response(status = 200, content = '') + responder.respond status, content + end + + attr_reader :responder + + end +end |