p8-redirect 0.1.0 → 0.3.0

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,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
@@ -2,6 +2,7 @@ History.txt
2
2
  Manifest.txt
3
3
  README.txt
4
4
  Rakefile
5
+ bin/redirect_app
5
6
  example.rb
6
7
  example.ru
7
8
  lib/redirect.rb
data/README.txt CHANGED
@@ -6,17 +6,36 @@ http://github.com/p8/redirect/tree/master
6
6
 
7
7
  Redirect is a simple Ruby redirect DSL build on Rack
8
8
 
9
- == EXAMPLE
9
+ == GETTING STARTED
10
10
 
11
- Create a file and pass an array to the redirect method:
11
+ From the commandline run:
12
+
13
+ redirect_app PROJECT_NAME
14
+
15
+ This create a directory PROJECT_NAME with the required files.
16
+ PROJECT_NAME.rb contains the main logic.
17
+
18
+ To locally run the app you can do:
19
+
20
+ ruby PROJECT_NAME.rb
21
+
22
+ == REDIRECTS
23
+
24
+ The first one is evaluated first, then the next one, etc..
12
25
 
13
- require 'redirect'
14
26
  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 http code (defaults is 301) in the options and pass a name for the sitemap.
27
+ ['/catch_url2', '/redirect_url2']
28
+
29
+ The catch_url can be a regular expression:
30
+
31
+ ['^/some_regexp', '/all']
32
+ [/old\/(.*)/, '/new/$1'] # /old/2008/02/01 will be redirected to /new/2008/02/01
33
+
34
+ You can pass extra options.
35
+ :code # Overwrite the http code (defaults is 301) in the options
36
+ :name # named redirects are public so they'll appear in you sitemap
37
+
38
+ ['/catch_url', '/redirect_url', {:code => 307, :name => 'redirect link'}]
20
39
 
21
40
  The default redirect code can be changed:
22
41
 
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
@@ -1,4 +1,5 @@
1
1
  require 'lib/redirect'
2
2
 
3
3
  redirect ['/old_url', '/new_url', {:code => 307, :name => 'test'}],
4
- ['^/some_regexp', '/all']
4
+ [/old\/(.*)/, '/new/$1'],
5
+ ['^/some_regexp', '/all'],
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.1.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 #{r.redirect_url}"
80
- return [r.code, {"Location" => r.redirect_url, "Content-Type" => "text/html"}, "Redirecting to: #{r.redirect_url}"]
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: p8-redirect
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.0
4
+ version: 0.3.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Petrik de Heus
@@ -9,15 +9,15 @@ autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
11
 
12
- date: 2009-02-22 00:00:00 -08:00
13
- default_executable:
12
+ date: 2009-02-24 00:00:00 -08:00
13
+ default_executable: redirect_app
14
14
  dependencies: []
15
15
 
16
16
  description: Redirect is a simple Ruby redirect DSL build on Rack
17
17
  email:
18
18
  - FIX@example.com
19
- executables: []
20
-
19
+ executables:
20
+ - redirect_app
21
21
  extensions: []
22
22
 
23
23
  extra_rdoc_files:
@@ -29,6 +29,7 @@ files:
29
29
  - Manifest.txt
30
30
  - README.txt
31
31
  - Rakefile
32
+ - bin/redirect_app
32
33
  - example.rb
33
34
  - example.ru
34
35
  - lib/redirect.rb