p8-redirect 0.0.1 → 0.0.2

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 CHANGED
@@ -1,3 +1,9 @@
1
+ === 0.0.2 / 2009-02-21
2
+
3
+ * 1 major enhancement
4
+
5
+ * Only require Hoe if installed
6
+
1
7
  === 0.0.1 / 2009-02-08
2
8
 
3
9
  * 1 major enhancement
data/README.txt CHANGED
@@ -4,11 +4,25 @@ http://github.com/p8/redirect/tree/master
4
4
 
5
5
  == DESCRIPTION:
6
6
 
7
- Redirect is a ruby redirect server build on Rack
7
+ Redirect is a simple Ruby redirect DSL build on Rack
8
8
 
9
9
  == EXAMPLE
10
10
 
11
- % ruby example.rb
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.
12
26
 
13
27
  == LICENSE:
14
28
 
data/Rakefile CHANGED
@@ -1,12 +1,14 @@
1
1
  # -*- ruby -*-
2
2
 
3
3
  require 'rubygems'
4
- require 'hoe'
5
4
  require 'lib/redirect.rb'
6
5
 
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
6
+ require 'hoe' rescue nil
7
+ if defined? Hoe
8
+ Hoe.new('redirect', Redirect::VERSION) do |p|
9
+ p.developer('Petrik de Heus', 'FIX@example.com')
10
+ p.remote_rdoc_dir = '' # Release to root
11
+ end
10
12
  end
11
13
  #
12
14
  # require 'metric_fu'
data/example.rb CHANGED
@@ -1,3 +1,4 @@
1
1
  require 'lib/redirect'
2
2
 
3
- redirect(['/old_url', '/new_url'], ['^/some_regexp', '/all'])
3
+ redirect ['/old_url', '/new_url', {:code => 307, :name => 'test'}],
4
+ ['^/some_regexp', '/all']
data/lib/redirect.rb CHANGED
@@ -5,7 +5,26 @@ require 'rack/request'
5
5
  require 'rack/response'
6
6
 
7
7
  module Redirect
8
- VERSION = '0.0.1'
8
+ VERSION = '0.0.2'
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
+
9
28
  end
10
29
 
11
30
  def redirect(*redirects)
@@ -20,23 +39,48 @@ end
20
39
  module Rack
21
40
 
22
41
  class Redirect
23
-
42
+ attr_reader :redirects
24
43
  def initialize(*redirects)
25
- @redirects = redirects
44
+ @redirects = redirects.collect do |r|
45
+ ::Redirect::Data.new(*r)
46
+ end
26
47
  end
27
48
 
28
49
  def call(env)
29
50
  req = Request.new(env)
30
- @redirects.each do |pair|
31
- key, redirect_url = pair
32
- if req.fullpath.match(key)
33
- puts "Match found for #{key}."
34
- puts "Redirecting to #{redirect_url}"
35
- return [301, {"Location" => redirect_url, "Content-Type" => "text/html"}, "Redirecting to: #{redirect_url}"]
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}"]
36
62
  end
37
63
  end
38
64
  [404, {"Content-Type" => "text/html"}, "not found"]
39
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
40
84
 
41
85
  end
42
86
 
data/spec/rack_spec.rb CHANGED
@@ -11,6 +11,15 @@ describe "Rack::Redirect" do
11
11
  res.body.should == "not found"
12
12
  end
13
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
+
14
23
  it "should redirect '/' if redirect exists" do
15
24
  @app = Rack::Redirect.new(['/', '/test'])
16
25
  res = Rack::MockRequest.new(@app).get('/')
@@ -18,6 +27,29 @@ describe "Rack::Redirect" do
18
27
  res.body.should == 'Redirecting to: /test'
19
28
  end
20
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
+
21
53
  it "should redirect if redirect exists" do
22
54
  @app = Rack::Redirect.new(['/old/2008', '/2008-new'])
23
55
  res = Rack::MockRequest.new(@app).get('/old/2008')
@@ -39,4 +71,46 @@ describe "Rack::Redirect" do
39
71
  res.body.should == 'Redirecting to: /new2'
40
72
  end
41
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
+
42
116
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: p8-redirect
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.1
4
+ version: 0.0.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - Petrik de Heus
@@ -9,11 +9,12 @@ autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
11
 
12
- date: 2009-02-10 00:00:00 -08:00
12
+ date: 2009-02-21 00:00:00 -08:00
13
13
  default_executable:
14
14
  dependencies:
15
15
  - !ruby/object:Gem::Dependency
16
16
  name: hoe
17
+ type: :development
17
18
  version_requirement:
18
19
  version_requirements: !ruby/object:Gem::Requirement
19
20
  requirements:
@@ -21,7 +22,7 @@ dependencies:
21
22
  - !ruby/object:Gem::Version
22
23
  version: 1.8.3
23
24
  version:
24
- description: Redirect is a ruby redirect server build on Rack
25
+ description: Redirect is a simple Ruby redirect DSL build on Rack
25
26
  email:
26
27
  - FIX@example.com
27
28
  executables: []
@@ -67,6 +68,6 @@ rubyforge_project: redirect
67
68
  rubygems_version: 1.2.0
68
69
  signing_key:
69
70
  specification_version: 2
70
- summary: Redirect is a ruby redirect server build on Rack
71
+ summary: Redirect is a simple Ruby redirect DSL build on Rack
71
72
  test_files: []
72
73