rewritten 0.7.0 → 0.8.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.
- checksums.yaml +4 -4
- data/HISTORY.rdoc +4 -0
- data/bin/rewritten-dump.rb +1 -1
- data/bin/rewritten-import.rb +1 -1
- data/lib/rack/html.rb +1 -7
- data/lib/rack/url.rb +1 -1
- data/lib/rewritten.rb +31 -7
- data/lib/rewritten/server.rb +3 -4
- data/lib/rewritten/server/public/style.css +19 -1
- data/lib/rewritten/server/views/fields.erb +9 -1
- data/lib/rewritten/server/views/to.erb +2 -2
- data/lib/rewritten/version.rb +1 -1
- data/test/rack/rewritten_url_test.rb +35 -0
- data/test/rewritten_test.rb +43 -5
- metadata +1 -1
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: f5e8e2e0cac2c6d7a2c2ff86d2be720ab49e6185
|
4
|
+
data.tar.gz: 90aa30fa3b3e23fc6fb8f3670747e9fad38097b6
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 50010ed61dde1c3e54f924e9ec41a2550fb5344167f716de4959b8f92658c65ba562247f75d630c008ae98efe7f223782a82797e435aaa6abb7374c89fb34971
|
7
|
+
data.tar.gz: 07d5fdfbf2ae667d9584569649835d32f5077e138173ef64a41c73612b88185a930b6c63531d1711753784673bc812ce2df9c32853a59ca9a4144f1eb2c1f8aa
|
data/HISTORY.rdoc
CHANGED
data/bin/rewritten-dump.rb
CHANGED
@@ -39,7 +39,7 @@ file = options[:out] == "-" ? STDOUT : File.open(options[:out], "w")
|
|
39
39
|
|
40
40
|
file.puts "#from;to"
|
41
41
|
Rewritten.all_tos.each do |to|
|
42
|
-
file.puts Rewritten.get_all_translations(to).map{|from| "#{from};#{to}"}.join("\n")
|
42
|
+
file.puts Rewritten.get_all_translations(to).map{|from| "#{Rewritten.full_line(from)};#{to}"}.join("\n")
|
43
43
|
end
|
44
44
|
|
45
45
|
|
data/bin/rewritten-import.rb
CHANGED
data/lib/rack/html.rb
CHANGED
@@ -20,7 +20,7 @@ module Rack
|
|
20
20
|
links = line.scan(/href="([^"]+)"/).flatten.uniq
|
21
21
|
res = line
|
22
22
|
links.each do |link|
|
23
|
-
t =
|
23
|
+
t = ::Rewritten.get_current_translation(link)
|
24
24
|
res.gsub!(/href="#{link}"/, %Q|href="#{t}"|) if t
|
25
25
|
end
|
26
26
|
new_response << res
|
@@ -32,12 +32,6 @@ module Rack
|
|
32
32
|
[status, headers, new_response]
|
33
33
|
end
|
34
34
|
|
35
|
-
private
|
36
|
-
|
37
|
-
def get_translation(url)
|
38
|
-
::Rewritten.list_range("to:#{url}", -1, 1)
|
39
|
-
end
|
40
|
-
|
41
35
|
end
|
42
36
|
|
43
37
|
end
|
data/lib/rack/url.rb
CHANGED
@@ -27,7 +27,7 @@ module Rack
|
|
27
27
|
current_path = ::Rewritten.get_current_translation(to)
|
28
28
|
current_path = current_path.split(":").last
|
29
29
|
|
30
|
-
if current_path == req.path_info
|
30
|
+
if current_path == req.path_info or ::Rewritten.has_flag?(path, 'L')
|
31
31
|
# if this is the current path, rewrite path and parameters
|
32
32
|
tpath, tparams = split_to_path_params(to)
|
33
33
|
req.path_info = tpath
|
data/lib/rewritten.rb
CHANGED
@@ -126,12 +126,16 @@ module Rewritten
|
|
126
126
|
# translations
|
127
127
|
#
|
128
128
|
|
129
|
-
def add_translation(
|
130
|
-
|
131
|
-
redis.sadd(:froms, from)
|
132
|
-
redis.sadd(:tos, to)
|
129
|
+
def add_translation(line, to)
|
130
|
+
from, flags = line.split(/\s+/)
|
133
131
|
|
132
|
+
flags = flags.scan(/\[(\w+)\]/).first if flags
|
134
133
|
|
134
|
+
redis.hset("from:#{from}", :to, to)
|
135
|
+
redis.hset("from:#{from}", :flags, flags) if flags
|
136
|
+
|
137
|
+
redis.sadd(:froms, from)
|
138
|
+
redis.sadd(:tos, to)
|
135
139
|
score = redis.zcard("to:#{to}") || 0
|
136
140
|
redis.zadd("to:#{to}", score, from)
|
137
141
|
end
|
@@ -174,8 +178,8 @@ module Rewritten
|
|
174
178
|
Array(Rewritten.redis.smembers(:tos))
|
175
179
|
end
|
176
180
|
|
177
|
-
def
|
178
|
-
|
181
|
+
def translate(from)
|
182
|
+
redis.hget("from:#{from}", :to)
|
179
183
|
end
|
180
184
|
|
181
185
|
def get_all_translations(to)
|
@@ -188,6 +192,26 @@ module Rewritten
|
|
188
192
|
return path
|
189
193
|
end
|
190
194
|
|
195
|
+
def get_flag_string(from)
|
196
|
+
Rewritten.redis.hget("from:#{from}", :flags)||""
|
197
|
+
end
|
198
|
+
|
199
|
+
def has_flag?(from, c)
|
200
|
+
return false unless Rewritten.redis.exists("from:#{from}")
|
201
|
+
get_flag_string(from).index(c) != nil
|
202
|
+
end
|
203
|
+
|
204
|
+
def full_line(from)
|
205
|
+
flags = get_flag_string(from)
|
206
|
+
|
207
|
+
if flags == ""
|
208
|
+
from
|
209
|
+
else
|
210
|
+
"#{from} [#{flags}]"
|
211
|
+
end
|
212
|
+
|
213
|
+
end
|
214
|
+
|
191
215
|
def exist_translation_for?(path)
|
192
216
|
get_current_translation(path) != path
|
193
217
|
end
|
@@ -202,7 +226,7 @@ module Rewritten
|
|
202
226
|
end
|
203
227
|
|
204
228
|
def includes?(path)
|
205
|
-
Rewritten.redis.
|
229
|
+
Rewritten.redis.hget("from:#{path}", :to)
|
206
230
|
end
|
207
231
|
|
208
232
|
# return the number of froms
|
data/lib/rewritten/server.rb
CHANGED
@@ -172,7 +172,7 @@ module Rewritten
|
|
172
172
|
else
|
173
173
|
@size = Rewritten.num_froms
|
174
174
|
froms = Rewritten.all_froms[@start, @start+Rewritten.per_page-1]
|
175
|
-
@translations = froms.map{|f| [f, Rewritten.
|
175
|
+
@translations = froms.map{|f| [f, Rewritten.translate(f)]}
|
176
176
|
end
|
177
177
|
|
178
178
|
|
@@ -186,7 +186,7 @@ module Rewritten
|
|
186
186
|
|
187
187
|
get "/edit" do
|
188
188
|
@old_to = @to = params[:to]
|
189
|
-
@translations = Rewritten.get_all_translations(@to)
|
189
|
+
@translations = Rewritten.get_all_translations(@to).map{|t| Rewritten.full_line(t)}
|
190
190
|
show "edit"
|
191
191
|
end
|
192
192
|
|
@@ -223,8 +223,7 @@ module Rewritten
|
|
223
223
|
end
|
224
224
|
|
225
225
|
get "/to" do
|
226
|
-
|
227
|
-
translations = Rewritten.z_range(params[:to], 0, -1)
|
226
|
+
@translations = Rewritten.get_all_translations(params[:to])
|
228
227
|
show "to"
|
229
228
|
end
|
230
229
|
|
@@ -21,6 +21,24 @@ body { padding:0; margin:0; }
|
|
21
21
|
#main span.hl { background:#efefef; padding:2px;}
|
22
22
|
#main h1 { margin:10px 0; font-size:190%; font-weight:bold; color:#ce1212;}
|
23
23
|
#main h2 { margin:10px 0; font-size:130%;}
|
24
|
+
#main h3 { margin:10px 0; font-size:120%;}
|
25
|
+
|
26
|
+
#main dl{ }
|
27
|
+
#main dl dt {
|
28
|
+
float: left;
|
29
|
+
font-weight: bold;
|
30
|
+
margin-right: 10px;
|
31
|
+
padding:5px;
|
32
|
+
width: 15px;
|
33
|
+
|
34
|
+
}
|
35
|
+
|
36
|
+
#main dl dd{
|
37
|
+
margin: 2px 0;
|
38
|
+
padding: 5px 0;
|
39
|
+
}
|
40
|
+
|
41
|
+
|
24
42
|
#main table { width:100%; margin:10px 0;}
|
25
43
|
#main table tr td, #main table tr th { border:1px solid #ccc; padding:6px;}
|
26
44
|
#main table tr th { background:#efefef; color:#888; font-size:80%; font-weight:bold;}
|
@@ -82,4 +100,4 @@ body { padding:0; margin:0; }
|
|
82
100
|
|
83
101
|
#main form {float:right; margin-top:-10px;}
|
84
102
|
|
85
|
-
#main .time a.toggle_format {text-decoration:none;}
|
103
|
+
#main .time a.toggle_format {text-decoration:none;}
|
@@ -5,12 +5,20 @@
|
|
5
5
|
<h2>Translations (What's seen in the browser's location bar)</h2>
|
6
6
|
<p>Last line being the final location in the browser</p>
|
7
7
|
<p>Subdomains are to be prefixed with a colon, e.g. my.subdomain:/some/path</p>
|
8
|
+
|
9
|
+
<h3>Flags </h3>
|
10
|
+
<p> Flags can modify above behavior. They are appended at the end of a line like so: /foo/bar [L] </p>
|
11
|
+
<dl>
|
12
|
+
<dt>H</dt>
|
13
|
+
<dd>Stop translating. Keep this line in the browser, don't go to last line.</dd>
|
14
|
+
</dl>
|
15
|
+
|
8
16
|
</label><br/>
|
9
17
|
|
10
18
|
<textarea name="translations" cols="80" rows="10"><%= @translations.join("\n") %></textarea> <br />
|
11
19
|
|
12
20
|
<p>
|
13
|
-
<label><h2>Resource (What
|
21
|
+
<label><h2>Resource (What your app sees)</h2></label><br/>
|
14
22
|
<textarea name="to" cols="80" rows="2"><%= @to %></textarea>
|
15
23
|
</p>
|
16
24
|
|
@@ -17,9 +17,9 @@
|
|
17
17
|
<th>Action</th>
|
18
18
|
</tr>
|
19
19
|
|
20
|
-
<% for translation in
|
20
|
+
<% for translation in @translations %>
|
21
21
|
<tr>
|
22
|
-
<td class=''><a href="<%= translation %>" target="new"><%= translation %></td>
|
22
|
+
<td class=''><a href="<%= translation %>" target="new"><%= Rewritten.full_line(translation) %></td>
|
23
23
|
<td class=''>
|
24
24
|
<a href="<%= u(%|/edit?to=#{escape(params[:to])}|) %>"> add </a>|
|
25
25
|
<a href="<%= u(%|/edit?to=#{escape(params[:to])}|) %>"> edit </a>|
|
data/lib/rewritten/version.rb
CHANGED
@@ -11,6 +11,10 @@ describe Rack::Rewritten::Url do
|
|
11
11
|
'rack.input' => '',
|
12
12
|
'rack.url_scheme' => 'http'}.merge(overrides)
|
13
13
|
end
|
14
|
+
|
15
|
+
def request_url(url)
|
16
|
+
call_args.merge('REQUEST_URI' => url, 'PATH_INFO' => url )
|
17
|
+
end
|
14
18
|
|
15
19
|
before {
|
16
20
|
Rewritten.add_translation '/foo/bar', '/products/1'
|
@@ -78,6 +82,37 @@ describe Rack::Rewritten::Url do
|
|
78
82
|
|
79
83
|
end
|
80
84
|
|
85
|
+
describe "flag behavior" do
|
86
|
+
|
87
|
+
before {
|
88
|
+
Rewritten.add_translation('/with/flags [L]', '/adwords/target')
|
89
|
+
Rewritten.add_translation('/with/flags2 [L]', '/adwords/target')
|
90
|
+
Rewritten.add_translation('/no/flags', '/adwords/target')
|
91
|
+
Rewritten.add_translation('/final/line', '/adwords/target')
|
92
|
+
}
|
93
|
+
|
94
|
+
it "must stay on [L] flagged froms" do
|
95
|
+
@app.expect :call, [200, {'Content-Type' => 'text/plain'},[""]], [Hash]
|
96
|
+
@app.expect :call, [200, {'Content-Type' => 'text/plain'},[""]], [Hash]
|
97
|
+
|
98
|
+
ret = @rack.call( request_url('/with/flags'))
|
99
|
+
ret[0].must_equal 200
|
100
|
+
|
101
|
+
ret = @rack.call( request_url('/with/flags2'))
|
102
|
+
ret[0].must_equal 200
|
103
|
+
|
104
|
+
@app.verify
|
105
|
+
end
|
106
|
+
|
107
|
+
it "must redirect for other entries" do
|
108
|
+
ret = @rack.call( request_url('/no/flags') )
|
109
|
+
@app.verify
|
110
|
+
ret[0].must_equal 301
|
111
|
+
ret[1]['Location'].must_equal "http://www.example.org/final/line"
|
112
|
+
end
|
113
|
+
|
114
|
+
end
|
115
|
+
|
81
116
|
|
82
117
|
end
|
83
118
|
|
data/test/rewritten_test.rb
CHANGED
@@ -6,15 +6,53 @@ describe Rewritten do
|
|
6
6
|
Rewritten.add_translation('/from', '/to')
|
7
7
|
Rewritten.add_translation('/from2', '/to')
|
8
8
|
Rewritten.add_translation('/from3', '/to2')
|
9
|
+
|
10
|
+
#with flags
|
11
|
+
Rewritten.add_translation('/with/flags [L12]', '/to3')
|
9
12
|
}
|
10
13
|
|
11
|
-
|
12
|
-
|
14
|
+
describe "basic interface" do
|
15
|
+
|
16
|
+
it "must translate froms" do
|
17
|
+
Rewritten.translate('/from').must_equal '/to'
|
18
|
+
Rewritten.translate('/from2').must_equal '/to'
|
19
|
+
Rewritten.translate('/from3').must_equal '/to2'
|
20
|
+
Rewritten.translate('/with/flags').must_equal '/to3'
|
21
|
+
end
|
22
|
+
|
23
|
+
it "must give current_translation" do
|
24
|
+
Rewritten.get_current_translation('/to').must_equal '/from2'
|
25
|
+
Rewritten.get_current_translation('/to2').must_equal '/from3'
|
26
|
+
Rewritten.get_current_translation('/to3').must_equal '/with/flags'
|
27
|
+
Rewritten.get_current_translation('/n/a').must_equal '/n/a'
|
28
|
+
end
|
29
|
+
|
30
|
+
it "must return the complete line with flags" do
|
31
|
+
Rewritten.full_line('/from').must_equal '/from'
|
32
|
+
Rewritten.full_line('/with/flags').must_equal '/with/flags [L12]'
|
33
|
+
end
|
34
|
+
|
35
|
+
it "must give all tos" do
|
36
|
+
Rewritten.all_tos.sort.must_equal ["/to", "/to2", "/to3"]
|
37
|
+
end
|
38
|
+
|
13
39
|
end
|
14
40
|
|
15
|
-
|
16
|
-
|
17
|
-
|
41
|
+
|
42
|
+
describe "flag management" do
|
43
|
+
|
44
|
+
it "must return a flag string" do
|
45
|
+
Rewritten.get_flag_string('/with/flags').must_equal "L12"
|
46
|
+
Rewritten.get_flag_string('/n/a').must_equal ""
|
47
|
+
end
|
48
|
+
|
49
|
+
it "must query flags" do
|
50
|
+
Rewritten.has_flag?('/with/flags', 'X').must_equal false
|
51
|
+
Rewritten.has_flag?('/with/flags', 'L').must_equal true
|
52
|
+
Rewritten.has_flag?('/with/flags', '1').must_equal true
|
53
|
+
Rewritten.has_flag?('/with/flags', '2').must_equal true
|
54
|
+
end
|
55
|
+
|
18
56
|
end
|
19
57
|
|
20
58
|
end
|