rewritten 0.15.2 → 0.16.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -1,4 +1,3 @@
1
1
  require 'rewritten'
2
2
 
3
3
  run Rewritten::Server
4
-
@@ -7,32 +7,31 @@ module Rewritten
7
7
  def path
8
8
  plural = ActiveSupport::Inflector.pluralize(self.class.to_s)
9
9
  resources = ActiveSupport::Inflector.underscore(plural)
10
- "/#{resources}/#{self.id}"
10
+ "/#{resources}/#{id}"
11
11
  end
12
12
 
13
13
  def rewritten_url
14
- return "" unless persisted?
14
+ return '' unless persisted?
15
15
  Rewritten.get_current_translation(path)
16
16
  end
17
17
 
18
18
  def rewritten_url=(new_url)
19
- if !new_url.nil? && new_url != "" && new_url != rewritten_url
19
+ if !new_url.nil? && new_url != '' && new_url != rewritten_url
20
20
  Rewritten.add_translation(new_url, path)
21
21
  end
22
22
  end
23
23
 
24
24
  def rewritten_urls
25
- return [] unless persisted?
25
+ return [] unless persisted?
26
26
  Rewritten.get_all_translations(path)
27
27
  end
28
28
 
29
- def has_rewritten_url?
29
+ def rewritten_url?
30
30
  Rewritten.exist_translation_for?(path)
31
31
  end
32
-
32
+
33
33
  def remove_rewritten_urls
34
34
  Rewritten.remove_all_translations(path)
35
35
  end
36
-
37
36
  end
38
37
  end
@@ -55,4 +55,3 @@ module Rewritten
55
55
  end
56
56
  end
57
57
  end
58
-
@@ -1,12 +1,9 @@
1
1
  module Rewritten
2
2
  module Rails
3
3
  module UrlHelper
4
-
5
4
  def url_for(options = nil)
6
5
  Rewritten.get_current_translation(super)
7
6
  end
8
-
9
7
  end
10
8
  end
11
9
  end
12
-
@@ -6,7 +6,6 @@ require 'time'
6
6
 
7
7
  module Rewritten
8
8
  class Server < Sinatra::Base
9
-
10
9
  dir = File.dirname(File.expand_path(__FILE__))
11
10
 
12
11
  set :views, "#{dir}/server/views"
@@ -18,15 +17,15 @@ module Rewritten
18
17
  alias_method :h, :escape_html
19
18
 
20
19
  def current_section
21
- url_path request.path_info.sub('/','').split('/')[0].downcase
20
+ url_path request.path_info.sub('/', '').split('/')[0].downcase
22
21
  end
23
22
 
24
23
  def current_page
25
- url_path request.path_info.sub('/','')
24
+ url_path request.path_info.sub('/', '')
26
25
  end
27
26
 
28
27
  def url_path(*path_parts)
29
- [ path_prefix, path_parts ].join("/").squeeze('/')
28
+ [path_prefix, path_parts].join('/').squeeze('/')
30
29
  end
31
30
  alias_method :u, :url_path
32
31
 
@@ -63,7 +62,7 @@ module Rewritten
63
62
  end
64
63
  end
65
64
 
66
- def redis_get_value_as_array(key, start=0)
65
+ def redis_get_value_as_array(key, start = 0)
67
66
  case Resque.redis.type(key)
68
67
  when 'none'
69
68
  []
@@ -79,12 +78,12 @@ module Rewritten
79
78
  end
80
79
 
81
80
  def extract_translations
82
- text = params[:translations] or ""
81
+ text = params[:translations] || ''
83
82
  text.split("\n").map(&:strip).reject(&:empty?)
84
83
  end
85
84
 
86
85
  def show_args(args)
87
- Array(args).map { |a| a.inspect }.join("\n")
86
+ Array(args).map(&:inspect).join("\n")
88
87
  end
89
88
 
90
89
  def worker_hosts
