accept_language 0.1.3 → 1.0.0
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 +4 -4
- data/.rubocop_todo.yml +6 -6
- data/Gemfile.lock +3 -3
- data/README.md +9 -10
- data/VERSION.semver +1 -1
- data/accept_language.gemspec +2 -3
- data/lib/accept_language.rb +8 -5
- data/lib/accept_language/intersection.rb +31 -25
- data/lib/accept_language/parser.rb +13 -18
- metadata +5 -4
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 803066f79c629cbd5fc0f3b2090492069bf371f94def82983665b4e0b2be27b2
|
4
|
+
data.tar.gz: aa20bcefe4bc9a687b25ce4275ab721fdabf81f03c6306affc23fb8ed2b8beb3
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: c81186ce6869f201daf1414714a6792248a3ee89e8208d6cad3d37e605330cdbdfa1ce7206ee36ae1354f191c816b8b2b050e6ef52b5af4ccc427f2f9bfb5e43
|
7
|
+
data.tar.gz: 7d3c640960e56adb03937706b325492ce50be3e3d659927ffc348f8fb638b2fb515ed29c5425cb1878967bf0ccad04cb1e161f0476dc555f8a06dcae01792c66
|
data/.rubocop_todo.yml
CHANGED
@@ -1,6 +1,6 @@
|
|
1
1
|
# This configuration was generated by
|
2
2
|
# `rubocop --auto-gen-config`
|
3
|
-
# on 2019-06-
|
3
|
+
# on 2019-06-14 00:44:48 +0200 using RuboCop version 0.71.0.
|
4
4
|
# The point is for the user to remove these configuration records
|
5
5
|
# one by one as the offenses are removed from the code base.
|
6
6
|
# Note that changes in the inspected code, or installation of new
|
@@ -8,22 +8,22 @@
|
|
8
8
|
|
9
9
|
# Offense count: 1
|
10
10
|
Metrics/AbcSize:
|
11
|
-
Max:
|
11
|
+
Max: 18
|
12
12
|
|
13
|
-
# Offense count:
|
13
|
+
# Offense count: 2
|
14
14
|
# Configuration parameters: CountComments, ExcludedMethods.
|
15
15
|
# ExcludedMethods: refine
|
16
16
|
Metrics/BlockLength:
|
17
|
-
Max:
|
17
|
+
Max: 75
|
18
18
|
|
19
19
|
# Offense count: 1
|
20
20
|
# Configuration parameters: CountComments, ExcludedMethods.
|
21
21
|
Metrics/MethodLength:
|
22
22
|
Max: 11
|
23
23
|
|
24
|
-
# Offense count:
|
24
|
+
# Offense count: 8
|
25
25
|
# Cop supports --auto-correct.
|
26
26
|
# Configuration parameters: AutoCorrect, AllowHeredoc, AllowURI, URISchemes, IgnoreCopDirectives, IgnoredPatterns.
|
27
27
|
# URISchemes: http, https
|
28
28
|
Metrics/LineLength:
|
29
|
-
Max:
|
29
|
+
Max: 112
|
data/Gemfile.lock
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
PATH
|
2
2
|
remote: .
|
3
3
|
specs:
|
4
|
-
accept_language (0.
|
4
|
+
accept_language (1.0.0)
|
5
5
|
|
6
6
|
GEM
|
7
7
|
remote: https://rubygems.org/
|
@@ -20,12 +20,12 @@ GEM
|
|
20
20
|
rspec-core (~> 3.8.0)
|
21
21
|
rspec-expectations (~> 3.8.0)
|
22
22
|
rspec-mocks (~> 3.8.0)
|
23
|
-
rspec-core (3.8.
|
23
|
+
rspec-core (3.8.1)
|
24
24
|
rspec-support (~> 3.8.0)
|
25
25
|
rspec-expectations (3.8.4)
|
26
26
|
diff-lcs (>= 1.2.0, < 2.0)
|
27
27
|
rspec-support (~> 3.8.0)
|
28
|
-
rspec-mocks (3.8.
|
28
|
+
rspec-mocks (3.8.1)
|
29
29
|
diff-lcs (>= 1.2.0, < 2.0)
|
30
30
|
rspec-support (~> 3.8.0)
|
31
31
|
rspec-support (3.8.2)
|
data/README.md
CHANGED
@@ -31,8 +31,8 @@ It's intended to be used in a Web server that supports some level of internation
|
|
31
31
|
Examples:
|
32
32
|
|
33
33
|
```ruby
|
34
|
-
AcceptLanguage.parse('da, en-gb;q=0.8, en;q=0.7')
|
35
|
-
AcceptLanguage.parse('fr-CH, fr;q=0.9, en;q=0.8, de;q=0.7, *;q=0.5')
|
34
|
+
AcceptLanguage.parse('da, en-gb;q=0.8, en;q=0.7') # => {:da=>1.0, :"en-gb"=>0.8, :en=>0.7}
|
35
|
+
AcceptLanguage.parse('fr-CH, fr;q=0.9, en;q=0.8, de;q=0.7, *;q=0.5') # => {:"fr-ch"=>1.0, :fr=>0.9, :en=>0.8, :de=>0.7, :*=>0.5}
|
36
36
|
```
|
37
37
|
|
38
38
|
In order to help facilitate better i18n, a method is provided to return the intersection of the languages the user prefers and the languages your application supports.
|
@@ -40,14 +40,13 @@ In order to help facilitate better i18n, a method is provided to return the inte
|
|
40
40
|
Examples:
|
41
41
|
|
42
42
|
```ruby
|
43
|
-
AcceptLanguage.intersection('da, en-gb;q=0.8, en;q=0.7', :
|
44
|
-
AcceptLanguage.intersection('da, en-gb;q=0.8, en;q=0.7', :
|
45
|
-
AcceptLanguage.intersection('fr-CH, fr
|
46
|
-
AcceptLanguage.intersection('fr-CH, fr
|
47
|
-
AcceptLanguage.intersection('
|
48
|
-
AcceptLanguage.intersection('
|
49
|
-
AcceptLanguage.intersection('*;q=0.5,
|
50
|
-
AcceptLanguage.intersection('fr;q=0, zh;q=0.4', :fr) # => nil
|
43
|
+
AcceptLanguage.intersection('da, en-gb;q=0.8, en;q=0.7', :ja, :ro, :da) # => :da
|
44
|
+
AcceptLanguage.intersection('da, en-gb;q=0.8, en;q=0.7', :ja, :ro) # => nil
|
45
|
+
AcceptLanguage.intersection('fr-CH', :fr, two_letter_truncate: false) # => nil
|
46
|
+
AcceptLanguage.intersection('fr-CH', :fr, two_letter_truncate: true) # => :fr
|
47
|
+
AcceptLanguage.intersection('de, zh;q=0.4, fr;q=0', :fr) # => nil
|
48
|
+
AcceptLanguage.intersection('de, zh;q=0.4, *;q=0.5, fr;q=0', :fr) # => nil
|
49
|
+
AcceptLanguage.intersection('de, zh;q=0.4, *;q=0.5, fr;q=0', :ar) # => :ar
|
51
50
|
```
|
52
51
|
|
53
52
|
### Rails integration example
|
data/VERSION.semver
CHANGED
@@ -1 +1 @@
|
|
1
|
-
0.
|
1
|
+
1.0.0
|
data/accept_language.gemspec
CHANGED
@@ -7,9 +7,8 @@ Gem::Specification.new do |spec|
|
|
7
7
|
spec.email = ['contact@cyril.email']
|
8
8
|
|
9
9
|
spec.summary = 'Parser for Accept-Language request HTTP header'
|
10
|
-
spec.description = 'Parses the Accept-Language header from an HTTP '
|
11
|
-
'request and produces a
|
12
|
-
'quality.'
|
10
|
+
spec.description = 'Parses the Accept-Language header from an HTTP ' \
|
11
|
+
'request and produces a hash of languages and qualities.'
|
13
12
|
spec.homepage = 'https://github.com/cyril/accept_language.rb'
|
14
13
|
spec.license = 'MIT'
|
15
14
|
|
data/lib/accept_language.rb
CHANGED
@@ -2,14 +2,17 @@
|
|
2
2
|
|
3
3
|
# Tiny library for parsing the Accept-Language header.
|
4
4
|
module AcceptLanguage
|
5
|
-
|
6
|
-
|
5
|
+
# @example
|
6
|
+
# AcceptLanguage.intersection('ja, en-gb;q=0.8, en;q=0.7', :ar, :ja) # => :ja
|
7
|
+
def self.intersection(raw_input, *supported_langs, two_letter_truncate: true)
|
8
|
+
Intersection.new(raw_input, *supported_langs, two_letter_truncate: two_letter_truncate).call
|
7
9
|
end
|
8
10
|
|
9
|
-
|
10
|
-
|
11
|
+
# @example
|
12
|
+
# AcceptLanguage.parse('ja, en-gb;q=0.8, en;q=0.7') # => { ja: 1.0, "en-gb": 0.8, en: 0.7 }
|
13
|
+
def self.parse(raw_input, two_letter_truncate: false)
|
14
|
+
Parser.call(raw_input, two_letter_truncate: two_letter_truncate)
|
11
15
|
end
|
12
16
|
end
|
13
17
|
|
14
18
|
require_relative 'accept_language/intersection'
|
15
|
-
require_relative 'accept_language/parser'
|
@@ -2,51 +2,57 @@
|
|
2
2
|
|
3
3
|
module AcceptLanguage
|
4
4
|
# @example
|
5
|
-
# Intersection.new('
|
5
|
+
# AcceptLanguage::Intersection.new('ja, en-gb;q=0.8, en;q=0.7', :ar, :ja).call # => :ja
|
6
6
|
# @note Compare an Accept-Language header value with your application's
|
7
7
|
# supported languages to find the common languages that could be presented
|
8
8
|
# to a user.
|
9
9
|
# @see https://tools.ietf.org/html/rfc2616#section-14.4
|
10
10
|
class Intersection
|
11
|
-
attr_reader :
|
11
|
+
attr_reader :preferences, :supported_langs
|
12
12
|
|
13
|
-
def initialize(
|
14
|
-
@
|
15
|
-
@default_supported_language = default_supported_language.to_sym.downcase
|
16
|
-
@other_supported_languages = other_supported_languages.map { |lang| lang.to_sym.downcase }.to_set
|
13
|
+
def initialize(raw_input, *supported_langs, two_letter_truncate: true)
|
14
|
+
@preferences = Parser.call(raw_input, two_letter_truncate: two_letter_truncate)
|
17
15
|
|
18
|
-
|
19
|
-
|
20
|
-
|
21
|
-
|
22
|
-
end
|
23
|
-
|
24
|
-
@default_supported_language = @default_supported_language[0, 2].to_sym
|
25
|
-
|
26
|
-
@other_supported_languages = @other_supported_languages.map do |other_supported_language|
|
27
|
-
other_supported_language[0, 2].to_sym
|
28
|
-
end.to_set
|
16
|
+
@supported_langs = supported_langs.map do |lang|
|
17
|
+
lang = lang.downcase
|
18
|
+
lang = lang[0, 2] if two_letter_truncate
|
19
|
+
lang.to_sym
|
20
|
+
end.uniq
|
29
21
|
end
|
30
22
|
|
31
23
|
def call
|
32
|
-
|
33
|
-
|
24
|
+
qualities_without_zero_in_desc_order.each do |quality|
|
25
|
+
tag = preferences.key(quality)
|
26
|
+
|
27
|
+
if wildcard?(tag)
|
28
|
+
lang = any_tag_not_matched_by_any_other_range
|
29
|
+
return lang unless lang.nil?
|
30
|
+
end
|
34
31
|
|
35
|
-
|
32
|
+
return tag if supported_langs.include?(tag)
|
36
33
|
end
|
34
|
+
|
35
|
+
nil
|
37
36
|
end
|
38
37
|
|
39
38
|
protected
|
40
39
|
|
41
|
-
def
|
42
|
-
|
40
|
+
def any_tag_not_matched_by_any_other_range
|
41
|
+
every_tag_not_matched_by_any_other_range.first
|
42
|
+
end
|
43
|
+
|
44
|
+
def every_tag_not_matched_by_any_other_range
|
45
|
+
supported_langs - preferences.keys
|
46
|
+
end
|
47
|
+
|
48
|
+
def qualities_without_zero_in_desc_order
|
49
|
+
preferences.values.reject(&:zero?).uniq.sort.reverse
|
43
50
|
end
|
44
51
|
|
45
|
-
def wildcard
|
46
|
-
:*
|
52
|
+
def wildcard?(value)
|
53
|
+
value.equal?(:*)
|
47
54
|
end
|
48
55
|
end
|
49
56
|
end
|
50
57
|
|
51
58
|
require_relative 'parser'
|
52
|
-
require 'set'
|
@@ -2,29 +2,24 @@
|
|
2
2
|
|
3
3
|
module AcceptLanguage
|
4
4
|
# @example
|
5
|
-
# Parser.
|
6
|
-
# @note Parse
|
7
|
-
# language tags.
|
5
|
+
# AcceptLanguage::Parser.call('da, en-gb;q=0.8, en;q=0.7') # => { da: 1.0, "en-gb": 0.8, en: 0.7 }
|
6
|
+
# @note Parse an Accept-Language header value into a hash of tag and quality.
|
8
7
|
# @see https://tools.ietf.org/html/rfc2616#section-14.4
|
9
|
-
|
10
|
-
def
|
11
|
-
|
12
|
-
|
8
|
+
module Parser
|
9
|
+
def self.call(raw_input, two_letter_truncate: false)
|
10
|
+
raw_input.to_s.delete(' ').split(',').inject({}) do |hash, lang|
|
11
|
+
tag, quality = lang.split(/;q=/i)
|
12
|
+
next hash if tag.nil?
|
13
13
|
|
14
|
-
|
15
|
-
preferences.sort { |lang_a, lang_b| lang_b.fetch(1) <=> lang_a.fetch(1) }
|
16
|
-
.map(&:first)
|
17
|
-
end
|
14
|
+
tag = tag.downcase.to_sym
|
18
15
|
|
19
|
-
|
16
|
+
if two_letter_truncate && tag.length > 2
|
17
|
+
tag = tag[0, 2].to_sym
|
18
|
+
next hash if hash.key?(tag)
|
19
|
+
end
|
20
20
|
|
21
|
-
def preferences
|
22
|
-
@string.delete(' ').split(',').inject({}) do |result, language|
|
23
|
-
tag, quality = language.split(/;q=/i)
|
24
21
|
quality = quality.nil? ? 1.0 : quality.to_f
|
25
|
-
|
26
|
-
|
27
|
-
result.merge(tag.downcase.to_sym => quality)
|
22
|
+
hash.merge(tag => quality)
|
28
23
|
end
|
29
24
|
end
|
30
25
|
end
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: accept_language
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 1.0.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Cyril Kato
|
8
8
|
autorequire:
|
9
9
|
bindir: exe
|
10
10
|
cert_chain: []
|
11
|
-
date: 2019-06-
|
11
|
+
date: 2019-06-13 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: bundler
|
@@ -95,7 +95,7 @@ dependencies:
|
|
95
95
|
- !ruby/object:Gem::Version
|
96
96
|
version: '0.9'
|
97
97
|
description: Parses the Accept-Language header from an HTTP request and produces a
|
98
|
-
|
98
|
+
hash of languages and qualities.
|
99
99
|
email:
|
100
100
|
- contact@cyril.email
|
101
101
|
executables: []
|
@@ -139,7 +139,8 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
139
139
|
- !ruby/object:Gem::Version
|
140
140
|
version: '0'
|
141
141
|
requirements: []
|
142
|
-
|
142
|
+
rubyforge_project:
|
143
|
+
rubygems_version: 2.7.6
|
143
144
|
signing_key:
|
144
145
|
specification_version: 4
|
145
146
|
summary: Parser for Accept-Language request HTTP header
|