rewritten 0.10.0 → 0.11.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: a44ade38a5b5052b5d98a4424fc444e7b4208939
4
- data.tar.gz: 6ddaf0da53b50cca46e91c6d4a77a90003abfeb4
3
+ metadata.gz: 4b8a500718f5764e1215c94dcefc425cfa4fb609
4
+ data.tar.gz: c36cd32276590c3ccda6b0ff56371dcd09e37f43
5
5
  SHA512:
6
- metadata.gz: e2c287f97d495e2a7f566baba6ab2a9c266f05f984daa5eca6d76cd74a0961c5fc0e62d08517ddf616612468dc2c74f1253b50f2ceefde2f3eb61e3ff59f9986
7
- data.tar.gz: 0ca027f2379473fbf9602fcaf04c33d62e00b3f56c3628d903e99b372e1b83102c78da4ee5aee4bac9644b69a005ee29ce469df5d4b8ec14061c2fc9139aa2b5
6
+ metadata.gz: 4597038ac4306fbc18f19474c744de3fa1077c817b85e5d98c360bac7900e9f5340d8a83c8243f1f25afe1c3d2fa7297c8b654e5d5210b5f196c7ed2ac50bdb4
7
+ data.tar.gz: be0923ab8fd3e7567ef325257ecfa1be9a4b0942e45665985d7aa15992a9c3928f591af3ff7c4d599dfb20cd70c1fee3d18193695b63dd4957b1deb0bb8852a3
data/HISTORY.rdoc CHANGED
@@ -1,3 +1,11 @@
1
+ == 0.11.0
2
+
3
+ * Partial translations and canonical tags in Rack::Rewritten::Url
4
+
5
+ == 0.10.0
6
+
7
+ * add Rails URL Helpers
8
+
1
9
  == 0.9.0
2
10
 
3
11
  * always chomp '/' from request_path
@@ -5,7 +13,7 @@
5
13
 
6
14
  == 0.8.0
7
15
 
8
- * introduce the [H] flag to stop translating
16
+ * introduce the [L] flag to stop translating
9
17
 
10
18
  == 0.7.0
11
19
 
