rack-rewrite 0.1.1 → 0.1.2

Sign up to get free protection for your applications and to get access to all the features.
data/README.rdoc CHANGED
@@ -14,8 +14,8 @@ that hard-earned reputation. By writing rewrite rules that issue 301's for
14
14
  old URL's, we can "transfer" that google ranking to the new site. An example
15
15
  rule might look like:
16
16
 
17
- rewrite '/contact-us.php', '/contact-us'
18
- rewrite '/wiki/John_Trupiano', '/john'
17
+ r301 '/contact-us.php', '/contact-us'
18
+ r301 '/wiki/John_Trupiano', '/john'
19
19
 
20
20
  === Retiring old routes
21
21
 
@@ -83,7 +83,20 @@ however, is that these actually short-circuit the rack stack and send back
83
83
  Recall that rules are interpreted from top to bottom. So you can install
84
84
  "default" rewrite rules if you like. [2] is a sample default rule that
85
85
  will redirect all other requests to the wiki to a google search.
86
+
87
+ == Tips
88
+
89
+ === Keeping your querystring
90
+
91
+ When rewriting a URL, you may want to keep your querystring in tact (for
92
+ example if you're tracking traffic sources). You will need to include a
93
+ capture group and substitution pattern in your rewrite rule to achieve this.
94
+
95
+ rewrite %r{/wiki/John_Trupiano(\?.*)?}, '/john$1'
86
96
 
97
+ This rule will store the querystring in a capture group (via '(?.*)' ) and
98
+ will substitute the querystring back into the rewritten URL (via $1).
99
+
87
100
  == Copyright
88
101
 
89
102
  Copyright (c) 2009 John Trupiano. See LICENSE for details.
data/Rakefile CHANGED
@@ -10,7 +10,7 @@ begin
10
10
  gem.email = "jtrupiano@gmail.com"
11
11
  gem.homepage = "http://github.com/jtrupiano/rack-rewrite"
12
12
  gem.authors = ["John Trupiano"]
13
- gem.rubyforge_project = "rack-rewrite"
13
+ gem.rubyforge_project = "johntrupiano"
14
14
  gem.add_development_dependency "shoulda"
15
15
  # gem is a Gem::Specification... see http://www.rubygems.org/read/chapter/20 for additional settings
16
16
  end
data/VERSION CHANGED
@@ -1 +1 @@
1
- 0.1.1
1
+ 0.1.2
data/lib/rack-rewrite.rb CHANGED
@@ -23,7 +23,7 @@ module Rack
23
23
 
24
24
  private
25
25
  def find_first_matching_rule(env) #:nodoc:
26
- @rule_set.rules.detect { |rule| rule.matches?(env['PATH_INFO']) }
26
+ @rule_set.rules.detect { |rule| rule.matches?(env['REQUEST_URI']) }
27
27
  end
28
28
  end
29
29
  end
@@ -10,9 +10,9 @@ module Rack
10
10
  # We're explicitly defining private functions for our DSL rather than
11
11
  # using method_missing
12
12
 
13
- # Creates a rewrite rule that will simply rewrite the REQUEST_URI and
14
- # PATH_INFO headers of the Rack environment. The user's browser
15
- # will continue to show the initially requested URL.
13
+ # Creates a rewrite rule that will simply rewrite the REQUEST_URI,
14
+ # PATH_INFO, and QUERYSTRING headers of the Rack environment. The
15
+ # user's browser will continue to show the initially requested URL.
16
16
  #
17
17
  # rewrite '/wiki/John_Trupiano', '/john'
18
18
  # rewrite %r{/wiki/(\w+)_\w+}, '/$1'
@@ -55,10 +55,10 @@ module Rack
55
55
  end
56
56
  end
57
57
 
58
- # Either (a) return a Rack response (short-circuting the Rack stack), or
58
+ # Either (a) return a Rack response (short-circuiting the Rack stack), or
59
59
  # (b) alter env as necessary and return true
60
60
  def apply!(env) #:nodoc:
61
- interpreted_to = self.send(:interpret_to, env['PATH_INFO'])
61
+ interpreted_to = self.send(:interpret_to, env['REQUEST_URI'])
62
62
  case self.rule_type
63
63
  when :r301
64
64
  [301, {'Location' => interpreted_to}, ['Redirecting...']]
@@ -66,10 +66,17 @@ module Rack
66
66
  [302, {'Location' => interpreted_to}, ['Redirecting...']]
67
67
  when :rewrite
68
68
  # return [200, {}, {:content => env.inspect}]
69
- env['PATH_INFO'] = env['REQUEST_URI'] = interpreted_to
69
+ env['REQUEST_URI'] = interpreted_to
70
+ if q_index = interpreted_to.index('?')
71
+ env['PATH_INFO'] = interpreted_to[0..q_index-1]
72
+ env['QUERYSTRING'] = interpreted_to[q_index+1..interpreted_to.size-1]
73
+ else
74
+ env['PATH_INFO'] = interpreted_to
75
+ env['QUERYSTRING'] = ''
76
+ end
70
77
  true
71
78
  else
72
- raise Exception.new("Unsupported rule: #{rule.rule_type}")
79
+ raise Exception.new("Unsupported rule: #{self.rule_type}")
73
80
  end
74
81
  end
75
82
 
data/rack-rewrite.gemspec CHANGED
@@ -5,11 +5,11 @@
5
5
 
6
6
  Gem::Specification.new do |s|
7
7
  s.name = %q{rack-rewrite}
8
- s.version = "0.1.1"
8
+ s.version = "0.1.2"
9
9
 
10
10
  s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
11
11
  s.authors = ["John Trupiano"]
12
- s.date = %q{2009-10-12}
12
+ s.date = %q{2009-10-13}
13
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
14
  s.email = %q{jtrupiano@gmail.com}
15
15
  s.extra_rdoc_files = [
@@ -33,7 +33,7 @@ Gem::Specification.new do |s|
33
33
  s.homepage = %q{http://github.com/jtrupiano/rack-rewrite}
34
34
  s.rdoc_options = ["--charset=UTF-8"]
35
35
  s.require_paths = ["lib"]
36
- s.rubyforge_project = %q{rack-rewrite}
36
+ s.rubyforge_project = %q{johntrupiano}
37
37
  s.rubygems_version = %q{1.3.5}
38
38
  s.summary = %q{A rack middleware for enforcing rewrite rules}
39
39
  s.test_files = [
@@ -3,7 +3,7 @@ require 'test_helper'
3
3
  class RackRewriteTest < Test::Unit::TestCase
4
4
 
5
5
  def call_args(overrides={})
6
- {'PATH_INFO' => '/wiki/Yair_Flicker'}.merge(overrides)
6
+ {'REQUEST_URI' => '/wiki/Yair_Flicker', 'PATH_INFO' => '/wiki/Yair_Flicker', 'QUERYSTRING' => ''}.merge(overrides)
7
7
  end
8
8
 
9
9
  def self.should_not_halt
@@ -42,7 +42,7 @@ class RackRewriteTest < Test::Unit::TestCase
42
42
 
43
43
  context 'Given an app' do
44
44
  setup do
45
- @app = Class.new { def call; true; end }.new
45
+ @app = Class.new { def call(app); true; end }.new
46
46
  end
47
47
 
48
48
  context 'when no rewrite rule matches' do
@@ -79,10 +79,24 @@ class RackRewriteTest < Test::Unit::TestCase
79
79
  end
80
80
  }
81
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)
82
+
83
+ context 'the env' do
84
+ setup do
85
+ @initial_args = call_args.dup
86
+ @rack.call(@initial_args)
87
+ end
88
+
89
+ should "set PATH_INFO to '/john'" do
90
+ assert_equal '/john', @initial_args['PATH_INFO']
91
+ end
92
+ should "set REQUEST_URI to '/john'" do
93
+ assert_equal '/john', @initial_args['REQUEST_URI']
94
+ end
95
+ should "set QUERYSTRING to ''" do
96
+ assert_equal '', @initial_args['QUERYSTRING']
97
+ end
85
98
  end
86
99
  end
100
+
87
101
  end
88
102
  end
data/test/rule_test.rb CHANGED
@@ -8,10 +8,33 @@ class RuleTest < Test::Unit::TestCase
8
8
  env = {'PATH_INFO' => '/abc'}
9
9
  assert_equal rule.send(:interpret_to, '/abc'), rule.apply!(env)[1]['Location']
10
10
  end
11
+
12
+ should 'keep the QUERYSTRING when a 301 rule matches a URL with a querystring' do
13
+ rule = Rack::Rewrite::Rule.new(:r301, %r{/john(.*)}, '/yair$1')
14
+ env = {'REQUEST_URI' => '/john?show_bio=1', 'PATH_INFO' => '/john', 'QUERYSTRING' => 'show_bio=1'}
15
+ assert_equal '/yair?show_bio=1', rule.apply!(env)[1]['Location']
16
+ end
17
+
18
+ should 'keep the QUERYSTRING when a rewrite rule that requires a querystring matches a URL with a querystring' do
19
+ rule = Rack::Rewrite::Rule.new(:rewrite, %r{/john(\?.*)}, '/yair$1')
20
+ env = {'REQUEST_URI' => '/john?show_bio=1', 'PATH_INFO' => '/john', 'QUERYSTRING' => 'show_bio=1'}
21
+ rule.apply!(env)
22
+ assert_equal '/yair', env['PATH_INFO']
23
+ assert_equal 'show_bio=1', env['QUERYSTRING']
24
+ assert_equal '/yair?show_bio=1', env['REQUEST_URI']
25
+ end
26
+
27
+ should 'update the QUERYSTRING when a rewrite rule changes its value' do
28
+ rule = Rack::Rewrite::Rule.new(:rewrite, %r{/(\w+)\?show_bio=(\d)}, '/$1?bio=$2')
29
+ env = {'REQUEST_URI' => '/john?show_bio=1', 'PATH_INFO' => '/john', 'QUERYSTRING' => 'show_bio=1'}
30
+ rule.apply!(env)
31
+ assert_equal '/john', env['PATH_INFO']
32
+ assert_equal 'bio=1', env['QUERYSTRING']
33
+ assert_equal '/john?bio=1', env['REQUEST_URI']
34
+ end
11
35
  end
12
36
 
13
37
  context 'Rule#matches' do
14
-
15
38
  context 'Given any rule with a "from" string of /features' do
16
39
  setup do
17
40
  @rule = Rack::Rewrite::Rule.new(:rewrite, '/features', '/facial_features')
@@ -58,7 +81,6 @@ class RuleTest < Test::Unit::TestCase
58
81
  end
59
82
 
60
83
  context 'Rule#interpret_to' do
61
-
62
84
  should 'return #to when #from is a string' do
63
85
  rule = Rack::Rewrite::Rule.new(:rewrite, '/abc', '/def')
64
86
  assert_equal '/def', rule.send(:interpret_to, '/abc')
@@ -80,5 +102,4 @@ class RuleTest < Test::Unit::TestCase
80
102
  assert_equal 'jihgfedcba', rule.send(:interpret_to, "abcdefghij")
81
103
  end
82
104
  end
83
-
84
105
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: rack-rewrite
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.1
4
+ version: 0.1.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - John Trupiano
@@ -9,7 +9,7 @@ autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
11
 
12
- date: 2009-10-12 00:00:00 -04:00
12
+ date: 2009-10-13 00:00:00 -04:00
13
13
  default_executable:
14
14
  dependencies:
15
15
  - !ruby/object:Gem::Dependency
@@ -67,7 +67,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
67
67
  version:
68
68
  requirements: []
69
69
 
70
- rubyforge_project: rack-rewrite
70
+ rubyforge_project: johntrupiano
71
71
  rubygems_version: 1.3.5
72
72
  signing_key:
73
73
  specification_version: 3