@@ -95,7 +94,7 @@ module Rewritten
95
94
  hosts = Hash.new { [] }
96
95
 
97
96
  Resque.workers.each do |worker|
98
- host, _ = worker.to_s.split(':')
97
+ host, = worker.to_s.split(':')
99
98
  hosts[host] += [worker.to_s]
100
99
  end
101
100
 
@@ -108,94 +107,86 @@ module Rewritten
108
107
 
109
108
  def partial(template, local_vars = {})
110
109
  @partial = true
111
- erb(template.to_sym, {:layout => false}, local_vars)
110
+ erb(template.to_sym, { layout: false }, local_vars)
112
111
  ensure
113
112
  @partial = false
114
113
  end
115
114
 
116
115
  def poll
117
116
  if @polling
118
- text = "Last Updated: #{Time.now.strftime("%H:%M:%S")}"
117
+ text = "Last Updated: #{Time.now.strftime('%H:%M:%S')}"
119
118
  else
120
119
  text = "<a href='#{u(request.path_info)}.poll' rel='poll'>Live Poll</a>"
121
120
  end
122
121
  "<p class='poll'>#{text}</p>"
123
122
  end
124
-
125
123
  end # enf of helpers
126
-
127
124
 
128
125
  def show(page, layout = true)
129
- begin
130
- erb page.to_sym, {:layout => layout}, :rewritten => Rewritten
131
- rescue Errno::ECONNREFUSED
132
- erb :error, {:layout => false}, :error => "Can't connect to Redis! (#{Rewritten.redis_id})"
133
- end
126
+ erb page.to_sym, { layout: layout }, rewritten: Rewritten
127
+ rescue Errno::ECONNREFUSED
128
+ erb :error, { layout: false }, error: "Can't connect to Redis! (#{Rewritten.redis_id})"
134
129
  end
135
-
130
+
136
131
  def show_for_polling(page)
137
- content_type "text/html"
132
+ content_type 'text/html'
138
133
  @polling = true
139
134
  show(page.to_sym, false).gsub(/\s{1,}/, ' ')
140
135
  end
141
136
 
142
-
143
137
  ################################################################################
144
138
 
145
-
146
- get "/?" do
139
+ get '/?' do
147
140
  redirect url_path(:translations)
148
141
  end
149
142
 
150
- get "/translations" do
143
+ get '/translations' do
151
144
  @size = 0
152
145
  @start = params[:start].to_i
153
146
 
154
147
  filter = params[:f]
155
148
 
156
- if filter && filter != ""
149
+ if filter && filter != ''
157
150
  @translations = []
158
151
 
159
152
  keys = Rewritten.redis.keys("from:#{filter}") +
160
- Rewritten.redis.keys("to:#{filter}")
153
+ Rewritten.redis.keys("to:#{filter}")
161
154
 
162
155
  keys.each do |key|
163
- prefix, url = key.split(":")
164
- if prefix == "from"
156
+ prefix, url = key.split(':')
157
+ if prefix == 'from'
165
158
  to = Rewritten.translate(url)
166
159
  @translations << [url, to]
167
- elsif prefix == "to"
168
- from = Rewritten.get_current_translation(url)
160
+ elsif prefix == 'to'
161
+ from = Rewritten.get_current_translation(url)
169
162
  @translations << [from, url]
170
163
  end
171
164
  end
172
165
 
173
166
  @size = @translations.size
174
- @translations = @translations[params[:start].to_i..params[:start].to_i+Rewritten.per_page-1]
167
+ @translations = @translations[params[:start].to_i..params[:start].to_i + Rewritten.per_page - 1]
175
168
 
176
169
  else
177
170
  @size = Rewritten.num_froms
178
- froms = Rewritten.all_froms[@start, @start+Rewritten.per_page-1]
179
- @translations = froms.map{|f| [f, Rewritten.translate(f)]}
171
+ froms = Rewritten.all_froms[@start, @start + Rewritten.per_page - 1]
172
+ @translations = froms.map { |f| [f, Rewritten.translate(f)] }
180
173
  end
