rewritten 0.7.0 → 0.8.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: 6b219a3b39bc494eb23d7768fd6de8fbab2b7933
4
- data.tar.gz: 2f1ee10125acebf0d1b007e1d65a7f2b6648c6c7
3
+ metadata.gz: f5e8e2e0cac2c6d7a2c2ff86d2be720ab49e6185
4
+ data.tar.gz: 90aa30fa3b3e23fc6fb8f3670747e9fad38097b6
5
5
  SHA512:
6
- metadata.gz: e15bbb18a1f5bf571c0846049ca57daba0ffd1665f7ee5d4035decbbb16389921abf28a9b6be4c9b03e6959a7e1bf9def345dce6f9402a00db00ffe057b3b8fb
7
- data.tar.gz: 5e4bee52c3efef72088ca7ebd0326c7168cc17848b8c62bf2a4d4753363bbf0d0df28d4b9dc9e2b156e8d7cc966b0e058c9518f9ffbc05d5ab287bd524d25be7
6
+ metadata.gz: 50010ed61dde1c3e54f924e9ec41a2550fb5344167f716de4959b8f92658c65ba562247f75d630c008ae98efe7f223782a82797e435aaa6abb7374c89fb34971
7
+ data.tar.gz: 07d5fdfbf2ae667d9584569649835d32f5077e138173ef64a41c73612b88185a930b6c63531d1711753784673bc812ce2df9c32853a59ca9a4144f1eb2c1f8aa
data/HISTORY.rdoc CHANGED
@@ -1,3 +1,7 @@
1
+ == 0.8.0
2
+
3
+ * introduce the [H] flag to stop translating
4
+
1
5
  == 0.7.0
2
6
 
3
7
  * use sorted sets for the targets instead of lists
@@ -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
 
@@ -46,7 +46,7 @@ File.open(options[:file]).each do |line|
46
46
  next if line =~ /^#/
47
47
  from,to = line.split(";")
48
48
  puts "adding #{from} -> #{to}" if options[:verbose]
49
- Rewritten.add_translation(from,to)
49
+ Rewritten.add_translation(from,to.chomp)
50
50
  end
51
51
 
52
52
 
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 = get_translation(link)
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(from, to)
130
- redis.set("from:#{from}", to)
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 all_translations
178
- Hash[all_tos.map {|to| [to, Rewritten.get_all_translations(to)]}]
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.get("from:#{path}")
229
+ Rewritten.redis.hget("from:#{path}", :to)
206
230
  end
207
231
 
208
232
  # return the number of froms
@@ -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.redis.get("from:#{f}")]}
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
- #translations = Rewritten.list_range(params[:to], 0, -1)
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 your app sees)</h2></label><br/>
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 Rewritten.z_range("to:#{params[:to]}", 0, 100) %>
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>|
@@ -1,4 +1,4 @@
1
1
  module Rewritten
2
- VERSION = "0.7.0"
2
+ VERSION = "0.8.0"
3
3
  end
4
4
 
@@ -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
 
@@ -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
- it "must give all tos" do
12
- Rewritten.all_tos.sort.must_equal ["/to", "/to2"]
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
- it "must return all translations" do
16
- expected = { "/to" => ['/from', '/from2'], "/to2" => ['/from3']}
17
- Rewritten.all_translations.must_equal expected
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
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: rewritten
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.7.0
4
+ version: 0.8.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Kai Rubarth