rewritten 0.15.2 → 0.16.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/.rubocop.yml +12 -0
- data/.rubocop_todo.yml +116 -0
- data/Gemfile +1 -1
- data/HISTORY.rdoc +4 -0
- data/Rakefile +1 -5
- data/bin/rewritten-dump.rb +11 -14
- data/bin/rewritten-import.rb +9 -12
- data/bin/rewritten-web.rb +7 -9
- data/config.ru +0 -1
- data/lib/rack/canonical.rb +6 -14
- data/lib/rack/dummy.rb +7 -14
- data/lib/rack/html.rb +4 -13
- data/lib/rack/record.rb +3 -9
- data/lib/rack/subdomain.rb +5 -13
- data/lib/rack/url.rb +19 -24
- data/lib/rewritten.rb +45 -69
- data/lib/rewritten/config.ru +0 -1
- data/lib/rewritten/document.rb +6 -7
- data/lib/rewritten/helpers.rb +0 -1
- data/lib/rewritten/rails/url_helpers.rb +0 -3
- data/lib/rewritten/server.rb +58 -73
- data/lib/rewritten/server/test_helper.rb +2 -2
- data/lib/rewritten/version.rb +1 -2
- data/rewritten.gemspec +19 -22
- data/test/rack/rewritten_canonical_test.rb +23 -31
- data/test/rack/rewritten_html_test.rb +16 -21
- data/test/rack/rewritten_url_test.rb +77 -95
- data/test/rewritten/document_test.rb +28 -28
- data/test/rewritten_test.rb +25 -36
- data/test/test_helper.rb +2 -4
- metadata +4 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 1c46c2783b6c6c0b7513137ee064d35204d248a8
|
4
|
+
data.tar.gz: e30fe5d81af0086b38aca82dbd03e0a38b9833ef
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: d04627b4246a02ffb960087a07e54ac2a9d5f33196ef32d78b65e3b2ccaabe3515384a88f7536ed8305c9f5ceeb800bcf4c1fac8e9a1f109203839466727d113
|
7
|
+
data.tar.gz: 92736611e92c52327f5faa2754668c9cc97578f806c5aa145b2a7963c3616c0c137fb5c0333679b5312c1e61ecf2f30289494554b014b9599afa7cb92f878eb2
|
data/.rubocop.yml
ADDED
data/.rubocop_todo.yml
ADDED
@@ -0,0 +1,116 @@
|
|
1
|
+
# This configuration was generated by `rubocop --auto-gen-config`
|
2
|
+
# on 2015-07-31 12:23:42 +0200 using RuboCop version 0.32.0.
|
3
|
+
# The point is for the user to remove these configuration records
|
4
|
+
# one by one as the offenses are removed from the code base.
|
5
|
+
# Note that changes in the inspected code, or installation of new
|
6
|
+
# versions of RuboCop, may require this file to be generated again.
|
7
|
+
|
8
|
+
# Offense count: 1
|
9
|
+
# Configuration parameters: AllowSafeAssignment.
|
10
|
+
Lint/AssignmentInCondition:
|
11
|
+
Enabled: false
|
12
|
+
|
13
|
+
# Offense count: 1
|
14
|
+
# Cop supports --auto-correct.
|
15
|
+
# Configuration parameters: AlignWith, SupportedStyles, AutoCorrect.
|
16
|
+
Lint/DefEndAlignment:
|
17
|
+
Enabled: false
|
18
|
+
|
19
|
+
# Offense count: 1
|
20
|
+
# Cop supports --auto-correct.
|
21
|
+
# Configuration parameters: AlignWith, SupportedStyles, AutoCorrect.
|
22
|
+
Lint/EndAlignment:
|
23
|
+
Enabled: false
|
24
|
+
|
25
|
+
# Offense count: 2
|
26
|
+
Lint/UselessAssignment:
|
27
|
+
Enabled: false
|
28
|
+
|
29
|
+
# Offense count: 9
|
30
|
+
Metrics/AbcSize:
|
31
|
+
Max: 71
|
32
|
+
|
33
|
+
# Offense count: 1
|
34
|
+
# Configuration parameters: CountComments.
|
35
|
+
Metrics/ClassLength:
|
36
|
+
Max: 209
|
37
|
+
|
38
|
+
# Offense count: 4
|
39
|
+
Metrics/CyclomaticComplexity:
|
40
|
+
Max: 16
|
41
|
+
|
42
|
+
# Offense count: 40
|
43
|
+
# Configuration parameters: AllowURI, URISchemes.
|
44
|
+
Metrics/LineLength:
|
45
|
+
Max: 268
|
46
|
+
|
47
|
+
# Offense count: 10
|
48
|
+
# Configuration parameters: CountComments.
|
49
|
+
Metrics/MethodLength:
|
50
|
+
Max: 35
|
51
|
+
|
52
|
+
# Offense count: 1
|
53
|
+
# Configuration parameters: CountComments.
|
54
|
+
Metrics/ModuleLength:
|
55
|
+
Max: 231
|
56
|
+
|
57
|
+
# Offense count: 4
|
58
|
+
Metrics/PerceivedComplexity:
|
59
|
+
Max: 18
|
60
|
+
|
61
|
+
# Offense count: 1
|
62
|
+
# Cop supports --auto-correct.
|
63
|
+
# Configuration parameters: EnforcedStyle, SupportedStyles.
|
64
|
+
Style/AndOr:
|
65
|
+
Enabled: false
|
66
|
+
|
67
|
+
# Offense count: 2
|
68
|
+
# Cop supports --auto-correct.
|
69
|
+
# Configuration parameters: EnforcedStyle, SupportedStyles, ProceduralMethods, FunctionalMethods, IgnoredMethods.
|
70
|
+
Style/BlockDelimiters:
|
71
|
+
Enabled: false
|
72
|
+
|
73
|
+
# Offense count: 1
|
74
|
+
# Cop supports --auto-correct.
|
75
|
+
# Configuration parameters: EnforcedStyle, SupportedStyles.
|
76
|
+
Style/BracesAroundHashParameters:
|
77
|
+
Enabled: false
|
78
|
+
|
79
|
+
# Offense count: 2
|
80
|
+
# Configuration parameters: EnforcedStyle, SupportedStyles.
|
81
|
+
Style/ClassAndModuleChildren:
|
82
|
+
Enabled: false
|
83
|
+
|
84
|
+
# Offense count: 14
|
85
|
+
Style/Documentation:
|
86
|
+
Enabled: false
|
87
|
+
|
88
|
+
# Offense count: 3
|
89
|
+
# Configuration parameters: Exclude.
|
90
|
+
Style/FileName:
|
91
|
+
Enabled: false
|
92
|
+
|
93
|
+
# Offense count: 1
|
94
|
+
# Configuration parameters: MinBodyLength.
|
95
|
+
Style/GuardClause:
|
96
|
+
Enabled: false
|
97
|
+
|
98
|
+
# Offense count: 1
|
99
|
+
Style/ModuleFunction:
|
100
|
+
Enabled: false
|
101
|
+
|
102
|
+
# Offense count: 2
|
103
|
+
# Configuration parameters: EnforcedStyle, MinBodyLength, SupportedStyles.
|
104
|
+
Style/Next:
|
105
|
+
Enabled: false
|
106
|
+
|
107
|
+
# Offense count: 3
|
108
|
+
# Cop supports --auto-correct.
|
109
|
+
# Configuration parameters: EnforcedStyle, SupportedStyles, AllowInnerSlashes.
|
110
|
+
Style/RegexpLiteral:
|
111
|
+
Enabled: false
|
112
|
+
|
113
|
+
# Offense count: 1
|
114
|
+
# Configuration parameters: Methods.
|
115
|
+
Style/SingleLineBlockParams:
|
116
|
+
Enabled: false
|
data/Gemfile
CHANGED
data/HISTORY.rdoc
CHANGED
data/Rakefile
CHANGED
@@ -3,7 +3,7 @@ require 'rake/testtask'
|
|
3
3
|
require 'rdoc/task'
|
4
4
|
|
5
5
|
desc 'Default: run all tests'
|
6
|
-
task :
|
6
|
+
task default: :test
|
7
7
|
|
8
8
|
Bundler::GemHelper.install_tasks
|
9
9
|
|
@@ -23,7 +23,3 @@ Rake::RDocTask.new(:rdoc) do |rdoc|
|
|
23
23
|
rdoc.rdoc_files.include('HISTORY.rdoc')
|
24
24
|
rdoc.rdoc_files.include('lib/**/*.rb')
|
25
25
|
end
|
26
|
-
|
27
|
-
|
28
|
-
|
29
|
-
|
data/bin/rewritten-dump.rb
CHANGED
@@ -6,40 +6,37 @@ require 'optparse'
|
|
6
6
|
require 'rewritten'
|
7
7
|
|
8
8
|
options = {
|
9
|
-
:
|
10
|
-
:
|
9
|
+
out: 'rewritten.csv',
|
10
|
+
verbose: false
|
11
11
|
}
|
12
12
|
|
13
13
|
op = OptionParser.new do |opts|
|
14
|
-
opts.banner =
|
14
|
+
opts.banner = 'Usage: rewriten-dump.rb [options]'
|
15
15
|
|
16
|
-
opts.on(
|
16
|
+
opts.on('-v', '--verbose', 'be more verbose') do
|
17
17
|
options[:verbose] = true
|
18
18
|
end
|
19
19
|
|
20
|
-
opts.on(
|
20
|
+
opts.on('-o', '--out FILE', 'output file or "-" for stdout') do |o|
|
21
21
|
options[:out] = o
|
22
22
|
end
|
23
23
|
|
24
|
-
opts.on(
|
24
|
+
opts.on('-u', '--uri URI', 'uri to the redis db') do |uri|
|
25
25
|
options[:uri] = uri
|
26
26
|
end
|
27
27
|
|
28
|
-
opts.on(
|
28
|
+
opts.on('-h', '--help', 'print help') do
|
29
29
|
puts opts
|
30
30
|
exit 0
|
31
31
|
end
|
32
|
-
|
33
32
|
end
|
34
33
|
op.parse!
|
35
34
|
|
36
|
-
Rewritten.redis = options[:uri] if options[:uri]
|
35
|
+
Rewritten.redis = options[:uri] if options[:uri]
|
37
36
|
|
38
|
-
file = options[:out] ==
|
37
|
+
file = options[:out] == '-' ? STDOUT : File.open(options[:out], 'w')
|
39
38
|
|
40
|
-
file.puts
|
39
|
+
file.puts '#from;to'
|
41
40
|
Rewritten.all_tos.each do |to|
|
42
|
-
file.puts Rewritten.get_all_translations(to).map{|from| "#{Rewritten.full_line(from)};#{to}"}.join("\n")
|
41
|
+
file.puts Rewritten.get_all_translations(to).map { |from| "#{Rewritten.full_line(from)};#{to}" }.join("\n")
|
43
42
|
end
|
44
|
-
|
45
|
-
|
data/bin/rewritten-import.rb
CHANGED
@@ -7,28 +7,27 @@ require 'rewritten'
|
|
7
7
|
require 'multi_json'
|
8
8
|
|
9
9
|
options = {
|
10
|
-
:
|
10
|
+
drop: false
|
11
11
|
}
|
12
12
|
|
13
13
|
op = OptionParser.new do |opts|
|
14
|
-
opts.banner =
|
14
|
+
opts.banner = 'Usage: rewriten-dump.rb [options]'
|
15
15
|
|
16
|
-
opts.on(
|
16
|
+
opts.on('-v', '--verbose', 'be more verbose') do |v|
|
17
17
|
options[:verbose] = v
|
18
18
|
end
|
19
19
|
|
20
|
-
opts.on(
|
20
|
+
opts.on('-f', '--file FILE', 'input file') do |o|
|
21
21
|
options[:file] = o
|
22
22
|
end
|
23
23
|
|
24
|
-
opts.on(
|
24
|
+
opts.on('-u', '--uri URI', 'uri to the redis db') do |uri|
|
25
25
|
options[:uri] = uri
|
26
26
|
end
|
27
27
|
|
28
|
-
opts.on(
|
28
|
+
opts.on('-d', '--drop', 'drop translations first') do
|
29
29
|
options[:drop] = true
|
30
30
|
end
|
31
|
-
|
32
31
|
end
|
33
32
|
|
34
33
|
op.parse!
|
@@ -38,15 +37,13 @@ unless options[:file]
|
|
38
37
|
exit
|
39
38
|
end
|
40
39
|
|
41
|
-
Rewritten.redis = options[:uri] if options[:uri]
|
40
|
+
Rewritten.redis = options[:uri] if options[:uri]
|
42
41
|
|
43
42
|
Rewritten.clear_translations if options[:drop]
|
44
43
|
|
45
44
|
File.open(options[:file]).each do |line|
|
46
45
|
next if line =~ /^#/
|
47
|
-
from,to = line.split(
|
46
|
+
from, to = line.split(';')
|
48
47
|
puts "adding #{from} -> #{to}" if options[:verbose]
|
49
|
-
Rewritten.add_translation(from,to.chomp)
|
48
|
+
Rewritten.add_translation(from, to.chomp)
|
50
49
|
end
|
51
|
-
|
52
|
-
|
data/bin/rewritten-web.rb
CHANGED
@@ -6,20 +6,18 @@ begin
|
|
6
6
|
rescue LoadError
|
7
7
|
require 'rubygems'
|
8
8
|
require 'vegas'
|
9
|
-
end
|
9
|
+
end
|
10
10
|
|
11
11
|
require 'rewritten/server'
|
12
12
|
|
13
13
|
Vegas::Runner.new(Rewritten::Server, 'rewritten-web', {
|
14
|
-
|
15
|
-
|
16
|
-
|
17
|
-
|
18
|
-
}) do |runner, opts,
|
19
|
-
opts.on('-N NAMESPACE',
|
14
|
+
before_run: lambda {|v|
|
15
|
+
path = (ENV['RESQUECONFIG'] || v.args.first)
|
16
|
+
load path.to_s.strip if path
|
17
|
+
}
|
18
|
+
}) do |runner, opts, _app|
|
19
|
+
opts.on('-N NAMESPACE', '--namespace NAMESPACE', 'set the Redis namespace') {|namespace|
|
20
20
|
runner.logger.info "Using Redis namespace '#{namespace}'"
|
21
21
|
Rewritten.redis.namespace = namespace
|
22
22
|
}
|
23
23
|
end
|
24
|
-
|
25
|
-
|
data/config.ru
CHANGED
data/lib/rack/canonical.rb
CHANGED
@@ -1,11 +1,8 @@
|
|
1
1
|
require 'rack'
|
2
2
|
|
3
3
|
module Rack
|
4
|
-
|
5
4
|
module Rewritten
|
6
|
-
|
7
5
|
class Canonical
|
8
|
-
|
9
6
|
def initialize(app)
|
10
7
|
@app = app
|
11
8
|
end
|
@@ -15,30 +12,25 @@ module Rack
|
|
15
12
|
|
16
13
|
status, headers, response = @app.call(req.env)
|
17
14
|
|
18
|
-
if status == 200 && headers[
|
19
|
-
body =
|
15
|
+
if status == 200 && headers['Content-Type'] =~ /text\/html|application\/xhtml\+xml/
|
16
|
+
body = ''
|
20
17
|
response.each { |part| body << part }
|
21
|
-
index = body.rindex(
|
18
|
+
index = body.rindex('</head>')
|
22
19
|
if index
|
23
20
|
# go with a request duplicate since infinitive works on translations
|
24
21
|
target_req = req.dup
|
25
|
-
target_req.path_info = ::Rewritten.infinitive(
|
22
|
+
target_req.path_info = ::Rewritten.infinitive(::Rewritten.get_current_translation(req.path))
|
26
23
|
target_req.env['QUERY_STRING'] = ''
|
27
24
|
target = target_req.url
|
28
25
|
|
29
|
-
body.insert(index, %
|
30
|
-
headers[
|
26
|
+
body.insert(index, %(<link rel="canonical" href="#{target}"/>))
|
27
|
+
headers['Content-Length'] = body.length.to_s
|
31
28
|
response = [body]
|
32
29
|
end
|
33
30
|
|
34
31
|
end
|
35
32
|
[status, headers, response]
|
36
33
|
end
|
37
|
-
|
38
34
|
end
|
39
|
-
|
40
35
|
end
|
41
|
-
|
42
36
|
end
|
43
|
-
|
44
|
-
|
data/lib/rack/dummy.rb
CHANGED
@@ -1,25 +1,18 @@
|
|
1
1
|
require 'rack'
|
2
2
|
|
3
3
|
module Rack
|
4
|
-
|
5
4
|
class Dummy
|
6
|
-
|
7
5
|
def call(env)
|
8
|
-
puts
|
6
|
+
puts '-> Rack::Dummy'
|
9
7
|
lines = []
|
10
8
|
req = Rack::Request.new(env)
|
11
|
-
lines <<
|
12
|
-
lines <<
|
13
|
-
lines <<
|
14
|
-
lines <<
|
15
|
-
lines <<
|
9
|
+
lines << req.path
|
10
|
+
lines << req.params.inspect
|
11
|
+
lines << req.host
|
12
|
+
lines << req.env.inspect
|
13
|
+
lines << "SUBDOMAIN: #{env['SUBDOMAIN']}"
|
16
14
|
lines << '<a href="/some/resource">'
|
17
|
-
[200, {
|
15
|
+
[200, { 'Content-Type' => 'text/plain' }, lines.join("\n")]
|
18
16
|
end
|
19
|
-
|
20
17
|
end
|
21
|
-
|
22
18
|
end
|
23
|
-
|
24
|
-
|
25
|
-
|
data/lib/rack/html.rb
CHANGED
@@ -1,11 +1,8 @@
|
|
1
1
|
require 'rack'
|
2
2
|
|
3
3
|
module Rack
|
4
|
-
|
5
4
|
module Rewritten
|
6
|
-
|
7
5
|
class Html
|
8
|
-
|
9
6
|
def initialize(app)
|
10
7
|
@app = app
|
11
8
|
end
|
@@ -20,15 +17,13 @@ module Rack
|
|
20
17
|
links = line.scan(/href="([^"]+)"/).flatten.uniq
|
21
18
|
res = line
|
22
19
|
links.each do |link|
|
23
|
-
|
24
20
|
if ::Rewritten.exist_translation_for?(link)
|
25
21
|
t = ::Rewritten.get_current_translation(link)
|
26
|
-
res.gsub!(%
|
27
|
-
res.gsub!(%
|
28
|
-
res.gsub!(%
|
29
|
-
res.gsub!(%
|
22
|
+
res.gsub!(%(href="#{link}"), %(href="#{t}")) if t
|
23
|
+
res.gsub!(%(href='#{link}'), %(href='#{t}')) if t
|
24
|
+
res.gsub!(%(href="#{link}?), %(href="#{t}?)) if t
|
25
|
+
res.gsub!(%(href='#{link}?), %(href='#{t}?)) if t
|
30
26
|
end
|
31
|
-
|
32
27
|
end
|
33
28
|
new_response << res
|
34
29
|
end
|
@@ -38,10 +33,6 @@ module Rack
|
|
38
33
|
|
39
34
|
[status, headers, new_response]
|
40
35
|
end
|
41
|
-
|
42
36
|
end
|
43
|
-
|
44
37
|
end
|
45
38
|
end
|
46
|
-
|
47
|
-
|
data/lib/rack/record.rb
CHANGED
@@ -1,11 +1,8 @@
|
|
1
1
|
require 'rack'
|
2
2
|
|
3
3
|
module Rack
|
4
|
-
|
5
4
|
module Rewritten
|
6
|
-
|
7
5
|
class Record
|
8
|
-
|
9
6
|
def initialize(app)
|
10
7
|
@app = app
|
11
8
|
end
|
@@ -13,18 +10,15 @@ module Rack
|
|
13
10
|
def call(env)
|
14
11
|
req = Rack::Request.new(env.clone)
|
15
12
|
status, headers, response = @app.call(env)
|
16
|
-
puts
|
13
|
+
puts '-> Rack::Rewritten::Record'
|
17
14
|
puts headers.inspect
|
18
15
|
|
19
|
-
if [200,301,302].include?(status)
|
20
|
-
::Rewritten.add_hit(req.path,status, headers[
|
16
|
+
if [200, 301, 302].include?(status)
|
17
|
+
::Rewritten.add_hit(req.path, status, headers['Content-Type']) if headers['Content-Type'] =~ /text\/html/
|
21
18
|
end
|
22
19
|
|
23
20
|
[status, headers, response]
|
24
21
|
end
|
25
|
-
|
26
22
|
end
|
27
|
-
|
28
23
|
end
|
29
|
-
|
30
24
|
end
|