accept_language 2.0.2 → 2.0.3
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/README.md +6 -1
- data/lib/accept_language/matcher.rb +12 -6
- data/lib/accept_language/parser.rb +10 -4
- data/lib/accept_language.rb +1 -1
- metadata +3 -3
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: a5c9dad0529da52e9c0fa650e2825c788a673ecf57c7158a9e160797079607c9
|
4
|
+
data.tar.gz: b9e7430c21121d7fb111cf6159f27bb8690e6728dbf8e8ecdd410ce9bbae4a58
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 2dc2b91a4809e88dadef537c951a00bec5adda83a6f80e9717a7ca4651352c606c94dc00a4b5a439668555c6942a92e5de26c985b8c11dc433aed7dde414d710
|
7
|
+
data.tar.gz: 1178cd443cd3c731a90347a9ad2b97cfe9f57084b234c98c1570d2ee8546e59112f0415e992e92bb342301bc3bd2861ac53739eefa55fc3967c64f0e7b086f09
|
data/README.md
CHANGED
@@ -29,7 +29,7 @@ gem "accept_language"
|
|
29
29
|
And then execute:
|
30
30
|
|
31
31
|
```sh
|
32
|
-
bundle
|
32
|
+
bundle install
|
33
33
|
```
|
34
34
|
|
35
35
|
Or install it yourself as:
|
@@ -90,6 +90,11 @@ class ApplicationController < ActionController::Base
|
|
90
90
|
end
|
91
91
|
```
|
92
92
|
|
93
|
+
## Read more
|
94
|
+
|
95
|
+
* [Language negotiation with Ruby](https://dev.to/cyri_/language-negotiation-with-ruby-5166)
|
96
|
+
* [Rubyで、言語交渉](https://qiita.com/cyril/items/45dc233edb7be9d614e7)
|
97
|
+
|
93
98
|
## Versioning
|
94
99
|
|
95
100
|
__AcceptLanguage__ uses [Semantic Versioning 2.0.0](https://semver.org/)
|
@@ -8,17 +8,19 @@ module AcceptLanguage
|
|
8
8
|
# Matcher.new("da" => 1.0, "en-GB" => 0.8, "en" => 0.7).call(:ug, :kk, :ru, :en) # => :en
|
9
9
|
# Matcher.new("da" => 1.0, "en-GB" => 0.8, "en" => 0.7).call(:fr, :en, :"en-GB") # => :"en-GB"
|
10
10
|
class Matcher
|
11
|
+
WILDCARD = "*"
|
12
|
+
|
11
13
|
attr_reader :excluded_langtags, :preferred_langtags
|
12
14
|
|
13
15
|
# @param [Hash<String, BigDecimal>] languages_range A list of accepted
|
14
16
|
# languages with their respective qualities.
|
15
17
|
def initialize(**languages_range)
|
16
|
-
@excluded_langtags = Set[]
|
18
|
+
@excluded_langtags = ::Set[]
|
17
19
|
langtags = []
|
18
20
|
|
19
21
|
languages_range.select do |langtag, quality|
|
20
22
|
if quality.zero?
|
21
|
-
@excluded_langtags << langtag unless
|
23
|
+
@excluded_langtags << langtag unless wildcard?(langtag)
|
22
24
|
else
|
23
25
|
level = (quality * 1_000).to_i
|
24
26
|
langtags[level] = langtag
|
@@ -37,7 +39,7 @@ module AcceptLanguage
|
|
37
39
|
available_langtags = drop_unacceptable(*available_langtags)
|
38
40
|
|
39
41
|
preferred_langtags.each do |preferred_tag|
|
40
|
-
if
|
42
|
+
if wildcard?(preferred_tag)
|
41
43
|
langtag = any_other_langtag(*available_langtags)
|
42
44
|
return langtag unless langtag.nil?
|
43
45
|
else
|
@@ -54,7 +56,7 @@ module AcceptLanguage
|
|
54
56
|
|
55
57
|
def any_other_langtag(*available_langtags)
|
56
58
|
available_langtags.find do |available_langtag|
|
57
|
-
langtags = preferred_langtags - [
|
59
|
+
langtags = preferred_langtags - [WILDCARD]
|
58
60
|
|
59
61
|
langtags.none? do |langtag|
|
60
62
|
available_langtag.match?(/\A#{langtag}/i)
|
@@ -63,10 +65,10 @@ module AcceptLanguage
|
|
63
65
|
end
|
64
66
|
|
65
67
|
def drop_unacceptable(*available_langtags)
|
66
|
-
available_langtags.inject(Set[]) do |langtags, available_langtag|
|
68
|
+
available_langtags.inject(::Set[]) do |langtags, available_langtag|
|
67
69
|
next langtags if unacceptable?(available_langtag)
|
68
70
|
|
69
|
-
langtags + Set[available_langtag]
|
71
|
+
langtags + ::Set[available_langtag]
|
70
72
|
end
|
71
73
|
end
|
72
74
|
|
@@ -75,6 +77,10 @@ module AcceptLanguage
|
|
75
77
|
langtag.match?(/\A#{excluded_langtag}/i)
|
76
78
|
end
|
77
79
|
end
|
80
|
+
|
81
|
+
def wildcard?(value)
|
82
|
+
value.eql?(WILDCARD)
|
83
|
+
end
|
78
84
|
end
|
79
85
|
end
|
80
86
|
|
@@ -1,11 +1,18 @@
|
|
1
1
|
# frozen_string_literal: true
|
2
2
|
|
3
|
+
require "bigdecimal"
|
4
|
+
|
3
5
|
module AcceptLanguage
|
4
6
|
# @note Parser for Accept-Language header fields.
|
5
7
|
# @example
|
6
8
|
# Parser.new("da, en-GB;q=0.8, en;q=0.7") # => #<AcceptLanguage::Parser:0x00007 @languages_range={"da"=>0.1e1, "en-GB"=>0.8e0, "en"=>0.7e0}>
|
7
9
|
# @see https://tools.ietf.org/html/rfc2616#section-14.4
|
8
10
|
class Parser
|
11
|
+
DEFAULT_QUALITY = BigDecimal("1")
|
12
|
+
SEPARATOR = ","
|
13
|
+
SPACE = " "
|
14
|
+
SUFFIX = ";q="
|
15
|
+
|
9
16
|
attr_reader :languages_range
|
10
17
|
|
11
18
|
# @param [String] field The Accept-Language header field to parse.
|
@@ -30,16 +37,15 @@ module AcceptLanguage
|
|
30
37
|
# @return [Hash<String, BigDecimal>] A list of accepted languages with their
|
31
38
|
# respective qualities.
|
32
39
|
def import(field)
|
33
|
-
field.delete(
|
34
|
-
tag, quality = lang.split(
|
40
|
+
field.delete(SPACE).split(SEPARATOR).inject({}) do |hash, lang|
|
41
|
+
tag, quality = lang.split(SUFFIX)
|
35
42
|
next hash if tag.nil?
|
36
43
|
|
37
|
-
quality = quality.nil? ?
|
44
|
+
quality = quality.nil? ? DEFAULT_QUALITY : BigDecimal(quality)
|
38
45
|
hash.merge(tag => quality)
|
39
46
|
end
|
40
47
|
end
|
41
48
|
end
|
42
49
|
end
|
43
50
|
|
44
|
-
require "bigdecimal"
|
45
51
|
require_relative "matcher"
|
data/lib/accept_language.rb
CHANGED
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: 2.0.
|
4
|
+
version: 2.0.3
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Cyril Kato
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2022-
|
11
|
+
date: 2022-07-14 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: bundler
|
@@ -175,7 +175,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
|
|
175
175
|
requirements:
|
176
176
|
- - ">="
|
177
177
|
- !ruby/object:Gem::Version
|
178
|
-
version: 2.7.
|
178
|
+
version: 2.7.6
|
179
179
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
180
180
|
requirements:
|
181
181
|
- - ">="
|