From 415f7c9d24b4d6542cc693b5183cdf2cb7514bab Mon Sep 17 00:00:00 2001 From: Guillaume Dott Date: Wed, 27 May 2015 11:09:44 +0200 Subject: [PATCH] Add `ProjectHoneypot::Rack` class to DRY --- lib/project_honeypot.rb | 1 + lib/project_honeypot/rack.rb | 10 ++++++++++ lib/project_honeypot/rack/forbidden.rb | 27 +++++++++++--------------- lib/project_honeypot/rack/header.rb | 23 +++++++++------------- 4 files changed, 31 insertions(+), 30 deletions(-) create mode 100644 lib/project_honeypot/rack.rb diff --git a/lib/project_honeypot.rb b/lib/project_honeypot.rb index 42b2ee1..3da44bd 100644 --- a/lib/project_honeypot.rb +++ b/lib/project_honeypot.rb @@ -1,6 +1,7 @@ require 'net/dns' require "project_honeypot/url" require "project_honeypot/base" +require "project_honeypot/rack" require "project_honeypot/rack/header" require "project_honeypot/rack/forbidden" diff --git a/lib/project_honeypot/rack.rb b/lib/project_honeypot/rack.rb new file mode 100644 index 0000000..d995983 --- /dev/null +++ b/lib/project_honeypot/rack.rb @@ -0,0 +1,10 @@ +module ProjectHoneypot + class Rack + def initialize(app, options={}) + @app = app + + raise ArgumentError, 'Must specify an API key' unless options[:api_key] + ProjectHoneypot.api_key = options[:api_key] + end + end +end diff --git a/lib/project_honeypot/rack/forbidden.rb b/lib/project_honeypot/rack/forbidden.rb index a3ddf71..91a02a9 100644 --- a/lib/project_honeypot/rack/forbidden.rb +++ b/lib/project_honeypot/rack/forbidden.rb @@ -1,20 +1,15 @@ -module ProjectHoneypot::Rack - class Forbidden - def initialize(app, options={}) - @app = app +module ProjectHoneypot + class Rack + class Forbidden < Rack + def call(env) + request = ::Rack::Request.new(env) + url = ProjectHoneypot.lookup(request.ip) - raise ArgumentError, 'Must specify an API key' unless options[:api_key] - ProjectHoneypot.api_key = options[:api_key] - end - - def call(env) - request = ::Rack::Request.new(env) - url = ProjectHoneypot.lookup(request.ip) - - if url.safe? - @app.call(request.env) - else - [403, {"Content-Type" => "text/html"}, ["Forbidden"]] + if url.safe? + @app.call(request.env) + else + [403, {"Content-Type" => "text/html"}, ["Forbidden"]] + end end end end diff --git a/lib/project_honeypot/rack/header.rb b/lib/project_honeypot/rack/header.rb index d7b3744..8d7fad8 100644 --- a/lib/project_honeypot/rack/header.rb +++ b/lib/project_honeypot/rack/header.rb @@ -1,19 +1,14 @@ -module ProjectHoneypot::Rack - class Header - def initialize(app, options={}) - @app = app +module ProjectHoneypot + class Rack + class Header < Rack + def call(env) + request = ::Rack::Request.new(env) + url = ProjectHoneypot.lookup(request.ip) - raise ArgumentError, 'Must specify an API key' unless options[:api_key] - ProjectHoneypot.api_key = options[:api_key] - end + env['PROJECT_HONEYPOT_SAFE'] = url.safe? - def call(env) - request = ::Rack::Request.new(env) - url = ProjectHoneypot.lookup(request.ip) - - env['PROJECT_HONEYPOT_SAFE'] = url.safe? - - @app.call(request.env) + @app.call(request.env) + end end end end