Permalink
Browse files

Send those slashdotters away

  • Loading branch information...
1 parent 1e48349 commit e10fc63e9cb1bb59b0e53ed1c56e18f7b46298e2 @redinger committed Oct 16, 2009
Showing with 67 additions and 27 deletions.
  1. +24 −0 README.markdown
  2. +0 −18 README.rdoc
  3. +2 −3 Rakefile
  4. +26 −0 lib/no_slashdot.rb
  5. 0 lib/rack-noslashdot.rb
  6. +13 −4 test/rack-noslashdot_test.rb
  7. +2 −2 test/test_helper.rb
View
@@ -0,0 +1,24 @@
+# rack-noslashdot
+
+rack-noslashdot will redirect anyone who has been refered by slashdot (and doesn't mask their HTTP_REFERER header)
+
+# usage
+
+just
+
+ config.middleware.use "NoSlashdot", :redirect => 'http://slashdot.org'
+
+or let the default kick in
+
+ config.middleware.use "NoSlashdot"
+
+# license
+
+Copyright (c) 2009 Christopher Redinger. See LICENSE for details.
+
+# thanks
+This is just a play on Julio Cesar's NoIE middleware.
+http://github.com/juliocesar/rack-noie
+
+in response to Peter Cooper's tweet
+http://twitter.com/peterc/status/4926493168
View
@@ -1,18 +0,0 @@
-= rack-noslashdot
-
-Description goes here.
-
-== Note on Patches/Pull Requests
-
-* Fork the project.
-* Make your feature addition or bug fix.
-* Add tests for it. This is important so I don't break it in a
- future version unintentionally.
-* Commit, do not mess with rakefile, version, or history.
- (if you want to have your own version, that is fine but
- bump version in a commit by itself I can ignore when I pull)
-* Send me a pull request. Bonus points for topic branches.
-
-== Copyright
-
-Copyright (c) 2009 Christopher Redinger. See LICENSE for details.
View
@@ -5,12 +5,11 @@ begin
require 'jeweler'
Jeweler::Tasks.new do |gem|
gem.name = "rack-noslashdot"
- gem.summary = %Q{TODO: one-line summary of your gem}
- gem.description = %Q{TODO: longer description of your gem}
+ gem.summary = %Q{TODO: Redirect slashdot referers}
+ gem.description = %Q{TODO: Because sometimes you just want to be left alone}
gem.email = "redinger@gmail.com"
gem.homepage = "http://github.com/redinger/rack-noslashdot"
gem.authors = ["Christopher Redinger"]
- gem.add_development_dependency "thoughtbot-shoulda"
# gem is a Gem::Specification... see http://www.rubygems.org/read/chapter/20 for additional settings
end
rescue LoadError
View
@@ -0,0 +1,26 @@
+class NoSlashdot
+ def initialize(app, options = {})
+ @app = app
+ @options = options
+ @options[:redirect] ||= 'http://slashdot.org'
+ end
+
+ def call(env)
+ slashdot_sent_ya?(env) ? kick_it : @app.call(env)
+ end
+
+ private
+ def slashdot_sent_ya?(env)
+ if env['HTTP_REFERER']
+ is_slashdot?(env['HTTP_REFERER']) and @options[:redirect] != env['PATH_INFO']
+ end
+ end
+
+ def is_slashdot?(referer_string)
+ referer_string.match(/slashdot.org/) ? true : false
+ end
+
+ def kick_it
+ [301, {'Location' => @options[:redirect]}, 'Fail browser is fail']
+ end
+end
View
No changes.
@@ -1,7 +1,16 @@
require 'test_helper'
-class RackNoslashdotTest < Test::Unit::TestCase
- should "probably rename this file and start testing for real" do
- flunk "hey buddy, you should probably rename this file and start testing for real"
+class NoslashdotTest < Test::Unit::TestCase
+ include Rack::Test::Methods
+
+ def app
+ NoSlashdot.new(lambda { |env| [200, {}, "You've been slashdotted!"] }, :redirect => 'http://slashdot.org')
+ end
+
+ def test_redirects_to_where_it_should_if_slashdot
+ get '/', {}, "HTTP_REFERER" => 'www.slashdot.org'
+ assert last_response.redirect?
+ follow_redirect!
+ assert_equal 'http://slashdot.org/', last_request.url
end
-end
+end
View
@@ -1,10 +1,10 @@
require 'rubygems'
require 'test/unit'
-require 'shoulda'
+require 'rack/test'
$LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))
$LOAD_PATH.unshift(File.dirname(__FILE__))
-require 'rack-noslashdot'
+require 'no_slashdot'
class Test::Unit::TestCase
end

0 comments on commit e10fc63

Please sign in to comment.