redirect 0.1.0 → 0.3.0
Sign up to get free protection for your applications and to get access to all the features.
- data/History.txt +13 -0
- data/Manifest.txt +1 -0
- data/README.txt +29 -9
- data/bin/redirect_app +21 -0
- data/example.rb +2 -1
- data/lib/redirect.rb +7 -3
- data/spec/rack_spec.rb +7 -0
- metadata +6 -5
data/History.txt
CHANGED
@@ -1,3 +1,16 @@
|
|
1
|
+
=== 0.3.0 / 2009-02-24
|
2
|
+
|
3
|
+
* 1 major enhancement
|
4
|
+
|
5
|
+
* Added reuse of match patterns
|
6
|
+
[/old\/(.*)/, '/new/$1'] # /old/2008/02/01 will be redirected to /new/2008/02/01
|
7
|
+
|
8
|
+
=== 0.2.0 / 2009-02-22
|
9
|
+
|
10
|
+
* 1 major enhancement
|
11
|
+
|
12
|
+
* Added a generator method
|
13
|
+
|
1
14
|
=== 0.1.0 / 2009-02-22
|
2
15
|
|
3
16
|
* 1 major enhancement
|
data/Manifest.txt
CHANGED
data/README.txt
CHANGED
@@ -4,19 +4,39 @@ http://github.com/p8/redirect/tree/master
|
|
4
4
|
|
5
5
|
== DESCRIPTION:
|
6
6
|
|
7
|
-
Redirect is a simple Ruby redirect DSL build on Rack
|
7
|
+
Redirect is a simple Ruby redirect DSL build on Rack.
|
8
|
+
It's like a simple Ruby mod_rewrite, so you can write and test your redirects in Ruby.
|
8
9
|
|
9
|
-
==
|
10
|
+
== GETTING STARTED:
|
10
11
|
|
11
|
-
|
12
|
+
From the commandline run:
|
13
|
+
|
14
|
+
redirect_app PROJECT_NAME
|
15
|
+
|
16
|
+
This create a directory PROJECT_NAME with the required files.
|
17
|
+
PROJECT_NAME.rb contains the main logic.
|
18
|
+
|
19
|
+
To locally run the app you can do:
|
20
|
+
|
21
|
+
ruby PROJECT_NAME.rb
|
22
|
+
|
23
|
+
== REDIRECTS:
|
24
|
+
|
25
|
+
The first one is evaluated first, then the next one, etc..
|
12
26
|
|
13
|
-
require 'redirect'
|
14
27
|
redirect ['/catch_url', '/redirect_url'],
|
15
|
-
['/catch_url2', '/redirect_url2'
|
16
|
-
|
17
|
-
|
18
|
-
|
19
|
-
|
28
|
+
['/catch_url2', '/redirect_url2']
|
29
|
+
|
30
|
+
The catch_url can be a regular expression:
|
31
|
+
|
32
|
+
['^/some_regexp', '/all']
|
33
|
+
[/old\/(.*)/, '/new/$1'] # /old/2008/02/01 will be redirected to /new/2008/02/01
|
34
|
+
|
35
|
+
You can pass extra options.
|
36
|
+
:code # Overwrite the http code (defaults is 301) in the options
|
37
|
+
:name # named redirects are public so they'll appear in you sitemap
|
38
|
+
|
39
|
+
['/catch_url', '/redirect_url', {:code => 307, :name => 'redirect link'}]
|
20
40
|
|
21
41
|
The default redirect code can be changed:
|
22
42
|
|
data/bin/redirect_app
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
require 'fileutils'
|
3
|
+
project = ARGV.first
|
4
|
+
FileUtils.mkdir_p(project, :verbose => false) unless File.directory?(project)
|
5
|
+
|
6
|
+
# create application
|
7
|
+
open("#{project}/#{project}.rb", "w") do |f|
|
8
|
+
f.puts %(require 'rubygems'\n) +
|
9
|
+
%(require 'redirect'\n\n) +
|
10
|
+
%(redirect ['/old_url', '/new_url', {:code => 307, :name => 'test'}],\n) +
|
11
|
+
%( ['^/some_regexp', '/all'])
|
12
|
+
end
|
13
|
+
|
14
|
+
# create config
|
15
|
+
open("#{project}/config.ru", "w") do |f|
|
16
|
+
f.puts %(require 'rubygems'\n) +
|
17
|
+
%(require 'redirect'\n\n) +
|
18
|
+
%(Redirect.autorun = false\n\n) +
|
19
|
+
%(require '#{project}.rb'\n\n) +
|
20
|
+
%(run Redirect.app)
|
21
|
+
end
|
data/example.rb
CHANGED
data/lib/redirect.rb
CHANGED
@@ -5,7 +5,7 @@ require 'rack/request'
|
|
5
5
|
require 'rack/response'
|
6
6
|
|
7
7
|
module Redirect
|
8
|
-
VERSION = '0.
|
8
|
+
VERSION = '0.3.0'
|
9
9
|
|
10
10
|
def self.default_code= default_code
|
11
11
|
@default_code = default_code
|
@@ -75,9 +75,13 @@ module Rack
|
|
75
75
|
end
|
76
76
|
@redirects.each do |r|
|
77
77
|
if req.fullpath.match(r.catch_url)
|
78
|
+
redirect_url = r.redirect_url
|
79
|
+
if $1
|
80
|
+
redirect_url.gsub!('$1', $1)
|
81
|
+
end
|
78
82
|
puts "Match found for #{r.catch_url}."
|
79
|
-
puts "Redirecting to #{
|
80
|
-
return [r.code, {"Location" =>
|
83
|
+
puts "Redirecting to #{redirect_url}"
|
84
|
+
return [r.code, {"Location" => redirect_url, "Content-Type" => "text/html"}, "Redirecting to: #{redirect_url}"]
|
81
85
|
end
|
82
86
|
end
|
83
87
|
[404, {"Content-Type" => "text/html"}, "not found"]
|
data/spec/rack_spec.rb
CHANGED
@@ -64,6 +64,13 @@ describe "Rack::Redirect" do
|
|
64
64
|
res.body.should == 'Redirecting to: /new'
|
65
65
|
end
|
66
66
|
|
67
|
+
it "should redirect for a regular_expression with rewrite" do
|
68
|
+
@app = Rack::Redirect.new([/old\/(.*)/, '/new/$1'])
|
69
|
+
res = Rack::MockRequest.new(@app).get('/old/2008/02/14')
|
70
|
+
res.headers.should == { 'Location' => '/new/2008/02/14', 'Content-Type' => 'text/html' }
|
71
|
+
res.body.should == 'Redirecting to: /new/2008/02/14'
|
72
|
+
end
|
73
|
+
|
67
74
|
it "should allow multiple redirects" do
|
68
75
|
@app = Rack::Redirect.new(['^/old3', '/new'], ['^/old2', '/new2'])
|
69
76
|
res = Rack::MockRequest.new(@app).get('/old2008')
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: redirect
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 0.3.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Petrik de Heus
|
@@ -9,7 +9,7 @@ autorequire:
|
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
11
|
|
12
|
-
date: 2009-02-
|
12
|
+
date: 2009-02-24 00:00:00 +01:00
|
13
13
|
default_executable:
|
14
14
|
dependencies:
|
15
15
|
- !ruby/object:Gem::Dependency
|
@@ -22,11 +22,11 @@ dependencies:
|
|
22
22
|
- !ruby/object:Gem::Version
|
23
23
|
version: 1.8.3
|
24
24
|
version:
|
25
|
-
description: Redirect is a simple Ruby redirect DSL build on Rack
|
25
|
+
description: Redirect is a simple Ruby redirect DSL build on Rack. It's like a simple Ruby mod_rewrite, so you can write and test your redirects in Ruby.
|
26
26
|
email:
|
27
27
|
- FIX@example.com
|
28
|
-
executables:
|
29
|
-
|
28
|
+
executables:
|
29
|
+
- redirect_app
|
30
30
|
extensions: []
|
31
31
|
|
32
32
|
extra_rdoc_files:
|
@@ -38,6 +38,7 @@ files:
|
|
38
38
|
- Manifest.txt
|
39
39
|
- README.txt
|
40
40
|
- Rakefile
|
41
|
+
- bin/redirect_app
|
41
42
|
- example.rb
|
42
43
|
- example.ru
|
43
44
|
- lib/redirect.rb
|