path_rewriter 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
data/.gitignore ADDED
@@ -0,0 +1 @@
1
+ coverage
data/README ADDED
File without changes
data/Rakefile ADDED
@@ -0,0 +1,32 @@
1
+ require File.expand_path("../depends.rb", __FILE__)
2
+ GemLoader.require(:rakefile)
3
+
4
+
5
+ Spec::Rake::SpecTask.new(:spec) do |spec|
6
+ spec.libs << 'lib' << 'spec'
7
+ spec.spec_opts = ['--options', "spec/spec.opts"]
8
+ spec.spec_files = FileList['spec/**/*_spec.rb']
9
+ end
10
+
11
+ namespace :spec do
12
+ Spec::Rake::SpecTask.new(:rcov) do |spec|
13
+ spec.libs << 'lib' << 'spec'
14
+ spec.pattern = 'spec/**/*_spec.rb'
15
+ spec.rcov = true
16
+ end
17
+ end
18
+
19
+
20
+ Jeweler::Tasks.new do |spec|
21
+ spec.name = "path_rewriter"
22
+ spec.summary = "rewrite rails generate url and route."
23
+ spec.email = "Sunteya@gmail.com"
24
+ spec.homepage = "http://github.com/sunteya/path_rewriter"
25
+ spec.authors = ["Sunteya"]
26
+ GemLoader.gemspec.integrate(spec)
27
+ end
28
+ Jeweler::GemcutterTasks.new
29
+
30
+
31
+ task :spec => :check_dependencies
32
+ task :default => :spec
data/VERSION ADDED
@@ -0,0 +1 @@
1
+ 0.0.1
data/depends.rb ADDED
@@ -0,0 +1,27 @@
1
+ require "rubygems"
2
+ gem "gem_loader", ">= 0.2.0"
3
+ require "gem_loader"
4
+
5
+
6
+ GemLoader.setup do
7
+
8
+ scope :runtime do
9
+ gem "actionpack", "~> 2.3.5", :require => ["action_pack", "action_view", "action_controller"]
10
+ gem 'addressable', ">= 2.1.1", :require => "addressable/uri"
11
+ end
12
+
13
+ scope :optional do
14
+ end
15
+
16
+ scope :test do
17
+ gem "rspec", ">= 1.3.0", :require => nil
18
+ end
19
+
20
+ scope :development => [:optional, :test]
21
+
22
+ scope :rakefile do
23
+ gem "rake", ">= 0.8.7"
24
+ gem "jeweler", ">= 1.4.0"
25
+ gem "rspec", :require => "spec/rake/spectask"
26
+ end
27
+ end
@@ -0,0 +1,42 @@
1
+ module PathRewriter
2
+ class Rails
3
+ class << self
4
+ attr_accessor :codec
5
+
6
+ def enable
7
+ self.install
8
+ self.codec = PathRewriter::UrlCodec.new
9
+ yield self
10
+ end
11
+
12
+ def install
13
+ ActionController::Base::optimise_named_routes = false
14
+
15
+ ActionController::Routing::Routes.class_eval do
16
+ def call_with_rewrite(orig)
17
+ env = orig.dup
18
+ begin
19
+ puts PathRewriter::Rails.codec
20
+ env["REQUEST_URI"] = PathRewriter::Rails.codec.decode(env["REQUEST_URI"])
21
+ call_without_rewrite(env)
22
+ rescue ActionController::NotImplemented, ActionController::MethodNotAllowed, ActionController::RoutingError
23
+ call_without_rewrite(orig)
24
+ end
25
+ end
26
+ alias_method_chain :call, :rewrite
27
+ end
28
+
29
+ [ ActionController::Base, ActionView::Base ].each do |klass|
30
+ klass.class_eval do
31
+ def url_for_with_rewrite(*args)
32
+ url = url_for_without_rewrite(*args)
33
+ PathRewriter::Rails.codec.encode(url)
34
+ end
35
+ alias_method_chain :url_for, :rewrite
36
+ end
37
+ end
38
+ end
39
+ end
40
+ end
41
+
42
+ end
@@ -0,0 +1,54 @@
1
+ module PathRewriter
2
+
3
+ class UrlCodec
4
+
5
+ attr_writer :base_path
6
+ attr_writer :root_path
7
+
8
+ def base_path
9
+ @base_path ||= ""
10
+ end
11
+
12
+ def root_path
13
+ @root_path ||= ""
14
+ end
15
+
16
+ def root_path_without_slash
17
+ self.root_path.sub(/^\//, "")
18
+ end
19
+
20
+ def base_path_without_slash
21
+ self.base_path.sub(/^\//, "")
22
+ end
23
+
24
+ def decode(uri)
25
+ return uri if self.base_path.blank?
26
+ uri = Addressable::URI.parse(uri)
27
+
28
+ segments = uri.path.sub(self.root_path, '').split("/")
29
+ segments.delete("")
30
+ segments.unshift(self.base_path_without_slash)
31
+ segments.unshift(self.root_path_without_slash) unless root_path.blank?
32
+ uri.path = "/" + segments.join("/")
33
+
34
+ uri.to_s
35
+ end
36
+
37
+ def encode(uri)
38
+ return uri if self.base_path.blank?
39
+ return uri unless uri.start_with?(self.root_path)
40
+ uri = Addressable::URI.parse(uri)
41
+
42
+ uri.path.sub!(self.root_path, '')
43
+ uri.path.sub!(self.base_path, '')
44
+
45
+ segments = uri.path.split("/")
46
+ segments.unshift(self.root_path_without_slash)
47
+ segments.delete("")
48
+ uri.path = "/" + segments.join("/")
49
+
50
+ uri.to_s
51
+ end
52
+ end
53
+
54
+ end
@@ -0,0 +1,8 @@
1
+ require File.expand_path("../../depends.rb", __FILE__)
2
+ GemLoader.require(:runtime)
3
+
4
+ require "path_rewriter/url_codec"
5
+ require "path_rewriter/rails"
6
+
7
+ module PathRewriter
8
+ end
@@ -0,0 +1,46 @@
1
+ require 'spec_helper'
2
+
3
+ describe PathRewriter::UrlCodec do
4
+
5
+ before(:each) do
6
+ @codec = PathRewriter::UrlCodec.new
7
+ end
8
+
9
+ it "should skip decode uri" do
10
+ @codec.decode("/request/foo.bar").should == "/request/foo.bar"
11
+ @codec.decode("/request/controller/foo.bar").should == "/request/controller/foo.bar"
12
+ end
13
+
14
+ it "should decode uri success" do
15
+ @codec.base_path = "/request"
16
+
17
+ @codec.decode("/").should == "/request"
18
+ @codec.decode("/foo").should == "/request/foo"
19
+ @codec.decode("/?123").should == "/request?123"
20
+ @codec.decode("/controller/foo.bar").should == "/request/controller/foo.bar"
21
+ end
22
+
23
+ it "should decode uri by namespace with root_path" do
24
+ @codec.root_path = "/root"
25
+ @codec.base_path = "/request"
26
+ @codec.decode("/root/bar").should == "/root/request/bar"
27
+ end
28
+
29
+ it "should encode uri" do
30
+ @codec.encode("/foo?123").should == "/foo?123"
31
+ @codec.encode("/").should == "/"
32
+ end
33
+
34
+ it "should encode uri by namespace" do
35
+ @codec.base_path = "/request"
36
+ @codec.encode("/request/foo?123").should == "/foo?123"
37
+ @codec.encode("/request/").should == "/"
38
+ @codec.encode("/request").should == "/"
39
+ end
40
+
41
+ it "should encode uri by namespace with root" do
42
+ @codec.root_path = "/root"
43
+ @codec.base_path = "/request"
44
+ @codec.encode("/root/request/bar").should == "/root/bar"
45
+ end
46
+ end
@@ -0,0 +1,5 @@
1
+ require 'spec_helper'
2
+
3
+ describe PathRewriter do
4
+
5
+ end
data/spec/spec.opts ADDED
@@ -0,0 +1 @@
1
+ --colour
@@ -0,0 +1,9 @@
1
+ require File.expand_path("../../depends.rb", __FILE__)
2
+ GemLoader.require(:test)
3
+ GemLoader.require(:runtime, :development)
4
+
5
+ require "path_rewriter"
6
+
7
+ Spec::Runner.configure do |config|
8
+
9
+ end
metadata ADDED
@@ -0,0 +1,130 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: path_rewriter
3
+ version: !ruby/object:Gem::Version
4
+ prerelease: false
5
+ segments:
6
+ - 0
7
+ - 0
8
+ - 1
9
+ version: 0.0.1
10
+ platform: ruby
11
+ authors:
12
+ - Sunteya
13
+ autorequire:
14
+ bindir: bin
15
+ cert_chain: []
16
+
17
+ date: 2010-04-06 00:00:00 +08:00
18
+ default_executable:
19
+ dependencies:
20
+ - !ruby/object:Gem::Dependency
21
+ name: gem_loader
22
+ prerelease: false
23
+ requirement: &id001 !ruby/object:Gem::Requirement
24
+ requirements:
25
+ - - ">="
26
+ - !ruby/object:Gem::Version
27
+ segments:
28
+ - 0
29
+ - 2
30
+ - 0
31
+ version: 0.2.0
32
+ type: :runtime
33
+ version_requirements: *id001
34
+ - !ruby/object:Gem::Dependency
35
+ name: actionpack
36
+ prerelease: false
37
+ requirement: &id002 !ruby/object:Gem::Requirement
38
+ requirements:
39
+ - - ~>
40
+ - !ruby/object:Gem::Version
41
+ segments:
42
+ - 2
43
+ - 3
44
+ - 5
45
+ version: 2.3.5
46
+ type: :runtime
47
+ version_requirements: *id002
48
+ - !ruby/object:Gem::Dependency
49
+ name: addressable
50
+ prerelease: false
51
+ requirement: &id003 !ruby/object:Gem::Requirement
52
+ requirements:
53
+ - - ">="
54
+ - !ruby/object:Gem::Version
55
+ segments:
56
+ - 2
57
+ - 1
58
+ - 1
59
+ version: 2.1.1
60
+ type: :runtime
61
+ version_requirements: *id003
62
+ - !ruby/object:Gem::Dependency
63
+ name: rspec
64
+ prerelease: false
65
+ requirement: &id004 !ruby/object:Gem::Requirement
66
+ requirements:
67
+ - - ">="
68
+ - !ruby/object:Gem::Version
69
+ segments:
70
+ - 1
71
+ - 3
72
+ - 0
73
+ version: 1.3.0
74
+ type: :development
75
+ version_requirements: *id004
76
+ description:
77
+ email: Sunteya@gmail.com
78
+ executables: []
79
+
80
+ extensions: []
81
+
82
+ extra_rdoc_files:
83
+ - README
84
+ files:
85
+ - .gitignore
86
+ - README
87
+ - Rakefile
88
+ - VERSION
89
+ - depends.rb
90
+ - lib/path_rewriter.rb
91
+ - lib/path_rewriter/rails.rb
92
+ - lib/path_rewriter/url_codec.rb
93
+ - spec/path_rewriter/url_codec_spec.rb
94
+ - spec/path_rewriter_spec.rb
95
+ - spec/spec.opts
96
+ - spec/spec_helper.rb
97
+ has_rdoc: true
98
+ homepage: http://github.com/sunteya/path_rewriter
99
+ licenses: []
100
+
101
+ post_install_message:
102
+ rdoc_options:
103
+ - --charset=UTF-8
104
+ require_paths:
105
+ - lib
106
+ required_ruby_version: !ruby/object:Gem::Requirement
107
+ requirements:
108
+ - - ">="
109
+ - !ruby/object:Gem::Version
110
+ segments:
111
+ - 0
112
+ version: "0"
113
+ required_rubygems_version: !ruby/object:Gem::Requirement
114
+ requirements:
115
+ - - ">="
116
+ - !ruby/object:Gem::Version
117
+ segments:
118
+ - 0
119
+ version: "0"
120
+ requirements: []
121
+
122
+ rubyforge_project:
123
+ rubygems_version: 1.3.6
124
+ signing_key:
125
+ specification_version: 3
126
+ summary: rewrite rails generate url and route.
127
+ test_files:
128
+ - spec/path_rewriter/url_codec_spec.rb
129
+ - spec/path_rewriter_spec.rb
130
+ - spec/spec_helper.rb