rack-locale-root-redirect 0.1 → 0.2
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/.rspec +1 -0
- data/LICENSE.md +26 -0
- data/README.md +21 -11
- data/Rakefile +11 -1
- data/lib/rack/locale-root-redirect.rb +1 -29
- data/lib/rack/locale_root_redirect.rb +35 -0
- data/rack-locale-root-redirect.gemspec +10 -5
- data/spec/rack/locale_root_redirect_spec.rb +27 -0
- data/spec/spec_helper.rb +10 -0
- metadata +63 -18
- data/LICENSE.txt +0 -22
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: 1881606ef44a9609f795c8e3d56e11baa446910e
|
4
|
+
data.tar.gz: 53e0b9a8c5a57afffff1751fd9f34d1e8b353164
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: bf52f7c8c5eb66f40d073953fea0ae499d48477b7e707a46fc324480e5e15945e89b969dd50e10fe6082c935beb41b9c4485310622b92cb9eba798772d932dfc
|
7
|
+
data.tar.gz: 327c039d77f5666750c6a0eb39e8caf6396d918fbfbe6fdbb6cf5e3639c2de8e8eed4b53ca48316dc50cf1544775fcdee4ec8cf5e2bfa53c5e601240da0a15d2
|
data/.rspec
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
--colour
|
data/LICENSE.md
ADDED
@@ -0,0 +1,26 @@
|
|
1
|
+
Copyright (c) 2013, Mirego
|
2
|
+
All rights reserved.
|
3
|
+
|
4
|
+
Redistribution and use in source and binary forms, with or without
|
5
|
+
modification, are permitted provided that the following conditions are met:
|
6
|
+
|
7
|
+
- Redistributions of source code must retain the above copyright notice,
|
8
|
+
this list of conditions and the following disclaimer.
|
9
|
+
- Redistributions in binary form must reproduce the above copyright notice,
|
10
|
+
this list of conditions and the following disclaimer in the documentation
|
11
|
+
and/or other materials provided with the distribution.
|
12
|
+
- Neither the name of the Mirego nor the names of its contributors may
|
13
|
+
be used to endorse or promote products derived from this software without
|
14
|
+
specific prior written permission.
|
15
|
+
|
16
|
+
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
|
17
|
+
AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
18
|
+
IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
19
|
+
ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
|
20
|
+
LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
|
21
|
+
CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
|
22
|
+
SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
|
23
|
+
INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
|
24
|
+
CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
|
25
|
+
ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
|
26
|
+
POSSIBILITY OF SUCH DAMAGE.
|
data/README.md
CHANGED
@@ -7,7 +7,7 @@
|
|
7
7
|
Add this line to your application’s Gemfile:
|
8
8
|
|
9
9
|
```ruby
|
10
|
-
gem 'rack-locale-root-redirect'
|
10
|
+
gem 'rack-locale-root-redirect', require: 'rack/locale-root-redirect'
|
11
11
|
```
|
12
12
|
|
13
13
|
And then execute:
|
@@ -22,9 +22,9 @@ With Sinatra:
|
|
22
22
|
|
23
23
|
```ruby
|
24
24
|
# Gemfile
|
25
|
-
gem
|
26
|
-
gem
|
27
|
-
gem
|
25
|
+
gem 'sinatra'
|
26
|
+
gem 'rack-accept', require: 'rack/accept'
|
27
|
+
gem 'rack-locale-root-redirect', require: 'rack/locale-root-redirect'
|
28
28
|
|
29
29
|
# config.ru
|
30
30
|
require 'bundler'
|
@@ -32,10 +32,10 @@ Bundler.require
|
|
32
32
|
|
33
33
|
class MyApp < Sinatra::Base
|
34
34
|
use Rack::Accept
|
35
|
-
use Rack::LocaleRootRedirect, :
|
35
|
+
use Rack::LocaleRootRedirect, fr: "/fr", en: "/en"
|
36
36
|
|
37
|
-
get(
|
38
|
-
get(
|
37
|
+
get('/fr') { 'Français!' }
|
38
|
+
get('/en') { 'English!' }
|
39
39
|
end
|
40
40
|
|
41
41
|
run MyApp
|
@@ -46,11 +46,21 @@ Then, test it:
|
|
46
46
|
```shell
|
47
47
|
$ rackup &
|
48
48
|
|
49
|
-
$ curl -sI "http://0.0.0.0:9292" -H "Accept-Language: fr;q=1, en;q=0.8" | grep "
|
50
|
-
HTTP/1.1
|
49
|
+
$ curl -sI "http://0.0.0.0:9292" -H "Accept-Language: fr;q=1, en;q=0.8" | grep "302\|Location"
|
50
|
+
HTTP/1.1 302 Moved Permanently
|
51
51
|
Location: /fr
|
52
52
|
|
53
|
-
$ curl -sI "http://0.0.0.0:9292" -H "Accept-Language: fr;q=0.4, en;q=0.8" | grep "
|
54
|
-
HTTP/1.1
|
53
|
+
$ curl -sI "http://0.0.0.0:9292" -H "Accept-Language: fr;q=0.4, en;q=0.8" | grep "302\|Location"
|
54
|
+
HTTP/1.1 302 Moved Permanently
|
55
55
|
Location: /en
|
56
56
|
```
|
57
|
+
|
58
|
+
## License
|
59
|
+
|
60
|
+
`Rack::LocaleRootRedirect` is © 2013 [Mirego](http://www.mirego.com) and may be freely distributed under the [New BSD license](http://opensource.org/licenses/BSD-3-Clause). See the [`LICENSE.md`](https://github.com/mirego/rack-locale-root-redirect/blob/master/LICENSE.md) file.
|
61
|
+
|
62
|
+
## About Mirego
|
63
|
+
|
64
|
+
Mirego is a team of passionate people who believe that work is a place where you can innovate and have fun. We proudly build mobile applications for [iPhone](http://mirego.com/en/iphone-app-development/ "iPhone application development"), [iPad](http://mirego.com/en/ipad-app-development/ "iPad application development"), [Android](http://mirego.com/en/android-app-development/ "Android application development"), [Blackberry](http://mirego.com/en/blackberry-app-development/ "Blackberry application development"), [Windows Phone](http://mirego.com/en/windows-phone-app-development/ "Windows Phone application development") and [Windows 8](http://mirego.com/en/windows-8-app-development/ "Windows 8 application development") in beautiful Quebec City.
|
65
|
+
|
66
|
+
We also love [open-source software](http://open.mirego.com/) and we try to extract as much code as possible from our projects to give back to the community.
|
data/Rakefile
CHANGED
@@ -1 +1,11 @@
|
|
1
|
-
require
|
1
|
+
require 'bundler'
|
2
|
+
require 'rake'
|
3
|
+
require 'bundler/gem_tasks'
|
4
|
+
require 'rspec/core/rake_task'
|
5
|
+
|
6
|
+
task default: :spec
|
7
|
+
|
8
|
+
desc 'Run all specs'
|
9
|
+
RSpec::Core::RakeTask.new(:spec) do |task|
|
10
|
+
task.pattern = 'spec/**/*_spec.rb'
|
11
|
+
end
|
@@ -1,29 +1 @@
|
|
1
|
-
require
|
2
|
-
|
3
|
-
module Rack
|
4
|
-
class LocaleRootRedirect
|
5
|
-
VERSION = "0.1"
|
6
|
-
|
7
|
-
def initialize(app, locales = {})
|
8
|
-
@locales = locales
|
9
|
-
@available_locales = locales.keys.map(&:to_s)
|
10
|
-
@default_locale = @available_locales.first
|
11
|
-
@app = app
|
12
|
-
end
|
13
|
-
|
14
|
-
def call(env)
|
15
|
-
status, headers, response = @app.call(env)
|
16
|
-
|
17
|
-
if match_data = %r[\A/(?<query_string>\?.*|\Z)].match(env["REQUEST_URI"])
|
18
|
-
language_matcher = env['rack-accept.request'].language
|
19
|
-
language_matcher.first_level_match = true
|
20
|
-
redirect_lang = language_matcher.best_of(@available_locales) || @default_locale
|
21
|
-
|
22
|
-
status = 302
|
23
|
-
headers["Location"] = @locales[redirect_lang.to_sym] + match_data[:query_string]
|
24
|
-
end
|
25
|
-
|
26
|
-
[status, headers, response]
|
27
|
-
end
|
28
|
-
end
|
29
|
-
end
|
1
|
+
require 'rack/locale_root_redirect'
|
@@ -0,0 +1,35 @@
|
|
1
|
+
require 'rack/accept'
|
2
|
+
|
3
|
+
module Rack
|
4
|
+
class LocaleRootRedirect
|
5
|
+
VERSION = '0.2'
|
6
|
+
|
7
|
+
def initialize(app, locales = {})
|
8
|
+
@locales = locales
|
9
|
+
@available_locales = locales.keys.map(&:to_s)
|
10
|
+
@default_locale = @available_locales.first
|
11
|
+
@app = app
|
12
|
+
end
|
13
|
+
|
14
|
+
def call(env)
|
15
|
+
status, headers, response = @app.call(env)
|
16
|
+
|
17
|
+
if root_request?(env)
|
18
|
+
language_matcher = env['rack-accept.request'].language.tap { |m| m.first_level_match = true }
|
19
|
+
redirect_lang = language_matcher.best_of(@available_locales) || @default_locale
|
20
|
+
|
21
|
+
status = 302
|
22
|
+
query_string = env['QUERY_STRING'] ? "?#{env['QUERY_STRING']}" : ''
|
23
|
+
headers['Location'] = @locales[redirect_lang.to_sym] + query_string
|
24
|
+
end
|
25
|
+
|
26
|
+
[status, headers, response]
|
27
|
+
end
|
28
|
+
|
29
|
+
protected
|
30
|
+
|
31
|
+
def root_request?(env)
|
32
|
+
%r[\A/(?<query_string>\?.*|\Z)].match(env['PATH_INFO'])
|
33
|
+
end
|
34
|
+
end
|
35
|
+
end
|
@@ -4,18 +4,23 @@ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
|
4
4
|
require 'rack/locale-root-redirect'
|
5
5
|
|
6
6
|
Gem::Specification.new do |gem|
|
7
|
-
gem.name =
|
7
|
+
gem.name = 'rack-locale-root-redirect'
|
8
8
|
gem.version = Rack::LocaleRootRedirect::VERSION
|
9
|
-
gem.authors = [
|
10
|
-
gem.email = [
|
9
|
+
gem.authors = ['Rémi Prévost']
|
10
|
+
gem.email = ['rprevost@mirego.com']
|
11
11
|
gem.description = %q{Rack::LocaleRootRedirect uses Rack:Accept to map '/' to a path based on the `Accept-Language` HTTP header.}
|
12
12
|
gem.summary = gem.description
|
13
|
-
gem.homepage =
|
13
|
+
gem.homepage = 'https://github.com/mirego/rack-locale-root-redirect'
|
14
|
+
gem.license = 'BSD 3-Clause'
|
14
15
|
|
15
16
|
gem.files = `git ls-files`.split($/)
|
16
17
|
gem.executables = gem.files.grep(%r{^bin/}).map{ |f| File.basename(f) }
|
17
18
|
gem.test_files = gem.files.grep(%r{^(test|spec|features)/})
|
18
19
|
gem.require_paths = ["lib"]
|
19
20
|
|
20
|
-
gem.add_runtime_dependency
|
21
|
+
gem.add_runtime_dependency 'rack-accept', '>= 0.4.5'
|
22
|
+
|
23
|
+
gem.add_development_dependency 'rspec', '~> 2.14'
|
24
|
+
gem.add_development_dependency 'coveralls'
|
25
|
+
gem.add_development_dependency 'rake'
|
21
26
|
end
|
@@ -0,0 +1,27 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
describe Rack::LocaleRootRedirect do
|
4
|
+
let(:app) { proc{[200,{},['Hello, world.']]} }
|
5
|
+
let(:stack) { Rack::LocaleRootRedirect.new(Rack::Accept.new(app), locales) }
|
6
|
+
let(:request) { Rack::MockRequest.new(stack) }
|
7
|
+
|
8
|
+
describe 'response' do
|
9
|
+
let(:locales) { { fr: '/fr', en: '/en' } }
|
10
|
+
let(:response) { request.get('/?foo=bar', 'HTTP_ACCEPT_LANGUAGE' => accept_language.join(',')) }
|
11
|
+
|
12
|
+
context 'with first matching language' do
|
13
|
+
let(:accept_language) { %w{en es;q=0.9} }
|
14
|
+
it { expect(response.headers['Location']).to eq '/en?foo=bar' }
|
15
|
+
end
|
16
|
+
|
17
|
+
context 'with second matching language' do
|
18
|
+
let(:accept_language) { %w{es en;q=0.8} }
|
19
|
+
it { expect(response.headers['Location']).to eq '/en?foo=bar' }
|
20
|
+
end
|
21
|
+
|
22
|
+
context 'with default matching language' do
|
23
|
+
let(:accept_language) { %w{es jp;q=0.8} }
|
24
|
+
it { expect(response.headers['Location']).to eq '/fr?foo=bar' }
|
25
|
+
end
|
26
|
+
end
|
27
|
+
end
|
data/spec/spec_helper.rb
ADDED
metadata
CHANGED
@@ -1,70 +1,115 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: rack-locale-root-redirect
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: '0.
|
5
|
-
prerelease:
|
4
|
+
version: '0.2'
|
6
5
|
platform: ruby
|
7
6
|
authors:
|
8
7
|
- Rémi Prévost
|
9
8
|
autorequire:
|
10
9
|
bindir: bin
|
11
10
|
cert_chain: []
|
12
|
-
date: 2013-
|
11
|
+
date: 2013-11-20 00:00:00.000000000 Z
|
13
12
|
dependencies:
|
14
13
|
- !ruby/object:Gem::Dependency
|
15
14
|
name: rack-accept
|
16
15
|
requirement: !ruby/object:Gem::Requirement
|
17
|
-
none: false
|
18
16
|
requirements:
|
19
|
-
- -
|
17
|
+
- - '>='
|
20
18
|
- !ruby/object:Gem::Version
|
21
19
|
version: 0.4.5
|
22
20
|
type: :runtime
|
23
21
|
prerelease: false
|
24
22
|
version_requirements: !ruby/object:Gem::Requirement
|
25
|
-
none: false
|
26
23
|
requirements:
|
27
|
-
- -
|
24
|
+
- - '>='
|
28
25
|
- !ruby/object:Gem::Version
|
29
26
|
version: 0.4.5
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: rspec
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - ~>
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: '2.14'
|
34
|
+
type: :development
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - ~>
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: '2.14'
|
41
|
+
- !ruby/object:Gem::Dependency
|
42
|
+
name: coveralls
|
43
|
+
requirement: !ruby/object:Gem::Requirement
|
44
|
+
requirements:
|
45
|
+
- - '>='
|
46
|
+
- !ruby/object:Gem::Version
|
47
|
+
version: '0'
|
48
|
+
type: :development
|
49
|
+
prerelease: false
|
50
|
+
version_requirements: !ruby/object:Gem::Requirement
|
51
|
+
requirements:
|
52
|
+
- - '>='
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: '0'
|
55
|
+
- !ruby/object:Gem::Dependency
|
56
|
+
name: rake
|
57
|
+
requirement: !ruby/object:Gem::Requirement
|
58
|
+
requirements:
|
59
|
+
- - '>='
|
60
|
+
- !ruby/object:Gem::Version
|
61
|
+
version: '0'
|
62
|
+
type: :development
|
63
|
+
prerelease: false
|
64
|
+
version_requirements: !ruby/object:Gem::Requirement
|
65
|
+
requirements:
|
66
|
+
- - '>='
|
67
|
+
- !ruby/object:Gem::Version
|
68
|
+
version: '0'
|
30
69
|
description: Rack::LocaleRootRedirect uses Rack:Accept to map '/' to a path based
|
31
70
|
on the `Accept-Language` HTTP header.
|
32
71
|
email:
|
33
|
-
-
|
72
|
+
- rprevost@mirego.com
|
34
73
|
executables: []
|
35
74
|
extensions: []
|
36
75
|
extra_rdoc_files: []
|
37
76
|
files:
|
38
77
|
- .gitignore
|
78
|
+
- .rspec
|
39
79
|
- Gemfile
|
40
|
-
- LICENSE.
|
80
|
+
- LICENSE.md
|
41
81
|
- README.md
|
42
82
|
- Rakefile
|
43
83
|
- lib/rack/locale-root-redirect.rb
|
84
|
+
- lib/rack/locale_root_redirect.rb
|
44
85
|
- rack-locale-root-redirect.gemspec
|
45
|
-
|
46
|
-
|
86
|
+
- spec/rack/locale_root_redirect_spec.rb
|
87
|
+
- spec/spec_helper.rb
|
88
|
+
homepage: https://github.com/mirego/rack-locale-root-redirect
|
89
|
+
licenses:
|
90
|
+
- BSD 3-Clause
|
91
|
+
metadata: {}
|
47
92
|
post_install_message:
|
48
93
|
rdoc_options: []
|
49
94
|
require_paths:
|
50
95
|
- lib
|
51
96
|
required_ruby_version: !ruby/object:Gem::Requirement
|
52
|
-
none: false
|
53
97
|
requirements:
|
54
|
-
- -
|
98
|
+
- - '>='
|
55
99
|
- !ruby/object:Gem::Version
|
56
100
|
version: '0'
|
57
101
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
58
|
-
none: false
|
59
102
|
requirements:
|
60
|
-
- -
|
103
|
+
- - '>='
|
61
104
|
- !ruby/object:Gem::Version
|
62
105
|
version: '0'
|
63
106
|
requirements: []
|
64
107
|
rubyforge_project:
|
65
|
-
rubygems_version: 1.
|
108
|
+
rubygems_version: 2.1.0
|
66
109
|
signing_key:
|
67
|
-
specification_version:
|
110
|
+
specification_version: 4
|
68
111
|
summary: Rack::LocaleRootRedirect uses Rack:Accept to map '/' to a path based on the
|
69
112
|
`Accept-Language` HTTP header.
|
70
|
-
test_files:
|
113
|
+
test_files:
|
114
|
+
- spec/rack/locale_root_redirect_spec.rb
|
115
|
+
- spec/spec_helper.rb
|
data/LICENSE.txt
DELETED
@@ -1,22 +0,0 @@
|
|
1
|
-
Copyright (c) 2012 Rémi Prévost
|
2
|
-
|
3
|
-
MIT License
|
4
|
-
|
5
|
-
Permission is hereby granted, free of charge, to any person obtaining
|
6
|
-
a copy of this software and associated documentation files (the
|
7
|
-
"Software"), to deal in the Software without restriction, including
|
8
|
-
without limitation the rights to use, copy, modify, merge, publish,
|
9
|
-
distribute, sublicense, and/or sell copies of the Software, and to
|
10
|
-
permit persons to whom the Software is furnished to do so, subject to
|
11
|
-
the following conditions:
|
12
|
-
|
13
|
-
The above copyright notice and this permission notice shall be
|
14
|
-
included in all copies or substantial portions of the Software.
|
15
|
-
|
16
|
-
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
17
|
-
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
18
|
-
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
19
|
-
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
20
|
-
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
21
|
-
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
22
|
-
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|