rack-rewrite 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
data/.document ADDED
@@ -0,0 +1,5 @@
1
+ README.rdoc
2
+ lib/**/*.rb
3
+ bin/*
4
+ features/**/*.feature
5
+ LICENSE
data/.gitignore ADDED
@@ -0,0 +1,5 @@
1
+ *.sw?
2
+ .DS_Store
3
+ coverage
4
+ rdoc
5
+ pkg
data/LICENSE ADDED
@@ -0,0 +1,20 @@
1
+ Copyright (c) 2009 John Trupiano
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining
4
+ a copy of this software and associated documentation files (the
5
+ "Software"), to deal in the Software without restriction, including
6
+ without limitation the rights to use, copy, modify, merge, publish,
7
+ distribute, sublicense, and/or sell copies of the Software, and to
8
+ permit persons to whom the Software is furnished to do so, subject to
9
+ the following conditions:
10
+
11
+ The above copyright notice and this permission notice shall be
12
+ included in all copies or substantial portions of the Software.
13
+
14
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
17
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
18
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
19
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
20
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.rdoc ADDED
@@ -0,0 +1,21 @@
1
+ = rack-rewrite
2
+
3
+ A rack middleware for enforcing rewrite rules. In many cases you can get away with rack-rewrite
4
+ instead of writing Apache mod_rewrite rules.
5
+
6
+ == Usage in a rails app
7
+ config.gem 'rack-rewrite', '~> 0.1.0'
8
+ config.middleware.insert_before(Rack::Lock, Rack::Rewrite) do
9
+ rewrite '/wiki/John_Trupiano', '/john'
10
+ r301 '/wiki/Yair_Flicker', '/yair'
11
+ r302 '/wiki/Greg_Jastrab', '/greg'
12
+ end
13
+
14
+ == Notes
15
+
16
+ * Haven't tested full paths for :rewrite, :r301, :r302
17
+ * No regular expression support yet
18
+
19
+ == Copyright
20
+
21
+ Copyright (c) 2009 John Trupiano. See LICENSE for details.
data/Rakefile ADDED
@@ -0,0 +1,61 @@
1
+ require 'rubygems'
2
+ require 'rake'
3
+
4
+ begin
5
+ require 'jeweler'
6
+ Jeweler::Tasks.new do |gem|
7
+ gem.name = "rack-rewrite"
8
+ gem.summary = %Q{A rack middleware for enforcing rewrite rules}
9
+ gem.description = %Q{A rack middleware for enforcing rewrite rules. In many cases you can get away with rack-rewrite instead of writing Apache mod_rewrite rules.}
10
+ gem.email = "jtrupiano@gmail.com"
11
+ gem.homepage = "http://github.com/jtrupiano/rack-rewrite"
12
+ gem.authors = ["John Trupiano"]
13
+ gem.rubyforge_project = "rack-rewrite"
14
+ gem.add_development_dependency "shoulda"
15
+ # gem is a Gem::Specification... see http://www.rubygems.org/read/chapter/20 for additional settings
16
+ end
17
+ Jeweler::GemcutterTasks.new
18
+ Jeweler::RubyforgeTasks.new do |rubyforge|
19
+ rubyforge.doc_task = "rdoc"
20
+ end
21
+ rescue LoadError
22
+ puts "Jeweler (or a dependency) not available. Install it with: sudo gem install jeweler"
23
+ end
24
+
25
+ require 'rake/testtask'
26
+ Rake::TestTask.new(:test) do |test|
27
+ test.libs << 'lib' << 'test'
28
+ test.pattern = 'test/**/*_test.rb'
29
+ test.verbose = true
30
+ end
31
+
32
+ begin
33
+ require 'rcov/rcovtask'
34
+ Rcov::RcovTask.new do |test|
35
+ test.libs << 'test'
36
+ test.pattern = 'test/**/*_test.rb'
37
+ test.verbose = true
38
+ end
39
+ rescue LoadError
40
+ task :rcov do
41
+ abort "RCov is not available. In order to run rcov, you must: sudo gem install spicycode-rcov"
42
+ end
43
+ end
44
+
45
+ task :test => :check_dependencies
46
+
47
+ task :default => :test
48
+
49
+ require 'rake/rdoctask'
50
+ Rake::RDocTask.new do |rdoc|
51
+ if File.exist?('VERSION')
52
+ version = File.read('VERSION')
53
+ else
54
+ version = ""
55
+ end
56
+
57
+ rdoc.rdoc_dir = 'rdoc'
58
+ rdoc.title = "rack-rewrite #{version}"
59
+ rdoc.rdoc_files.include('README*')
60
+ rdoc.rdoc_files.include('lib/**/*.rb')
61
+ end
data/VERSION ADDED
@@ -0,0 +1 @@
1
+ 0.1.0
@@ -0,0 +1,55 @@
1
+ module Rack
2
+ class Rewrite
3
+ def initialize(app, &rule_block)
4
+ @app = app
5
+ @rule_set = RuleSet.new
6
+ @rule_set.instance_eval(&rule_block) if block_given?
7
+ end
8
+
9
+ def call(env)
10
+ if matched_rule = find_rule(env)
11
+ apply(matched_rule, env)
12
+ else
13
+ @app.call(env)
14
+ end
15
+ end
16
+
17
+ # This logic needs to be pushed into Rule subclasses
18
+ def apply(rule, env)
19
+ case rule[0]
20
+ when :r301
21
+ [301, {'Location' => rule[2]}, ['Redirecting...']]
22
+ when :r302
23
+ [302, {'Location' => rule[2]}, ['Redirecting...']]
24
+ when :rewrite
25
+ # return [200, {}, {:content => env.inspect}]
26
+ env['PATH_INFO'] = env['REQUEST_URI'] = rule[2]
27
+ @app.call(env)
28
+ else
29
+ raise Exception.new("Unsupported rule: #{rule[0]}")
30
+ end
31
+ end
32
+
33
+ # This will probably have to change as rule matching gets more complicated
34
+ def find_rule(env)
35
+ @rule_set.rules.detect { |rule| rule[1] == env['PATH_INFO'] }
36
+ end
37
+
38
+ class RuleSet
39
+
40
+ attr_reader :rules
41
+ def initialize
42
+ @rules = []
43
+ end
44
+
45
+ private
46
+ # We're explicitly defining the functions for our DSL rather than using
47
+ # method_missing
48
+ %w(rewrite r301 r302).each do |meth|
49
+ define_method(meth) do |from, to|
50
+ @rules << [meth.to_sym, from, to]
51
+ end
52
+ end
53
+ end
54
+ end
55
+ end
@@ -0,0 +1,54 @@
1
+ # Generated by jeweler
2
+ # DO NOT EDIT THIS FILE
3
+ # Instead, edit Jeweler::Tasks in Rakefile, and run `rake gemspec`
4
+ # -*- encoding: utf-8 -*-
5
+
6
+ Gem::Specification.new do |s|
7
+ s.name = %q{rack-rewrite}
8
+ s.version = "0.1.0"
9
+
10
+ s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
11
+ s.authors = ["John Trupiano"]
12
+ s.date = %q{2009-10-10}
13
+ s.description = %q{A rack middleware for enforcing rewrite rules. In many cases you can get away with rack-rewrite instead of writing Apache mod_rewrite rules.}
14
+ s.email = %q{jtrupiano@gmail.com}
15
+ s.extra_rdoc_files = [
16
+ "LICENSE",
17
+ "README.rdoc"
18
+ ]
19
+ s.files = [
20
+ ".document",
21
+ ".gitignore",
22
+ "LICENSE",
23
+ "README.rdoc",
24
+ "Rakefile",
25
+ "VERSION",
26
+ "lib/rack-rewrite.rb",
27
+ "rack-rewrite.gemspec",
28
+ "test/rack-rewrite_test.rb",
29
+ "test/test_helper.rb"
30
+ ]
31
+ s.homepage = %q{http://github.com/jtrupiano/rack-rewrite}
32
+ s.rdoc_options = ["--charset=UTF-8"]
33
+ s.require_paths = ["lib"]
34
+ s.rubyforge_project = %q{rack-rewrite}
35
+ s.rubygems_version = %q{1.3.5}
36
+ s.summary = %q{A rack middleware for enforcing rewrite rules}
37
+ s.test_files = [
38
+ "test/rack-rewrite_test.rb",
39
+ "test/test_helper.rb"
40
+ ]
41
+
42
+ if s.respond_to? :specification_version then
43
+ current_version = Gem::Specification::CURRENT_SPECIFICATION_VERSION
44
+ s.specification_version = 3
45
+
46
+ if Gem::Version.new(Gem::RubyGemsVersion) >= Gem::Version.new('1.2.0') then
47
+ s.add_development_dependency(%q<shoulda>, [">= 0"])
48
+ else
49
+ s.add_dependency(%q<shoulda>, [">= 0"])
50
+ end
51
+ else
52
+ s.add_dependency(%q<shoulda>, [">= 0"])
53
+ end
54
+ end
@@ -0,0 +1,88 @@
1
+ require 'test_helper'
2
+
3
+ class RackRewriteTest < Test::Unit::TestCase
4
+
5
+ def call_args(overrides={})
6
+ {'PATH_INFO' => '/wiki/Yair_Flicker'}.merge(overrides)
7
+ end
8
+
9
+ def self.should_not_halt
10
+ should "not halt the rack chain" do
11
+ @app.expects(:call).once
12
+ @rack.call(call_args)
13
+ end
14
+ end
15
+
16
+ def self.should_be_a_rack_response
17
+ should 'be a rack a response' do
18
+ ret = @rack.call(call_args)
19
+ assert ret.is_a?(Array), 'return value is not a valid rack response'
20
+ assert_equal 3, ret.size, 'should have 3 arguments'
21
+ end
22
+ end
23
+
24
+ def self.should_halt
25
+ should "should halt the rack chain" do
26
+ @app.expects(:call).never
27
+ @rack.call(call_args)
28
+ end
29
+ should_be_a_rack_response
30
+ end
31
+
32
+ def self.should_location_redirect_to(location, code)
33
+ should "respond with http status code #{code}" do
34
+ ret = @rack.call(call_args)
35
+ assert_equal code, ret[0]
36
+ end
37
+ should 'send a location header' do
38
+ ret = @rack.call(call_args)
39
+ assert_equal location, ret[1]['Location'], 'Location is incorrect'
40
+ end
41
+ end
42
+
43
+ context 'Given an app' do
44
+ setup do
45
+ @app = Class.new { def call; true; end }.new
46
+ end
47
+
48
+ context 'when no rewrite rule matches' do
49
+ setup {
50
+ @rack = Rack::Rewrite.new(@app)
51
+ }
52
+ should_not_halt
53
+ end
54
+
55
+ context 'when a 301 rule matches' do
56
+ setup {
57
+ @rack = Rack::Rewrite.new(@app) do
58
+ r301 '/wiki/Yair_Flicker', '/yair'
59
+ end
60
+ }
61
+ should_halt
62
+ should_location_redirect_to('/yair', 301)
63
+ end
64
+
65
+ context 'when a 302 rule matches' do
66
+ setup {
67
+ @rack = Rack::Rewrite.new(@app) do
68
+ r302 '/wiki/Yair_Flicker', '/yair'
69
+ end
70
+ }
71
+ should_halt
72
+ should_location_redirect_to('/yair', 302)
73
+ end
74
+
75
+ context 'when a rewrite rule matches' do
76
+ setup {
77
+ @rack = Rack::Rewrite.new(@app) do
78
+ rewrite '/wiki/Yair_Flicker', '/john'
79
+ end
80
+ }
81
+ should_not_halt
82
+ should "set PATH_INFO and REQUEST_URI to '/john'" do
83
+ @app.expects(:call).with(call_args.merge({'PATH_INFO' => '/john', 'REQUEST_URI' => '/john'})).once
84
+ @rack.call(call_args)
85
+ end
86
+ end
87
+ end
88
+ end
@@ -0,0 +1,13 @@
1
+ require 'rubygems'
2
+ require 'test/unit'
3
+ gem 'shoulda', '~> 2.10.2'
4
+ require 'shoulda'
5
+ gem 'mocha', '~> 0.9.7'
6
+ require 'mocha'
7
+
8
+ $LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))
9
+ $LOAD_PATH.unshift(File.dirname(__FILE__))
10
+ require 'rack-rewrite'
11
+
12
+ class Test::Unit::TestCase
13
+ end
metadata ADDED
@@ -0,0 +1,75 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: rack-rewrite
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - John Trupiano
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+
12
+ date: 2009-10-10 00:00:00 -04:00
13
+ default_executable:
14
+ dependencies:
15
+ - !ruby/object:Gem::Dependency
16
+ name: shoulda
17
+ type: :development
18
+ version_requirement:
19
+ version_requirements: !ruby/object:Gem::Requirement
20
+ requirements:
21
+ - - ">="
22
+ - !ruby/object:Gem::Version
23
+ version: "0"
24
+ version:
25
+ description: A rack middleware for enforcing rewrite rules. In many cases you can get away with rack-rewrite instead of writing Apache mod_rewrite rules.
26
+ email: jtrupiano@gmail.com
27
+ executables: []
28
+
29
+ extensions: []
30
+
31
+ extra_rdoc_files:
32
+ - LICENSE
33
+ - README.rdoc
34
+ files:
35
+ - .document
36
+ - .gitignore
37
+ - LICENSE
38
+ - README.rdoc
39
+ - Rakefile
40
+ - VERSION
41
+ - lib/rack-rewrite.rb
42
+ - rack-rewrite.gemspec
43
+ - test/rack-rewrite_test.rb
44
+ - test/test_helper.rb
45
+ has_rdoc: true
46
+ homepage: http://github.com/jtrupiano/rack-rewrite
47
+ licenses: []
48
+
49
+ post_install_message:
50
+ rdoc_options:
51
+ - --charset=UTF-8
52
+ require_paths:
53
+ - lib
54
+ required_ruby_version: !ruby/object:Gem::Requirement
55
+ requirements:
56
+ - - ">="
57
+ - !ruby/object:Gem::Version
58
+ version: "0"
59
+ version:
60
+ required_rubygems_version: !ruby/object:Gem::Requirement
61
+ requirements:
62
+ - - ">="
63
+ - !ruby/object:Gem::Version
64
+ version: "0"
65
+ version:
66
+ requirements: []
67
+
68
+ rubyforge_project: rack-rewrite
69
+ rubygems_version: 1.3.5
70
+ signing_key:
71
+ specification_version: 3
72
+ summary: A rack middleware for enforcing rewrite rules
73
+ test_files:
74
+ - test/rack-rewrite_test.rb
75
+ - test/test_helper.rb