data/README.md CHANGED
@@ -1,4 +1,4 @@
1
- Rewritten [![Build Status](https://travis-ci.org/learnjin/rewritten.png?branch=master)](https://travis-ci.org/learnjin/rewritten/) [![Code Climate](https://codeclimate.com/github/learnjin/rewritten.png)](https://codeclimate.com/github/learnjin/rewritten) [![Coverage Status](https://coveralls.io/repos/learnjin/rewritten/badge.png)](https://coveralls.io/r/learnjin/rewritten)
1
+ Rewritten [![Build Status](https://travis-ci.org/learnjin/rewritten.png?branch=master)](https://travis-ci.org/learnjin/rewritten/) [![Csdfode Climate](https://codeclimate.com/github/learnjin/rewritten.png)](https://codeclimate.com/github/learnjin/rewritten) [![Coverage Status](https://coveralls.io/repos/learnjin/rewritten/badge.png)](https://coveralls.io/r/learnjin/rewritten)
2
2
  =========
3
3
 
4
4
  Rewritten is a lookup-based rewriting engine that rewrites requested URLs on
data/afile ADDED
File without changes
data/lib/rack/url.rb CHANGED
@@ -10,11 +10,12 @@ module Rack
10
10
  @app = app
11
11
  @translate_backwards = false
12
12
  @downcase_before_lookup = false
13
+ @translate_partial = false
13
14
 
14
15
  instance_eval(&block) if block_given?
15
16
  end
16
17
 
17
- def call(env)
18
+ def call(env, tail=nil)
18
19
  req = Rack::Request.new(env)
19
20
 
20
21
  subdomain = env["SUBDOMAIN"] ? "#{env["SUBDOMAIN"]}:" : ""
@@ -32,26 +33,53 @@ module Rack
32
33
  if current_path == req.path_info or ::Rewritten.has_flag?(path, 'L')
33
34
  # if this is the current path, rewrite path and parameters
34
35
  tpath, tparams = split_to_path_params(to)
35
- req.path_info = tpath
36
+
36
37
  env['QUERY_STRING'] = Rack::Utils.build_query(tparams.merge(req.params))
37
- @app.call(req.env)
38
+ req.path_info = tpath + (tail ? "/"+tail : "")
39
+ #@app.call(req.env)
40
+
41
+ # add the canonical tag to the body
42
+ status, headers, response = @app.call(req.env)
43
+
44
+ if status == 200 && headers["Content-Type"] =~ /text\/html|application\/xhtml\+xml/
45
+ body = ""
46
+ response.each { |part| body << part }
47
+ index = body.rindex("</head>")
48
+ if index
49
+ body.insert(index, %Q|<link rel="canonical" href="#{path}"/>| )
50
+ headers["Content-Length"] = body.length.to_s
51
+ response = [body]
52
+ end
53
+ end
54
+
55
+ [status, headers, response]
56
+
38
57
  else
39
58
  # if this is not the current path, redirect to current path
40
59
  # NOTE: assuming redirection is always to non-subdomain-path
41
-
60
+
42
61
  r = Rack::Response.new
43
62
 
44
63
  new_path = env["rack.url_scheme"].dup
45
64
  new_path << "://"
46
65
  new_path << env["HTTP_HOST"].dup.sub(/^#{subdomain.chomp(':')}\./, '')
47
- new_path << current_path
66
+ new_path << current_path + (tail ? "/"+tail : "")
48
67
  new_path << '?' << env["QUERY_STRING"] unless (env["QUERY_STRING"]||'').empty?
49
68
 
50
69
  r.redirect(new_path, 301)
51
70
  a = r.finish
52
71
  end
53
- else
54
- @app.call(req.env)
72
+ else
73
+ # Translation of partials (e.g. /some/path/tail -> /translated/path/tail)
74
+ if(path).count('/') > 1 && translate_partial?
75
+ parts = path.split('/')
76
+ req.path_info = parts.slice(0, parts.size-1).join('/')
77
+ self.call(req.env, parts.last + tail.to_s)
78
+ else
79
+ req.path_info = (tail ? req.path_info+"/"+tail : req.path_info)
80
+ @app.call(req.env)
81
+
82
+ end
55
83
  end
56
84
  end
57
85
 
@@ -64,9 +92,9 @@ module Rack
64
92
  private
65
93
 
66
94
  def translate_backwards?
67
- @translate_backwards
95
+ @translate_backwards
68
96
  end
69
-
97
+
70
98
  def translate_backwards=(yes_or_no)
71
99
  @translate_backwards = yes_or_no
72
100
  end
@@ -74,16 +102,18 @@ module Rack
74
102
  def downcase_before_lookup?
75
103
  @downcase_before_lookup
76
104
  end
77
-
105
+
78
106
  def downcase_before_lookup=(yes_or_no)
79
107
  @downcase_before_lookup = yes_or_no
80
108
  end
81
109
 
82
-
110
+ def translate_partial?
111
+ @translate_partial
112
+ end
113
+
114
+ def translate_partial=(yes_or_no)
115
+ @translate_partial = yes_or_no
116
+ end
83
117
  end
84
118
  end
85
-
86
119
  end
87
-
88
-
89
-
@@ -1,4 +1,4 @@
1
1
  module Rewritten
2
- VERSION = "0.10.0"
2
+ VERSION = "0.11.0"
3
3
  end
4
4
 
@@ -60,8 +60,73 @@ describe Rack::Rewritten::Url do
60
60
  ret[0].must_equal 200
61
61
  end
62
62
 
63
+ describe "partial translation" do
64
+
65
+ before {
66
+ @request_str = '/foo/baz/with_tail'
67
+ @env = request_url(@request_str)
68
+ @html_body = <<-HTML
69
+ <html>
70
+ <head></head>
71
+ <body>Hello</body>
72
+ </html>
73
+ HTML
74
+ }
75
+
76
+ it "must not translate partials by default" do
77
+ @app.expect :call, [200, {'Content-Type' => 'text/plain'},[""]], [Hash]
78
+ ret = @rack.call @env
79
+ @app.verify
80
+ @env['PATH_INFO'].must_equal @request_str
81
+ end
82
+
83
+ it "must translate partials if enabled" do
84
+ @rack = Rack::Rewritten::Url.new(@app) do
85
+ self.translate_partial = true
86
+ end
87
+
88
+ @app.expect :call, [200, {'Content-Type' => 'text/html'},[]], [Hash]
89
+
90
+ ret = @rack.call @env
91
+ @app.verify
92
+ @env['PATH_INFO'].must_equal '/products/1/with_tail'
93
+ end
94
+
95
+ it "must add the canonical tag to pages with trail" do
96
+
97
+ @rack = Rack::Rewritten::Url.new(lambda{|env| [200, {'Content-Type' => 'text/html'}, [@html_body]]}) do
98
+ self.translate_partial = true
99
+ end
100
+
101
+ res,env,body = @rack.call(@env)
102
+ html = body.join("")
103
+ html.must_include '<link rel="canonical" href="/foo/baz"/>'
104
+ end
105
+
106
+ it "won't translate segments not by separated by slashes" do
107
+ @rack = Rack::Rewritten::Url.new(@app) do
108
+ self.translate_partial = true
109
+ end
110
+ @app.expect :call, [200, {'Content-Type' => 'text/plain'},[""]], [Hash]
111
+ ret = @rack.call @env=request_url('/foo/bazzling')
112
+ @app.verify
113
+ @env['PATH_INFO'].must_equal '/foo/bazzling'
114
+ end
115
+
116
+ it "must carry on trail when redirecting" do
117
+ @rack = Rack::Rewritten::Url.new(@app) do
118
+ self.translate_partial = true
119
+ end
120
+ ret = @rack.call request_url('/foo/bar/with_tail', 'QUERY_STRING' => 'w=1')
121
+ @app.verify
122
+ ret[0].must_equal 301
123
+ ret[1]['Location'].must_equal "http://www.example.org/foo/baz/with_tail?w=1"
124
+ end
125
+
126
+ end
127
+
63
128
  describe "/ behavior" do
64
-
129
+
65
130
  it "must 301 redirect paths with / in the end to their chomped version" do
66
131
  ret = @rack.call request_url('/foo/bar/')
67
132
  @app.verify
@@ -104,7 +169,7 @@ describe Rack::Rewritten::Url do
104
169
  ret[0].must_equal 200
105
170
  end
106
171
 
107
- it "must redirect from resource url to nice url if enabled xxx" do
172
+ it "must redirect from resource url to nice url if enabled" do
108
173
  @rack = Rack::Rewritten::Url.new(@app) do
109
174
  self.translate_backwards = true
110
175
  end
@@ -148,7 +213,6 @@ describe Rack::Rewritten::Url do
148
213
 
149
214
  end
150
215
 
151
-
152
216
  end
153
217
 
154
218
 
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: rewritten
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.10.0
4
+ version: 0.11.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Kai Rubarth
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2013-06-12 00:00:00.000000000 Z
11
+ date: 2013-10-18 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: redis-namespace
@@ -153,6 +153,7 @@ files:
153
153
  - HISTORY.rdoc
154
154
  - README.md
155
155
  - Rakefile
156
+ - afile
156
157
  - bin/rewritten-dump.rb
157
158
  - bin/rewritten-import.rb
158
159
  - bin/rewritten-web.rb
@@ -218,7 +219,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
218
219
  version: '0'
219
220
  requirements: []
220
221
  rubyforge_project: rewritten
221
- rubygems_version: 2.0.0
222
+ rubygems_version: 2.0.3
222
223
  signing_key:
223
224
  specification_version: 4
224
225
  summary: A redis-based URL rewriting engine