181
174
 
182
-
183
175
  show 'translations'
184
176
  end
185
177
 
186
- get "/new" do
178
+ get '/new' do
187
179
  @translations = []
188
- show "new"
180
+ show 'new'
189
181
  end
190
182
 
191
- get "/edit" do
183
+ get '/edit' do
192
184
  @old_to = @to = params[:to]
193
- @translations = Rewritten.get_all_translations(@to).map{|t| Rewritten.full_line(t)}
194
- show "edit"
185
+ @translations = Rewritten.get_all_translations(@to).map { |t| Rewritten.full_line(t) }
186
+ show 'edit'
195
187
  end
196
188
 
197
- post "/edit" do
198
-
189
+ post '/edit' do
199
190
  @old_to = params[:old].strip
200
191
  @to = params[:to].strip
201
192
  @translations = extract_translations
@@ -205,85 +196,79 @@ module Rewritten
205
196
  # delete old translations
206
197
  Rewritten.remove_all_translations(@old_to)
207
198
  # create new translations
208
-
199
+
209
200
  Rewritten.add_translations(@to, @translations)
210
201
 
211
202
  redirect u("/to?to=#{escape(@to)}")
212
203
  else
213
- show "edit"
204
+ show 'edit'
214
205
  end
215
-
216
206
  end
217
207
 
218
- post "/translations" do
208
+ post '/translations' do
219
209
  @to = params[:to].strip
220
- @translations = extract_translations
210
+ @translations = extract_translations
221
211
  if @to != '' && @translations.size > 0
222
212
  Rewritten.add_translations(@to, @translations)
223
213
  redirect u('translations')
224
214
  else
225
- show "new"
215
+ show 'new'
226
216
  end
227
217
  end
228
218
 
229
- get "/to" do
219
+ get '/to' do
230
220
  @translations = Rewritten.get_all_translations(params[:to])
231
- show "to"
221
+ show 'to'
232
222
  end
233
223
 
234
- get "/cleanup" do
224
+ get '/cleanup' do
235
225
  # find keys that have no target
236
- @from_without_tos= []
226
+ @from_without_tos = []
237
227
  Rewritten.all_froms.each do |from|
238
- if Rewritten.redis.get("from:#{from}").empty?
239
- @from_without_tos << from
240
- end
228
+ @from_without_tos << from if Rewritten.redis.get("from:#{from}").empty?
241
229
  end
242
-
243
- show "cleanup"
230
+
231
+ show 'cleanup'
244
232
  end
245
233
 
246
- get "/delete" do
234
+ get '/delete' do
247
235
  @from = params[:from]
248
236
  @to = params[:to]
249
- show "delete"
237
+ show 'delete'
250
238
  end
251
239
 
252
240
  post '/delete' do
253
241
  from = params[:from]
254
242
  to = params[:to]
255
243
  Rewritten.remove_translation(from, to)
256
- redirect u("/")
244
+ redirect u('/')
257
245
  end
258
246
 
259
- get "/delete_all" do
247
+ get '/delete_all' do
260
248
  @to = params[:to]
261
- show "delete_all"
249
+ show 'delete_all'
262
250
  end
263
251
 
264
- post "/delete_all" do
252
+ post '/delete_all' do
265
253
  Rewritten.remove_all_translations(params[:to])
266
- redirect u("/")
254
+ redirect u('/')
267
255
  end
268
256
 
269
- get "/hits" do
270
- show "hits"
257
+ get '/hits' do
258
+ show 'hits'
271
259
  end
272
260
 
273
- get "/hits/clear" do
274
- show "clear_hits"
261
+ get '/hits/clear' do
262
+ show 'clear_hits'
275
263
  end
276
264
 
