rack-locale-root-redirect 0.3.1 → 0.4
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/.rubocop.yml +65 -0
- data/.travis.yml +3 -1
- data/README.md +2 -2
- data/lib/rack/locale_root_redirect/middleware.rb +16 -8
- data/lib/rack/locale_root_redirect/version.rb +1 -1
- data/rack-locale-root-redirect.gemspec +2 -0
- data/spec/rack/locale_root_redirect_spec.rb +31 -19
- data/spec/spec_helper.rb +1 -1
- metadata +32 -3
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 3eccc6e9e67d15de692433b015ef649127cbfc37
|
4
|
+
data.tar.gz: d3212f53bbbed10b42b7aaf4e394c2ec32f43beb
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 9a8ddc372ab7d393c2ba1cc0db021bff0757a9207bdabbaaec7fee6106adbb1c49d041d8081fa73d4a3180dce7bc0a4092fa807a84f72473d5a0afc5691ba0cf
|
7
|
+
data.tar.gz: a37684489115c88e8dcf76601f026dea4b579fcd8864e3508f9a104f308b8c19dc74fc42fd4b54e0a77df1f3ab8b28376c1c7548d856746c69f9748f8a5e5ea2
|
data/.rubocop.yml
ADDED
@@ -0,0 +1,65 @@
|
|
1
|
+
AllCops:
|
2
|
+
Exclude:
|
3
|
+
- rack-locale-root-redirect.gemspec
|
4
|
+
|
5
|
+
Documentation:
|
6
|
+
Enabled: false
|
7
|
+
|
8
|
+
Encoding:
|
9
|
+
Enabled: false
|
10
|
+
|
11
|
+
LineLength:
|
12
|
+
Max: 200
|
13
|
+
|
14
|
+
AccessModifierIndentation:
|
15
|
+
EnforcedStyle: outdent
|
16
|
+
|
17
|
+
IfUnlessModifier:
|
18
|
+
Enabled: false
|
19
|
+
|
20
|
+
CaseIndentation:
|
21
|
+
IndentWhenRelativeTo: case
|
22
|
+
IndentOneStep: true
|
23
|
+
|
24
|
+
MethodLength:
|
25
|
+
CountComments: false
|
26
|
+
Max: 20
|
27
|
+
|
28
|
+
SignalException:
|
29
|
+
Enabled: false
|
30
|
+
|
31
|
+
ColonMethodCall:
|
32
|
+
Enabled: false
|
33
|
+
|
34
|
+
AsciiComments:
|
35
|
+
Enabled: false
|
36
|
+
|
37
|
+
RegexpLiteral:
|
38
|
+
Enabled: false
|
39
|
+
|
40
|
+
AssignmentInCondition:
|
41
|
+
Enabled: false
|
42
|
+
|
43
|
+
ParameterLists:
|
44
|
+
CountKeywordArgs: false
|
45
|
+
|
46
|
+
SingleLineBlockParams:
|
47
|
+
Methods:
|
48
|
+
- reduce:
|
49
|
+
- memo
|
50
|
+
- item
|
51
|
+
|
52
|
+
Metrics/AbcSize:
|
53
|
+
Enabled: false
|
54
|
+
|
55
|
+
Style/CollectionMethods:
|
56
|
+
Enabled: true
|
57
|
+
|
58
|
+
Style/SymbolArray:
|
59
|
+
Enabled: true
|
60
|
+
|
61
|
+
Style/ExtraSpacing:
|
62
|
+
Enabled: true
|
63
|
+
|
64
|
+
Style/FileName:
|
65
|
+
Enabled: false
|
data/.travis.yml
CHANGED
data/README.md
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
# Rack::LocaleRootRedirect
|
2
2
|
|
3
|
-
|
4
|
-
|
3
|
+
<a href="https://rubygems.org/gems/rack-locale-root-redirect"><img src="http://img.shields.io/gem/v/rack-locale-root-redirect.svg" /></a>
|
4
|
+
<a href="https://travis-ci.org/mirego/rack-locale-root-redirect"><img src="http://img.shields.io/travis/mirego/rack-locale-root-redirect.svg" /></a>
|
5
5
|
|
6
6
|
`Rack::LocaleRootRedirect` redirects requests to `"/"` based on the `Accept-Language` HTTP header.
|
7
7
|
|
@@ -1,8 +1,9 @@
|
|
1
1
|
module Rack
|
2
2
|
class LocaleRootRedirect
|
3
3
|
STATUS = 302
|
4
|
-
|
4
|
+
ROOT_REQUEST_REGEX = %r{\A/(?<query_string>\?.*|\Z)}
|
5
5
|
RACK_ACCEPT_MISSING = 'Rack::LocaleRootRedirect must be used after Rack::Accept. Please make your application use Rack::Accept before Rack::LocaleRootRedirect.'
|
6
|
+
REDIRECTED_RESPONSE_REGEX = %r{\A3\d\d\Z}
|
6
7
|
|
7
8
|
# @private
|
8
9
|
def initialize(app, locales = {})
|
@@ -17,7 +18,7 @@ module Rack
|
|
17
18
|
def call(env)
|
18
19
|
status, headers, response = @app.call(env)
|
19
20
|
|
20
|
-
if
|
21
|
+
if should_redirect?(env, status)
|
21
22
|
locale = best_locale(env)
|
22
23
|
|
23
24
|
status = STATUS
|
@@ -32,20 +33,27 @@ module Rack
|
|
32
33
|
protected
|
33
34
|
|
34
35
|
# Return whether we must act on this request
|
36
|
+
def should_redirect?(env, status)
|
37
|
+
!redirected_response?(status) && root_request?(env)
|
38
|
+
end
|
39
|
+
|
40
|
+
# Return whether the request was on the root endpoint (`/`)
|
35
41
|
def root_request?(env)
|
36
|
-
|
42
|
+
ROOT_REQUEST_REGEX.match(env['PATH_INFO'])
|
43
|
+
end
|
44
|
+
|
45
|
+
# Return whether the response we’re altering is already a redirection
|
46
|
+
def redirected_response?(status)
|
47
|
+
REDIRECTED_RESPONSE_REGEX.match(status.to_s)
|
37
48
|
end
|
38
49
|
|
39
50
|
# Return the best locale to redirect to based on the request enviroment
|
40
51
|
def best_locale(env)
|
41
52
|
if accept = env['rack-accept.request']
|
42
|
-
matcher = accept.language.tap
|
43
|
-
matcher.first_level_match = true
|
44
|
-
end
|
45
|
-
|
53
|
+
matcher = accept.language.tap { |m| m.first_level_match = true }
|
46
54
|
matcher.best_of(@available_locales) || @default_locale
|
47
55
|
else
|
48
|
-
raise StandardError
|
56
|
+
raise StandardError, RACK_ACCEPT_MISSING
|
49
57
|
end
|
50
58
|
end
|
51
59
|
end
|
@@ -1,38 +1,50 @@
|
|
1
1
|
require 'spec_helper'
|
2
2
|
|
3
3
|
describe Rack::LocaleRootRedirect do
|
4
|
-
let(:app) { proc{[200,{},['Hello, world.']]} }
|
5
4
|
let(:request) { Rack::MockRequest.new(stack) }
|
5
|
+
let(:locales) { { fr: '/fr', en: '/en' } }
|
6
6
|
|
7
7
|
context 'with stack using Rack::Accept' do
|
8
8
|
let(:stack) { Rack::LocaleRootRedirect.new(Rack::Accept.new(app), locales) }
|
9
|
+
let(:response) { request.get('/?foo=bar', 'HTTP_ACCEPT_LANGUAGE' => accept_language.join(',')) }
|
9
10
|
|
10
|
-
|
11
|
-
let(:
|
12
|
-
let(:response) { request.get('/?foo=bar', 'HTTP_ACCEPT_LANGUAGE' => accept_language.join(',')) }
|
11
|
+
context 'with response already being redirected' do
|
12
|
+
let(:app) { proc { [301, { 'Location' => '/new-place' }, ['You are being redirected...']] } }
|
13
13
|
|
14
|
-
|
15
|
-
let(:accept_language) { %w
|
16
|
-
it { expect(response.headers['Location']).to eq '/
|
17
|
-
it { expect(response.headers['Vary']).to
|
18
|
-
end
|
19
|
-
|
20
|
-
context 'with second matching language' do
|
21
|
-
let(:accept_language) { %w{es en;q=0.8} }
|
22
|
-
it { expect(response.headers['Location']).to eq '/en?foo=bar' }
|
23
|
-
it { expect(response.headers['Vary']).to eq 'Accept-Language'}
|
14
|
+
describe 'response headers' do
|
15
|
+
let(:accept_language) { %w() }
|
16
|
+
it { expect(response.headers['Location']).to eq '/new-place' }
|
17
|
+
it { expect(response.headers['Vary']).to be_nil }
|
24
18
|
end
|
19
|
+
end
|
25
20
|
|
26
|
-
|
27
|
-
|
28
|
-
|
29
|
-
|
21
|
+
context 'with response not already being redirected' do
|
22
|
+
let(:app) { proc { [200, {}, ['Hello, world.']] } }
|
23
|
+
|
24
|
+
describe 'response headers' do
|
25
|
+
context 'with first matching language' do
|
26
|
+
let(:accept_language) { %w(en es;q=0.9) }
|
27
|
+
it { expect(response.headers['Location']).to eq '/en?foo=bar' }
|
28
|
+
it { expect(response.headers['Vary']).to eq 'Accept-Language' }
|
29
|
+
end
|
30
|
+
|
31
|
+
context 'with second matching language' do
|
32
|
+
let(:accept_language) { %w(es en;q=0.8) }
|
33
|
+
it { expect(response.headers['Location']).to eq '/en?foo=bar' }
|
34
|
+
it { expect(response.headers['Vary']).to eq 'Accept-Language' }
|
35
|
+
end
|
36
|
+
|
37
|
+
context 'with default matching language' do
|
38
|
+
let(:accept_language) { %w(es jp;q=0.8) }
|
39
|
+
it { expect(response.headers['Location']).to eq '/fr?foo=bar' }
|
40
|
+
it { expect(response.headers['Vary']).to eq 'Accept-Language' }
|
41
|
+
end
|
30
42
|
end
|
31
43
|
end
|
32
44
|
end
|
33
45
|
|
34
46
|
context 'with stack without Rack::Accept' do
|
35
|
-
let(:
|
47
|
+
let(:app) { proc { [200, {}, ['Hello, world.']] } }
|
36
48
|
let(:stack) { Rack::LocaleRootRedirect.new(app, locales) }
|
37
49
|
|
38
50
|
specify do
|
data/spec/spec_helper.rb
CHANGED
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: rack-locale-root-redirect
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: '0.4'
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Rémi Prévost
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2015-
|
11
|
+
date: 2015-05-25 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: rack-accept
|
@@ -52,6 +52,34 @@ dependencies:
|
|
52
52
|
- - ">="
|
53
53
|
- !ruby/object:Gem::Version
|
54
54
|
version: '0'
|
55
|
+
- !ruby/object:Gem::Dependency
|
56
|
+
name: rubocop
|
57
|
+
requirement: !ruby/object:Gem::Requirement
|
58
|
+
requirements:
|
59
|
+
- - '='
|
60
|
+
- !ruby/object:Gem::Version
|
61
|
+
version: '0.29'
|
62
|
+
type: :development
|
63
|
+
prerelease: false
|
64
|
+
version_requirements: !ruby/object:Gem::Requirement
|
65
|
+
requirements:
|
66
|
+
- - '='
|
67
|
+
- !ruby/object:Gem::Version
|
68
|
+
version: '0.29'
|
69
|
+
- !ruby/object:Gem::Dependency
|
70
|
+
name: phare
|
71
|
+
requirement: !ruby/object:Gem::Requirement
|
72
|
+
requirements:
|
73
|
+
- - ">="
|
74
|
+
- !ruby/object:Gem::Version
|
75
|
+
version: '0'
|
76
|
+
type: :development
|
77
|
+
prerelease: false
|
78
|
+
version_requirements: !ruby/object:Gem::Requirement
|
79
|
+
requirements:
|
80
|
+
- - ">="
|
81
|
+
- !ruby/object:Gem::Version
|
82
|
+
version: '0'
|
55
83
|
description: Rack::LocaleRootRedirect uses Rack:Accept to map '/' to a path based
|
56
84
|
on the `Accept-Language` HTTP header.
|
57
85
|
email:
|
@@ -62,6 +90,7 @@ extra_rdoc_files: []
|
|
62
90
|
files:
|
63
91
|
- ".gitignore"
|
64
92
|
- ".rspec"
|
93
|
+
- ".rubocop.yml"
|
65
94
|
- ".travis.yml"
|
66
95
|
- Gemfile
|
67
96
|
- LICENSE.md
|
@@ -93,7 +122,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
93
122
|
version: '0'
|
94
123
|
requirements: []
|
95
124
|
rubyforge_project:
|
96
|
-
rubygems_version: 2.
|
125
|
+
rubygems_version: 2.4.5
|
97
126
|
signing_key:
|
98
127
|
specification_version: 4
|
99
128
|
summary: Rack::LocaleRootRedirect uses Rack:Accept to map '/' to a path based on the
|