Add `ProjectHoneypot::Rack` class to DRY

master
Guillaume Dott 2015-05-27 11:09:44 +02:00
parent 707b115428
commit 415f7c9d24
4 changed files with 31 additions and 30 deletions

View File

@ -1,6 +1,7 @@
require 'net/dns' require 'net/dns'
require "project_honeypot/url" require "project_honeypot/url"
require "project_honeypot/base" require "project_honeypot/base"
require "project_honeypot/rack"
require "project_honeypot/rack/header" require "project_honeypot/rack/header"
require "project_honeypot/rack/forbidden" require "project_honeypot/rack/forbidden"

View File

@ -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

View File

@ -1,20 +1,15 @@
module ProjectHoneypot::Rack module ProjectHoneypot
class Forbidden class Rack
def initialize(app, options={}) class Forbidden < Rack
@app = app def call(env)
request = ::Rack::Request.new(env)
url = ProjectHoneypot.lookup(request.ip)
raise ArgumentError, 'Must specify an API key' unless options[:api_key] if url.safe?
ProjectHoneypot.api_key = options[:api_key] @app.call(request.env)
end else
[403, {"Content-Type" => "text/html"}, ["Forbidden"]]
def call(env) end
request = ::Rack::Request.new(env)
url = ProjectHoneypot.lookup(request.ip)
if url.safe?
@app.call(request.env)
else
[403, {"Content-Type" => "text/html"}, ["Forbidden"]]
end end
end end
end end

View File

@ -1,19 +1,14 @@
module ProjectHoneypot::Rack module ProjectHoneypot
class Header class Rack
def initialize(app, options={}) class Header < Rack
@app = app def call(env)
request = ::Rack::Request.new(env)
url = ProjectHoneypot.lookup(request.ip)
raise ArgumentError, 'Must specify an API key' unless options[:api_key] env['PROJECT_HONEYPOT_SAFE'] = url.safe?
ProjectHoneypot.api_key = options[:api_key]
end
def call(env) @app.call(request.env)
request = ::Rack::Request.new(env) end
url = ProjectHoneypot.lookup(request.ip)
env['PROJECT_HONEYPOT_SAFE'] = url.safe?
@app.call(request.env)
end end
end end
end end