handler301 0.2.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,7 @@
1
+ 0.2.0 (Apr 12, 2010)
2
+
3
+ * Releasing gem
4
+
5
+ 0.1.0 (Apr 8, 2010)
6
+
7
+ * Initial release
@@ -0,0 +1,20 @@
1
+ Copyright (c) 2010 Guillaume Luccisano - g-mai|: guillaume.luccisano
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining
4
+ a copy of this software and associated documentation files (the
5
+ "Software"), to deal in the Software without restriction, including
6
+ without limitation the rights to use, copy, modify, merge, publish,
7
+ distribute, sublicense, and/or sell copies of the Software, and to
8
+ permit persons to whom the Software is furnished to do so, subject to
9
+ the following conditions:
10
+
11
+ The above copyright notice and this permission notice shall be
12
+ included in all copies or substantial portions of the Software.
13
+
14
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
17
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
18
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
19
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
20
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
@@ -0,0 +1,47 @@
1
+ = Handler 301
2
+
3
+ Handler 301 is a plugin for Ruby on Rails that lets you easily manage 301 redirections in on place in order to redirect them to current named routes.
4
+
5
+ All necessary informations are available on the wiki : http://wiki.github.com/kwi/handler301
6
+
7
+ == Configuration example :
8
+
9
+ In the config/handler301.yml of your rails app :
10
+
11
+ "old_url.html": home_path
12
+ "old_url_without_path.html": home
13
+ "product_number_one.html": products_path :id => 1
14
+
15
+ Then, configure a controller that match non matching routes in your config/routes.rb
16
+
17
+ ==== Rails2
18
+
19
+ ActionController::Routing::Routes.draw do |map|
20
+ map.connect "*path", :controller => 'error', :action => 'handle404'
21
+ end
22
+
23
+ ==== Rails3
24
+
25
+ MkdBaseApp::Application.routes.draw do
26
+ match "*path" => 'error#handle404'
27
+ end
28
+
29
+ And finally, in your ErrorController (or whatever the name of your non matching routes controller) :
30
+
31
+
32
+ class ErrorController < ApplicationController
33
+ def handle404
34
+ unless handle_301(request.path, request.query_parameters)
35
+
36
+ # Do your stuff here
37
+
38
+ end
39
+ end
40
+ end
41
+
42
+
43
+ == TODO
44
+
45
+ * Change for using Rails metal in order to speed up the mechanism
46
+
47
+ Copyright (c) 2010 Guillaume Luccisano - g-mai|: guillaume.luccisano, released under the MIT license
@@ -0,0 +1,23 @@
1
+ require 'rake'
2
+ require 'rake/testtask'
3
+ require 'rake/rdoctask'
4
+
5
+ desc 'Default: run unit tests.'
6
+ task :default => :test
7
+
8
+ desc 'Test the handler301 plugin.'
9
+ Rake::TestTask.new(:test) do |t|
10
+ t.libs << 'lib'
11
+ t.libs << 'test'
12
+ t.pattern = 'test/**/*_test.rb'
13
+ t.verbose = true
14
+ end
15
+
16
+ desc 'Generate documentation for the handler301 plugin.'
17
+ Rake::RDocTask.new(:rdoc) do |rdoc|
18
+ rdoc.rdoc_dir = 'rdoc'
19
+ rdoc.title = 'Handler301'
20
+ rdoc.options << '--line-numbers' << '--inline-source'
21
+ rdoc.rdoc_files.include('README')
22
+ rdoc.rdoc_files.include('lib/**/*.rb')
23
+ end
data/init.rb ADDED
@@ -0,0 +1,2 @@
1
+ # encoding: utf-8
2
+ require 'handler301'
@@ -0,0 +1,64 @@
1
+ # encoding: utf8
2
+ module Handler301
3
+ Hash301 = {}
4
+
5
+ private
6
+ # Automatically add _path if not present
7
+ def handle_301_auto_add_path(path_name)
8
+ path_name =~ /(_path|_url)$/ ? path_name : "#{path_name}_path"
9
+ end
10
+
11
+ public
12
+ # Handle 301 redirections
13
+ # Make the redirection if an url match
14
+ # Return true if a redirection has been made
15
+ def handle_301(path, query_params = {})
16
+ return nil if !path
17
+
18
+ path = path.gsub(/^\//, '').split('?', 2).first
19
+
20
+ if r = Hash301[path]
21
+ query_params.delete('action')
22
+ query_params.delete('controller')
23
+
24
+ redirect_path = nil
25
+ # Standard case, use the simple route name
26
+ if !r.index(' ')
27
+ redirect_path = self.send(handle_301_auto_add_path(r).to_sym, query_params)
28
+ else # Blank case / Works only with hashes
29
+
30
+ r = r.split(' ', 2)
31
+ rhash = r.last
32
+ rhash = "{#{rhash}}" unless rhash =~ /^\{.*\}$/
33
+
34
+ # Must do an eval here ?
35
+ hash = eval(rhash) rescue {}
36
+
37
+ redirect_path = send(handle_301_auto_add_path(r[0]).to_sym, hash.merge(query_params))
38
+ end
39
+
40
+ if redirect_path
41
+ redirect_to(redirect_path, :status => 301)
42
+ return true
43
+ end
44
+ end
45
+
46
+ # No redirection has been made
47
+ return nil
48
+ end
49
+
50
+ def self.load_301_file(file)
51
+ h = YAML.load_file(file) rescue {}
52
+ puts "[Handler301] Error in loading '#{file}', this file is empty or not valid." if h.size == 0
53
+ Hash301.merge!(h)
54
+ end
55
+
56
+ end
57
+
58
+ # Load config 301 file
59
+ if defined? Rails
60
+ Handler301::load_301_file('config/handler301.yml')
61
+ end
62
+
63
+ # Include 301 handler in ActionController base
64
+ ActionController::Base.send :include, Handler301
metadata ADDED
@@ -0,0 +1,69 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: handler301
3
+ version: !ruby/object:Gem::Version
4
+ prerelease: false
5
+ segments:
6
+ - 0
7
+ - 2
8
+ - 0
9
+ version: 0.2.0
10
+ platform: ruby
11
+ authors:
12
+ - Guillaume Luccisano
13
+ autorequire:
14
+ bindir: bin
15
+ cert_chain: []
16
+
17
+ date: 2010-04-12 00:00:00 +02:00
18
+ default_executable:
19
+ dependencies: []
20
+
21
+ description: "Handler 301 is a plugin for Ruby on Rails that lets you easily manage 301 redirections in on place in order to redirect them to current named routes. "
22
+ email: guillaume.luccisano@gmail.com
23
+ executables: []
24
+
25
+ extensions: []
26
+
27
+ extra_rdoc_files: []
28
+
29
+ files:
30
+ - lib/handler301.rb
31
+ - CHANGELOG.rdoc
32
+ - MIT-LICENSE
33
+ - Rakefile
34
+ - README.rdoc
35
+ - init.rb
36
+ has_rdoc: true
37
+ homepage: http://github.com/kwi/handler301
38
+ licenses: []
39
+
40
+ post_install_message:
41
+ rdoc_options: []
42
+
43
+ require_paths:
44
+ - lib
45
+ required_ruby_version: !ruby/object:Gem::Requirement
46
+ requirements:
47
+ - - ">="
48
+ - !ruby/object:Gem::Version
49
+ segments:
50
+ - 0
51
+ version: "0"
52
+ required_rubygems_version: !ruby/object:Gem::Requirement
53
+ requirements:
54
+ - - ">="
55
+ - !ruby/object:Gem::Version
56
+ segments:
57
+ - 1
58
+ - 3
59
+ - 4
60
+ version: 1.3.4
61
+ requirements: []
62
+
63
+ rubyforge_project: handler301
64
+ rubygems_version: 1.3.6
65
+ signing_key:
66
+ specification_version: 3
67
+ summary: Simple rails 301 redirection handler with a config file. Rework your url schema without being worry !
68
+ test_files: []
69
+