277
- post "/hits/clear" do
278
- Rewritten.redis.del("hits")
279
- redirect u("/hits")
265
+ post '/hits/clear' do
266
+ Rewritten.redis.del('hits')
267
+ redirect u('/hits')
280
268
  end
281
269
 
282
270
  def self.tabs
283
- @tabs ||= ["Translations", "Hits"]
271
+ @tabs ||= %w(Translations Hits)
284
272
  end
285
-
286
273
  end
287
274
  end
288
-
289
-
@@ -7,10 +7,10 @@ module Resque
7
7
  include Rack::Test::Methods
8
8
  def app
9
9
  Resque::Server.new
10
- end
10
+ end
11
11
 
12
12
  def self.should_respond_with_success
13
- test "should respond with success" do
13
+ test 'should respond with success' do
14
14
  assert last_response.ok?, last_response.errors
15
15
  end
16
16
  end
@@ -1,4 +1,3 @@
1
1
  module Rewritten
2
- VERSION = "0.15.2"
2
+ VERSION = '0.16.0'
3
3
  end
4
-
@@ -1,32 +1,32 @@
1
1
  # -*- encoding: utf-8 -*-
2
- $:.push File.expand_path("../lib", __FILE__)
3
- require "rewritten/version"
2
+ $LOAD_PATH.push File.expand_path('../lib', __FILE__)
3
+ require 'rewritten/version'
4
4
 
5
5
  Gem::Specification.new do |s|
6
- s.name = "rewritten"
6
+ s.name = 'rewritten'
7
7
  s.version = Rewritten::VERSION
8
- s.authors = ["Kai Rubarth"]
9
- s.email = ["kai@doxter.de"]
10
- s.homepage = ""
11
- s.summary = %q{A redis-based URL rewriting engine}
8
+ s.authors = ['Kai Rubarth']
9
+ s.email = ['kai@doxter.de']
10
+ s.homepage = ''
11
+ s.summary = 'A redis-based URL rewriting engine'
12
12
 
13
- s.rubyforge_project = "rewritten"
13
+ s.rubyforge_project = 'rewritten'
14
14
 
15
15
  s.files = `git ls-files`.split("\n")
16
16
  s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
17
- s.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
18
- s.require_paths = ["lib"]
17
+ s.executables = `git ls-files -- bin/*`.split("\n").map { |f| File.basename(f) }
18
+ s.require_paths = ['lib']
19
19
 
20
- s.add_dependency "redis-namespace"
21
- s.add_dependency "vegas", "~> 0.1.2"
22
- s.add_dependency "sinatra", ">= 0.9.2"
23
- s.add_dependency "multi_json", "~> 1.0"
24
- s.add_dependency "activesupport"
20
+ s.add_dependency 'redis-namespace'
21
+ s.add_dependency 'vegas', '~> 0.1.2'
22
+ s.add_dependency 'sinatra', '>= 0.9.2'
23
+ s.add_dependency 'multi_json', '~> 1.0'
24
+ s.add_dependency 'activesupport'
25
25
 
26
- s.add_development_dependency "rake"
27
- s.add_development_dependency "minitest"
28
- s.add_development_dependency "pry"
29
- s.add_development_dependency "coveralls"
26
+ s.add_development_dependency 'rake'
27
+ s.add_development_dependency 'minitest'
28
+ s.add_development_dependency 'pry'
29
+ s.add_development_dependency 'coveralls'
30
30
 
31
31
  s.description = <<description
32
32
  Rewritten is a lookup-based rewriting engine that rewrites requested
@@ -46,7 +46,4 @@ Gem::Specification.new do |s|
46
46
  4. A Rack app for substituting URLs in HTML pages with their current translation (Rack::Rewritten::Html)
47
47
  5. A Rack app for recording successful request (Rack::Rewritten::Record)
48
48
  description
49
-
50
49
  end
51
-
52
-
@@ -1,22 +1,20 @@
1
1
  require 'test_helper'
2
2
 
3
3
  describe Rack::Rewritten::Canonical do
