diff options
author | Azul <azul@riseup.net> | 2016-08-30 12:10:57 +0200 |
---|---|---|
committer | Azul <azul@riseup.net> | 2016-08-30 12:36:40 +0200 |
commit | b22e23eb9f5a1cb3e37ab2a26b1091183574f4e4 (patch) | |
tree | b3ca7f91dd78994c5d0164645922b3929b9177a3 | |
parent | 494e6f32daf0f6bf37321507b83848b4be087100 (diff) |
refactor: make the RequestHandler classes callable
Whenever a RequestHandler class is called we instantiate it with the request.
Then we call handle on the instance. This way we can access the request and
its content via accessors rather than only in the handle method.
-rw-r--r-- | lib/nickserver/dispatcher.rb | 8 | ||||
-rw-r--r-- | lib/nickserver/request_handlers/base.rb | 18 | ||||
-rw-r--r-- | lib/nickserver/request_handlers/fingerprint_handler.rb | 25 | ||||
-rw-r--r-- | lib/nickserver/request_handlers/hkp_email_handler.rb | 12 | ||||
-rw-r--r-- | lib/nickserver/request_handlers/invalid_email_handler.rb | 12 | ||||
-rw-r--r-- | lib/nickserver/request_handlers/local_email_handler.rb | 20 | ||||
-rw-r--r-- | test/unit/request_handlers/local_email_handler_test.rb | 2 |
7 files changed, 65 insertions, 32 deletions
diff --git a/lib/nickserver/dispatcher.rb b/lib/nickserver/dispatcher.rb index e461f12..9a031ab 100644 --- a/lib/nickserver/dispatcher.rb +++ b/lib/nickserver/dispatcher.rb @@ -47,10 +47,10 @@ module Nickserver def handler_chain [ - RequestHandlers::InvalidEmailHandler.new, - RequestHandlers::LocalEmailHandler.new, - RequestHandlers::HkpEmailHandler.new, - RequestHandlers::FingerprintHandler.new, + RequestHandlers::InvalidEmailHandler, + RequestHandlers::LocalEmailHandler, + RequestHandlers::HkpEmailHandler, + RequestHandlers::FingerprintHandler, Proc.new { Nickserver::Response.new(404, "Not Found\n") } ] end diff --git a/lib/nickserver/request_handlers/base.rb b/lib/nickserver/request_handlers/base.rb new file mode 100644 index 0000000..e5d8992 --- /dev/null +++ b/lib/nickserver/request_handlers/base.rb @@ -0,0 +1,18 @@ +module Nickserver + module RequestHandlers + class Base + + def self.call(request) + new(request).handle + end + + def initialize(request) + @request = request + end + + protected + attr_reader :request + end + end +end + diff --git a/lib/nickserver/request_handlers/fingerprint_handler.rb b/lib/nickserver/request_handlers/fingerprint_handler.rb index 3c04fcd..5b2dc7d 100644 --- a/lib/nickserver/request_handlers/fingerprint_handler.rb +++ b/lib/nickserver/request_handlers/fingerprint_handler.rb @@ -1,27 +1,30 @@ +require 'nickserver/request_handlers/base' require 'nickserver/hkp/source' require 'nickserver/error_response' module Nickserver module RequestHandlers - class FingerprintHandler + class FingerprintHandler < Base - def call(request) - return unless request.fingerprint - handle_request(request) - end - - protected - - def handle_request(request) - fingerprint = request.fingerprint + def handle + return unless fingerprint if fingerprint.length == 40 && !fingerprint[/\H/] - source = Nickserver::Hkp::Source.new source.get_key_by_fingerprint(fingerprint) else ErrorResponse.new('Fingerprint invalid: ' + fingerprint) end end + protected + + def fingerprint + request.fingerprint + end + + def source + Nickserver::Hkp::Source.new + end + end end end diff --git a/lib/nickserver/request_handlers/hkp_email_handler.rb b/lib/nickserver/request_handlers/hkp_email_handler.rb index 799e138..2f73773 100644 --- a/lib/nickserver/request_handlers/hkp_email_handler.rb +++ b/lib/nickserver/request_handlers/hkp_email_handler.rb @@ -3,16 +3,18 @@ require 'nickserver/hkp/source' module Nickserver module RequestHandlers - class HkpEmailHandler + class HkpEmailHandler < Base - def call(request) - return unless request.email - email = EmailAddress.new(request.email) - source.query(email) + def handle + source.query(email) if request.email end protected + def email + @email ||= EmailAddress.new(request.email) + end + def source Nickserver::Hkp::Source.new end diff --git a/lib/nickserver/request_handlers/invalid_email_handler.rb b/lib/nickserver/request_handlers/invalid_email_handler.rb index eaf8156..bb98f65 100644 --- a/lib/nickserver/request_handlers/invalid_email_handler.rb +++ b/lib/nickserver/request_handlers/invalid_email_handler.rb @@ -1,15 +1,21 @@ +require 'nickserver/request_handlers/base' require 'nickserver/email_address' require 'nickserver/error_response' module Nickserver module RequestHandlers - class InvalidEmailHandler - def call(request) + class InvalidEmailHandler < Base + + def handle return unless request.email - email = EmailAddress.new(request.email) ErrorResponse.new("Not a valid address") if email.invalid? end + protected + + def email + @email ||= EmailAddress.new(request.email) + end end end end diff --git a/lib/nickserver/request_handlers/local_email_handler.rb b/lib/nickserver/request_handlers/local_email_handler.rb index 9e8ed48..9165ef2 100644 --- a/lib/nickserver/request_handlers/local_email_handler.rb +++ b/lib/nickserver/request_handlers/local_email_handler.rb @@ -1,21 +1,25 @@ +require 'nickserver/request_handlers/base' require 'nickserver/email_address' -require 'nickserver/error_response' require 'nickserver/couch_db/source' module Nickserver module RequestHandlers - class LocalEmailHandler + class LocalEmailHandler < Base - def call(request) - return nil unless request.email - domain = Config.domain || request.domain - email = EmailAddress.new(request.email) - return nil unless email.domain?(domain) - source.query email + def handle + source.query(email) if request.email && email.domain?(domain) end protected + def domain + Config.domain || request.domain + end + + def email + @email ||= EmailAddress.new(request.email) + end + def source Nickserver::CouchDB::Source.new end diff --git a/test/unit/request_handlers/local_email_handler_test.rb b/test/unit/request_handlers/local_email_handler_test.rb index 1bfe264..1645451 100644 --- a/test/unit/request_handlers/local_email_handler_test.rb +++ b/test/unit/request_handlers/local_email_handler_test.rb @@ -26,7 +26,7 @@ class LocalEmailHandlerTest < MiniTest::Test protected def handler - Nickserver::RequestHandlers::LocalEmailHandler.new + Nickserver::RequestHandlers::LocalEmailHandler end def source_class |