From 5a4ca4d44accee8066f54e1b0cec166c9f413fc1 Mon Sep 17 00:00:00 2001 From: Guillaume DOTT Date: Fri, 21 Dec 2012 16:13:03 +0100 Subject: [PATCH] Add rack middleware --- lib/project-honeypot.rb | 5 +++-- lib/project_honeypot/rack.rb | 19 +++++++++++++++++++ 2 files changed, 22 insertions(+), 2 deletions(-) create mode 100644 lib/project_honeypot/rack.rb diff --git a/lib/project-honeypot.rb b/lib/project-honeypot.rb index 7cf376b..971f376 100644 --- a/lib/project-honeypot.rb +++ b/lib/project-honeypot.rb @@ -1,6 +1,7 @@ require 'net/dns' -require File.dirname(__FILE__) + "/project_honeypot/url.rb" -require File.dirname(__FILE__) + "/project_honeypot/base.rb" +require "project_honeypot/url" +require "project_honeypot/base" +require "project_honeypot/rack" module ProjectHoneypot class << self diff --git a/lib/project_honeypot/rack.rb b/lib/project_honeypot/rack.rb new file mode 100644 index 0000000..ee002c4 --- /dev/null +++ b/lib/project_honeypot/rack.rb @@ -0,0 +1,19 @@ +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 + + def call(env) + request = ::Rack::Request.new(env) + url = ProjectHoneypot.lookup(request.ip) + + env['PROJECT_HONEYPOT_SAFE'] = url.safe? + + @app.call(request.env) + end + end +end