4
-
5
- def call_args(overrides={})
6
- {'HTTP_HOST' => 'www.example.org',
4
+ def call_args(overrides = {})
5
+ { 'HTTP_HOST' => 'www.example.org',
7
6
  'REQUEST_URI' => '/foo/with/params',
8
- 'SCRIPT_INFO'=> '',
7
+ 'SCRIPT_INFO' => '',
9
8
  'PATH_INFO' => '/foo/with/params',
10
9
  'QUERY_STRING' => '',
11
10
  'SERVER_PORT' => 80,
12
11
  'rack.input' => '',
13
- 'rack.url_scheme' => 'http'}.merge(overrides)
14
- end
15
-
16
- def request_url(url, params={})
17
- call_args.merge({'REQUEST_URI' => url, 'PATH_INFO' => url}.merge(params) )
12
+ 'rack.url_scheme' => 'http' }.merge(overrides)
18
13
  end
19
14
 
15
+ def request_url(url, params = {})
16
+ call_args.merge({ 'REQUEST_URI' => url, 'PATH_INFO' => url }.merge(params))
17
+ end
20
18
 
21
19
  before do
22
20
  Rewritten.add_translation '/foo/bar', '/products/1'
@@ -29,37 +27,31 @@ describe Rack::Rewritten::Canonical do
29
27
  <body>Hello</body>
30
28
  </html>
31
29
  HTML
32
- @rack = Rack::Rewritten::Canonical.new(lambda{|env| [200, {'Content-Type' => 'text/html'}, [@html_body]]})
33
-
30
+ @rack = Rack::Rewritten::Canonical.new(->(_env) { [200, { 'Content-Type' => 'text/html' }, [@html_body]] })
34
31
  end
35
32
 
36
- describe 'canonical tag' do
37
-
38
- it "must add the canonical tag to current translation if on non-translated page" do
39
- res,env,body = @rack.call request_url('/products/1')
40
- html = body.join("")
41
- html.must_include '<link rel="canonical" href="http://www.example.org/foo/baz"/>'
33
+ describe 'canonical tag' do
34
+ it 'must add the canonical tag to current translation if on non-translated page' do
35
+ _res, _env, body = @rack.call request_url('/products/1')
36
+ html = body.join('')
37
+ html.must_include '<link rel="canonical" href="http://www.example.org/foo/baz"/>'
42
38
  end
43
39
 
44
- it "the target of the canonical tag must have no params" do
45
- res,env,body = @rack.call request_url('/products/1').merge('QUERY_STRING' => 'some=param' )
46
- html = body.join("")
47
- html.must_include '<link rel="canonical" href="http://www.example.org/foo/baz"/>'
40
+ it 'the target of the canonical tag must have no params' do
41
+ _res, _env, body = @rack.call request_url('/products/1').merge('QUERY_STRING' => 'some=param')
42
+ html = body.join('')
43
+ html.must_include '<link rel="canonical" href="http://www.example.org/foo/baz"/>'
48
44
  end
49
45
 
50
46
  describe 'context partial' do
51
- before{ Rewritten.translate_partial = true }
52
- after{ Rewritten.translate_partial = false }
47
+ before { Rewritten.translate_partial = true }
48
+ after { Rewritten.translate_partial = false }
53
49
 
54
- it "must add the canonical tag to pages with tail" do
55
- res,env,body = @rack.call request_url('/products/1/with/tail')
56
- html = body.join("")
57
- html.must_include '<link rel="canonical" href="http://www.example.org/foo/baz"/>'
50
+ it 'must add the canonical tag to pages with tail' do
51
+ _res, _env, body = @rack.call request_url('/products/1/with/tail')
52
+ html = body.join('')
53
+ html.must_include '<link rel="canonical" href="http://www.example.org/foo/baz"/>'
58
54
  end
59
-
60
55
  end
61
-
62
56
  end
63
-
64
57
  end
65
-