rack-locale-root-redirect 0.3.1 → 0.4

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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 1d2d5a0277e85cb40191e22c43805c04c61686b6
4
- data.tar.gz: ff459afe51d1e6f138e6b8b993b1278afdd2b6d6
3
+ metadata.gz: 3eccc6e9e67d15de692433b015ef649127cbfc37
4
+ data.tar.gz: d3212f53bbbed10b42b7aaf4e394c2ec32f43beb
5
5
  SHA512:
6
- metadata.gz: 5917d7929baaa7ba25c09cd9d23a06f4b09bb03cdb355f5c0283f9745cde0614484693c7547f2b6ef487e2a7e18933d2de53273654feab94b94befbb6bd02737
7
- data.tar.gz: c94307202a975a43d1dc18573700573cbdad25e1c36640ca1a6d7e7bf51e4538ef1f9bb133584d501f94432df48dd8aff29deb53ab549ed9cedce8a44a8680a3
6
+ metadata.gz: 9a8ddc372ab7d393c2ba1cc0db021bff0757a9207bdabbaaec7fee6106adbb1c49d041d8081fa73d4a3180dce7bc0a4092fa807a84f72473d5a0afc5691ba0cf
7
+ data.tar.gz: a37684489115c88e8dcf76601f026dea4b579fcd8864e3508f9a104f308b8c19dc74fc42fd4b54e0a77df1f3ab8b28376c1c7548d856746c69f9748f8a5e5ea2
@@ -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
@@ -7,7 +7,9 @@ rvm:
7
7
 
8
8
  sudo: false
9
9
 
10
- script: "echo 'DO IT' && bundle exec rake spec"
10
+ script:
11
+ - 'echo "Checking code style" && bundle exec phare'
12
+ - 'echo "Running tests" && bundle exec rake spec'
11
13
 
12
14
  notifications:
13
15
  hipchat:
data/README.md CHANGED
@@ -1,7 +1,7 @@
1
1
  # Rack::LocaleRootRedirect
2
2
 
3
- [![Build Status](https://travis-ci.org/mirego/rack-locale-root-redirect.png?branch=master)](https://travis-ci.org/mirego/rack-locale-root-redirect)
4
- [![Ruby Gem](https://badge.fury.io/rb/rack-locale-root-redirect.png)](https://rubygems.org/gems/microscope)
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
- REGEX = %r[\A/(?<query_string>\?.*|\Z)]
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 root_request?(env)
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
- REGEX.match(env['PATH_INFO'])
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 do |matcher|
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.new(RACK_ACCEPT_MISSING)
56
+ raise StandardError, RACK_ACCEPT_MISSING
49
57
  end
50
58
  end
51
59
  end
@@ -1,5 +1,5 @@
1
1
  module Rack
2
2
  class LocaleRootRedirect
3
- VERSION = '0.3.1'
3
+ VERSION = '0.4'
4
4
  end
5
5
  end
@@ -22,4 +22,6 @@ Gem::Specification.new do |gem|
22
22
 
23
23
  gem.add_development_dependency 'rspec', '~> 3.1'
24
24
  gem.add_development_dependency 'rake'
25
+ gem.add_development_dependency 'rubocop', '0.29'
26
+ gem.add_development_dependency 'phare'
25
27
  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
- describe 'response' do
11
- let(:locales) { { fr: '/fr', en: '/en' } }
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
- context 'with first matching language' do
15
- let(:accept_language) { %w{en es;q=0.9} }
16
- it { expect(response.headers['Location']).to eq '/en?foo=bar' }
17
- it { expect(response.headers['Vary']).to eq 'Accept-Language'}
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
- context 'with default matching language' do
27
- let(:accept_language) { %w{es jp;q=0.8} }
28
- it { expect(response.headers['Location']).to eq '/fr?foo=bar' }
29
- it { expect(response.headers['Vary']).to eq 'Accept-Language'}
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(:locales) { { fr: '/fr', en: '/en' } }
47
+ let(:app) { proc { [200, {}, ['Hello, world.']] } }
36
48
  let(:stack) { Rack::LocaleRootRedirect.new(app, locales) }
37
49
 
38
50
  specify do
@@ -1,4 +1,4 @@
1
- $:.unshift File.expand_path('../lib', __FILE__)
1
+ $LOAD_PATH.unshift File.expand_path('../lib', __FILE__)
2
2
 
3
3
  # We test using RSpec
4
4
  require 'rspec'
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.3.1
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-03-02 00:00:00.000000000 Z
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.2.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