redirect 0.0.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- data/History.txt +5 -0
- data/Manifest.txt +8 -0
- data/README.txt +50 -0
- data/Rakefile +16 -0
- data/example.rb +4 -0
- data/lib/redirect.rb +87 -0
- data/spec/helper.rb +7 -0
- data/spec/rack_spec.rb +116 -0
- metadata +73 -0
data/History.txt
ADDED
data/Manifest.txt
ADDED
data/README.txt
ADDED
@@ -0,0 +1,50 @@
|
|
1
|
+
= Redirect
|
2
|
+
|
3
|
+
http://github.com/p8/redirect/tree/master
|
4
|
+
|
5
|
+
== DESCRIPTION:
|
6
|
+
|
7
|
+
Redirect is a simple Ruby redirect DSL build on Rack
|
8
|
+
|
9
|
+
== EXAMPLE
|
10
|
+
|
11
|
+
Create a file and pass an array to the redirect method:
|
12
|
+
|
13
|
+
require 'redirect'
|
14
|
+
redirect ['/catch_url', '/redirect_url'],
|
15
|
+
['/catch_url2', '/redirect_url2', {:code => 307, :name => 'redirect link'}],
|
16
|
+
['^/some_regexp', '/all']
|
17
|
+
|
18
|
+
The catch_url can be a regular expression.
|
19
|
+
You can overwrite the default 301 http code in the options.
|
20
|
+
|
21
|
+
The default redirect code can be changed:
|
22
|
+
|
23
|
+
Redirect.default_code = 307
|
24
|
+
|
25
|
+
A sitemap.xml is generated for all redirects that have a name.
|
26
|
+
|
27
|
+
== LICENSE:
|
28
|
+
|
29
|
+
(The MIT License)
|
30
|
+
|
31
|
+
Copyright (c) 2009 Petrik de Heus
|
32
|
+
|
33
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
34
|
+
a copy of this software and associated documentation files (the
|
35
|
+
'Software'), to deal in the Software without restriction, including
|
36
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
37
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
38
|
+
permit persons to whom the Software is furnished to do so, subject to
|
39
|
+
the following conditions:
|
40
|
+
|
41
|
+
The above copyright notice and this permission notice shall be
|
42
|
+
included in all copies or substantial portions of the Software.
|
43
|
+
|
44
|
+
THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND,
|
45
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
46
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
|
47
|
+
IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
|
48
|
+
CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
|
49
|
+
TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
|
50
|
+
SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/Rakefile
ADDED
@@ -0,0 +1,16 @@
|
|
1
|
+
# -*- ruby -*-
|
2
|
+
|
3
|
+
require 'rubygems'
|
4
|
+
require 'hoe'
|
5
|
+
require 'lib/redirect.rb'
|
6
|
+
|
7
|
+
Hoe.new('redirect', Redirect::VERSION) do |p|
|
8
|
+
p.developer('Petrik de Heus', 'FIX@example.com')
|
9
|
+
p.remote_rdoc_dir = '' # Release to root
|
10
|
+
end
|
11
|
+
#
|
12
|
+
# require 'metric_fu'
|
13
|
+
#
|
14
|
+
# MetricFu::Configuration.run do |config|
|
15
|
+
# config.coverage = { :test_files => ['test/**/test_*.rb'] }
|
16
|
+
# end
|
data/example.rb
ADDED
data/lib/redirect.rb
ADDED
@@ -0,0 +1,87 @@
|
|
1
|
+
require 'rubygems'
|
2
|
+
require 'rack'
|
3
|
+
require 'rack/showexceptions'
|
4
|
+
require 'rack/request'
|
5
|
+
require 'rack/response'
|
6
|
+
|
7
|
+
module Redirect
|
8
|
+
VERSION = '0.0.1'
|
9
|
+
|
10
|
+
class Data
|
11
|
+
attr_reader :catch_url, :redirect_url, :code, :name
|
12
|
+
def initialize(catch_url, redirect_url, options = {})
|
13
|
+
@catch_url = catch_url
|
14
|
+
@redirect_url = redirect_url
|
15
|
+
@code = options[:code] || Redirect.default_code
|
16
|
+
@name = options[:name]
|
17
|
+
end
|
18
|
+
end
|
19
|
+
|
20
|
+
def self.default_code= default_code
|
21
|
+
@default_code = default_code
|
22
|
+
end
|
23
|
+
|
24
|
+
def self.default_code
|
25
|
+
@default_code ||= 301
|
26
|
+
end
|
27
|
+
|
28
|
+
end
|
29
|
+
|
30
|
+
def redirect(*redirects)
|
31
|
+
|
32
|
+
app = Rack::Redirect.new(*redirects)
|
33
|
+
Rack::Handler::WEBrick.run \
|
34
|
+
Rack::ShowExceptions.new(Rack::Lint.new(app)),
|
35
|
+
:Port => 3000
|
36
|
+
run app
|
37
|
+
end
|
38
|
+
|
39
|
+
module Rack
|
40
|
+
|
41
|
+
class Redirect
|
42
|
+
attr_reader :redirects
|
43
|
+
def initialize(*redirects)
|
44
|
+
@redirects = redirects.collect do |r|
|
45
|
+
::Redirect::Data.new(*r)
|
46
|
+
end
|
47
|
+
end
|
48
|
+
|
49
|
+
def call(env)
|
50
|
+
req = Request.new(env)
|
51
|
+
if req.fullpath == '/sitemap.xml'
|
52
|
+
return [200, {"Content-Type" => "text/xml"}, sitemap(req.host)]
|
53
|
+
end
|
54
|
+
if req.fullpath == '/' && index
|
55
|
+
return [200, {"Content-Type" => "text/html"}, index]
|
56
|
+
end
|
57
|
+
@redirects.each do |r|
|
58
|
+
if req.fullpath.match(r.catch_url)
|
59
|
+
puts "Match found for #{r.catch_url}."
|
60
|
+
puts "Redirecting to #{r.redirect_url}"
|
61
|
+
return [r.code, {"Location" => r.redirect_url, "Content-Type" => "text/html"}, "Redirecting to: #{r.redirect_url}"]
|
62
|
+
end
|
63
|
+
end
|
64
|
+
[404, {"Content-Type" => "text/html"}, "not found"]
|
65
|
+
end
|
66
|
+
|
67
|
+
def sitemap(host)
|
68
|
+
%(<?xml version="1.0" encoding="UTF-8"?>\n) +
|
69
|
+
%(<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">\n) +
|
70
|
+
@redirects.select{|r| r.name }.collect { |r|
|
71
|
+
%(<url>\n) +
|
72
|
+
%(<loc>http://#{host}#{r.redirect_url}</loc>\n) +
|
73
|
+
%(</url>\n)}.join +
|
74
|
+
%(</urlset>\n)
|
75
|
+
end
|
76
|
+
|
77
|
+
def index
|
78
|
+
@index
|
79
|
+
end
|
80
|
+
|
81
|
+
def index= index
|
82
|
+
@index = index
|
83
|
+
end
|
84
|
+
|
85
|
+
end
|
86
|
+
|
87
|
+
end
|
data/spec/helper.rb
ADDED
data/spec/rack_spec.rb
ADDED
@@ -0,0 +1,116 @@
|
|
1
|
+
require File.dirname(__FILE__) + '/helper'
|
2
|
+
|
3
|
+
describe "Rack::Redirect" do
|
4
|
+
|
5
|
+
it "should show 404 not found with if no redirect exists" do
|
6
|
+
@app = Rack::Redirect.new()
|
7
|
+
res = Rack::MockRequest.new(@app).get("/test")
|
8
|
+
|
9
|
+
res.not_found?.should be_true
|
10
|
+
res["Content-Type"].should == "text/html"
|
11
|
+
res.body.should == "not found"
|
12
|
+
end
|
13
|
+
|
14
|
+
it "should show index if no redirect exists but index does" do
|
15
|
+
@app = Rack::Redirect.new()
|
16
|
+
@app.index = "index"
|
17
|
+
res = Rack::MockRequest.new(@app).get("/")
|
18
|
+
# res.not_found?.should be_true
|
19
|
+
res["Content-Type"].should == "text/html"
|
20
|
+
res.body.should == "index"
|
21
|
+
end
|
22
|
+
|
23
|
+
it "should redirect '/' if redirect exists" do
|
24
|
+
@app = Rack::Redirect.new(['/', '/test'])
|
25
|
+
res = Rack::MockRequest.new(@app).get('/')
|
26
|
+
res.headers.should == { 'Location' => '/test', 'Content-Type' => 'text/html' }
|
27
|
+
res.body.should == 'Redirecting to: /test'
|
28
|
+
end
|
29
|
+
|
30
|
+
it "should show sitemap.xml" do
|
31
|
+
@app = Rack::Redirect.new(['/', '/test', {:name => 'test'}])
|
32
|
+
res = Rack::MockRequest.new(@app).get('/sitemap.xml')
|
33
|
+
res.headers.should == {'Content-Type' => 'text/xml' }
|
34
|
+
res.body.should == %(<?xml version="1.0" encoding="UTF-8"?>\n) +
|
35
|
+
%(<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">\n) +
|
36
|
+
%(<url>\n) +
|
37
|
+
%(<loc>http://example.org/test</loc>\n) +
|
38
|
+
%(</url>\n) +
|
39
|
+
%(</urlset>\n)
|
40
|
+
end
|
41
|
+
|
42
|
+
it "should show sitemap.xml only for named redirects" do
|
43
|
+
@app = Rack::Redirect.new(['/', '/test', {:name => 'test'}], ['/a', '/hidden'])
|
44
|
+
res = Rack::MockRequest.new(@app).get('/sitemap.xml')
|
45
|
+
res.body.should == %(<?xml version="1.0" encoding="UTF-8"?>\n) +
|
46
|
+
%(<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">\n) +
|
47
|
+
%(<url>\n) +
|
48
|
+
%(<loc>http://example.org/test</loc>\n) +
|
49
|
+
%(</url>\n) +
|
50
|
+
%(</urlset>\n)
|
51
|
+
end
|
52
|
+
|
53
|
+
it "should redirect if redirect exists" do
|
54
|
+
@app = Rack::Redirect.new(['/old/2008', '/2008-new'])
|
55
|
+
res = Rack::MockRequest.new(@app).get('/old/2008')
|
56
|
+
res.headers.should == { 'Location' => '/2008-new', 'Content-Type' => 'text/html' }
|
57
|
+
res.body.should == 'Redirecting to: /2008-new'
|
58
|
+
end
|
59
|
+
|
60
|
+
it "should redirect for a regular_expression" do
|
61
|
+
@app = Rack::Redirect.new(['^/old', '/new'])
|
62
|
+
res = Rack::MockRequest.new(@app).get('/old/2008')
|
63
|
+
res.headers.should == { 'Location' => '/new', 'Content-Type' => 'text/html' }
|
64
|
+
res.body.should == 'Redirecting to: /new'
|
65
|
+
end
|
66
|
+
|
67
|
+
it "should allow multiple redirects" do
|
68
|
+
@app = Rack::Redirect.new(['^/old3', '/new'], ['^/old2', '/new2'])
|
69
|
+
res = Rack::MockRequest.new(@app).get('/old2008')
|
70
|
+
res.headers.should == { 'Location' => '/new2', 'Content-Type' => 'text/html' }
|
71
|
+
res.body.should == 'Redirecting to: /new2'
|
72
|
+
end
|
73
|
+
|
74
|
+
it "should turn redirects array into Redirect Objects" do
|
75
|
+
@app = Rack::Redirect.new(['^/old3', '/new', {:code => 307}], ['^/old2', '/new2', {:name => 'test'}])
|
76
|
+
res = Rack::MockRequest.new(@app).get("/test")
|
77
|
+
|
78
|
+
res.not_found?.should be_true
|
79
|
+
res["Content-Type"].should == "text/html"
|
80
|
+
res.body.should == "not found"
|
81
|
+
end
|
82
|
+
|
83
|
+
end
|
84
|
+
|
85
|
+
describe "Redirect::Data" do
|
86
|
+
it "should turn an array into an Object" do
|
87
|
+
data = Redirect::Data.new('^/old3', '/new', {:code => 307, :name => 'test'})
|
88
|
+
data.catch_url.should == '^/old3'
|
89
|
+
data.redirect_url.should == '/new'
|
90
|
+
data.code.should == 307
|
91
|
+
data.name.should == 'test'
|
92
|
+
end
|
93
|
+
|
94
|
+
it "should turn an dafault array into an Object" do
|
95
|
+
data = Redirect::Data.new('^/old3', '/new')
|
96
|
+
data.catch_url.should == '^/old3'
|
97
|
+
data.redirect_url.should == '/new'
|
98
|
+
data.code.should == 301
|
99
|
+
data.name.should == nil
|
100
|
+
end
|
101
|
+
end
|
102
|
+
|
103
|
+
describe "Redirect" do
|
104
|
+
after do
|
105
|
+
Redirect.default_code = 301
|
106
|
+
end
|
107
|
+
|
108
|
+
it "Should be able to configure the default http code" do
|
109
|
+
data = Redirect::Data.new('/a', '/b')
|
110
|
+
data.code.should == 301
|
111
|
+
Redirect.default_code = 307
|
112
|
+
data = Redirect::Data.new('/a', '/b')
|
113
|
+
data.code.should == 307
|
114
|
+
end
|
115
|
+
|
116
|
+
end
|
metadata
ADDED
@@ -0,0 +1,73 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: redirect
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.1
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Petrik de Heus
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
|
12
|
+
date: 2009-02-19 00:00:00 +01:00
|
13
|
+
default_executable:
|
14
|
+
dependencies:
|
15
|
+
- !ruby/object:Gem::Dependency
|
16
|
+
name: hoe
|
17
|
+
type: :development
|
18
|
+
version_requirement:
|
19
|
+
version_requirements: !ruby/object:Gem::Requirement
|
20
|
+
requirements:
|
21
|
+
- - ">="
|
22
|
+
- !ruby/object:Gem::Version
|
23
|
+
version: 1.8.3
|
24
|
+
version:
|
25
|
+
description: Redirect is a simple Ruby redirect DSL build on Rack
|
26
|
+
email:
|
27
|
+
- FIX@example.com
|
28
|
+
executables: []
|
29
|
+
|
30
|
+
extensions: []
|
31
|
+
|
32
|
+
extra_rdoc_files:
|
33
|
+
- History.txt
|
34
|
+
- Manifest.txt
|
35
|
+
- README.txt
|
36
|
+
files:
|
37
|
+
- History.txt
|
38
|
+
- Manifest.txt
|
39
|
+
- README.txt
|
40
|
+
- Rakefile
|
41
|
+
- example.rb
|
42
|
+
- lib/redirect.rb
|
43
|
+
- spec/helper.rb
|
44
|
+
- spec/rack_spec.rb
|
45
|
+
has_rdoc: true
|
46
|
+
homepage: http://github.com/p8/redirect/tree/master
|
47
|
+
post_install_message:
|
48
|
+
rdoc_options:
|
49
|
+
- --main
|
50
|
+
- README.txt
|
51
|
+
require_paths:
|
52
|
+
- lib
|
53
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
54
|
+
requirements:
|
55
|
+
- - ">="
|
56
|
+
- !ruby/object:Gem::Version
|
57
|
+
version: "0"
|
58
|
+
version:
|
59
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
60
|
+
requirements:
|
61
|
+
- - ">="
|
62
|
+
- !ruby/object:Gem::Version
|
63
|
+
version: "0"
|
64
|
+
version:
|
65
|
+
requirements: []
|
66
|
+
|
67
|
+
rubyforge_project: redirect
|
68
|
+
rubygems_version: 1.3.1
|
69
|
+
signing_key:
|
70
|
+
specification_version: 2
|
71
|
+
summary: Redirect is a simple Ruby redirect DSL build on Rack
|
72
|
+
test_files: []
|
73
|
+
|