accept_language 0.1.3 → 1.0.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: af6ad773549b67a530133a79fd84c7b6de133ab571cb14642ce261eb0f9255d4
4
- data.tar.gz: c5a11c41e67023583c9b3ece32babeee369760d6d8b5f0fb2890f4dc055da5dc
3
+ metadata.gz: 803066f79c629cbd5fc0f3b2090492069bf371f94def82983665b4e0b2be27b2
4
+ data.tar.gz: aa20bcefe4bc9a687b25ce4275ab721fdabf81f03c6306affc23fb8ed2b8beb3
5
5
  SHA512:
6
- metadata.gz: f35d229b0ab907ebcbeba66f6163c7f66ef7567cca0795bb669487713ed0eb9bcf1caff663ae953ef070eca4f0a2f932ffb72a5ed4349f876ed871df3649dcae
7
- data.tar.gz: a05701325ee77c8aab3ce9057bd26bff5e75564e937020a7694306e81257a72ffbae7530dae570027dcff72afa8f8c2af31acc29ab50005638ecaf9f04f22af6
6
+ metadata.gz: c81186ce6869f201daf1414714a6792248a3ee89e8208d6cad3d37e605330cdbdfa1ce7206ee36ae1354f191c816b8b2b050e6ef52b5af4ccc427f2f9bfb5e43
7
+ data.tar.gz: 7d3c640960e56adb03937706b325492ce50be3e3d659927ffc348f8fb638b2fb515ed29c5425cb1878967bf0ccad04cb1e161f0476dc555f8a06dcae01792c66
@@ -1,6 +1,6 @@
1
1
  # This configuration was generated by
2
2
  # `rubocop --auto-gen-config`
3
- # on 2019-06-12 23:02:50 +0200 using RuboCop version 0.71.0.
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: 19
11
+ Max: 18
12
12
 
13
- # Offense count: 1
13
+ # Offense count: 2
14
14
  # Configuration parameters: CountComments, ExcludedMethods.
15
15
  # ExcludedMethods: refine
16
16
  Metrics/BlockLength:
17
- Max: 80
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: 15
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: 110
29
+ Max: 112
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- accept_language (0.1.3)
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.0)
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.0)
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') # => [:da, :"en-gb", :en]
35
- AcceptLanguage.parse('fr-CH, fr;q=0.9, en;q=0.8, de;q=0.7, *;q=0.5') # => [:"fr-ch", :fr, :en, :de, :*]
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', :ar, :da, :ja, :ro) # => :da
44
- AcceptLanguage.intersection('da, en-gb;q=0.8, en;q=0.7', :ar, :ja, :ro) # => nil
45
- AcceptLanguage.intersection('fr-CH, fr;q=0.9, en;q=0.8, de;q=0.7, *;q=0.5', :en, :ja) # => :en
46
- AcceptLanguage.intersection('fr-CH, fr;q=0.9, en;q=0.8, de;q=0.7, *;q=0.5', :ja) # => :ja
47
- AcceptLanguage.intersection('ko, fr-CH;q=0.9, en;q=0.8, de;q=0.7, *;q=0.5', :fr, :de) # => :fr
48
- AcceptLanguage.intersection('ko, fr-CH;q=0.9, en;q=0.8, de;q=0.7, *;q=0.5', :fr, :de, truncate: false) # => :de
49
- AcceptLanguage.intersection('*;q=0.5, zh;q=0.4', :ja, :zh) # => :ja
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
@@ -1 +1 @@
1
- 0.1.3
1
+ 1.0.0
@@ -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 list of languages sorted by ' \
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
 
@@ -2,14 +2,17 @@
2
2
 
3
3
  # Tiny library for parsing the Accept-Language header.
4
4
  module AcceptLanguage
5
- def self.intersection(raw_input, *supported_languages, truncate: true)
6
- Intersection.new(raw_input, *supported_languages, truncate: truncate).call
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
- def self.parse(raw_input)
10
- Parser.new(raw_input).call
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('da, en-gb;q=0.8, en;q=0.7', :ar, :ja, :ro).call
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 :accepted_languages, :default_supported_language, :other_supported_languages
11
+ attr_reader :preferences, :supported_langs
12
12
 
13
- def initialize(accepted_languages, default_supported_language, *other_supported_languages, truncate: true)
14
- @accepted_languages = Parser.new(accepted_languages).call
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
- return unless truncate
19
-
20
- @accepted_languages = @accepted_languages.map do |accepted_language|
21
- accepted_language[0, 2].to_sym
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
- accepted_languages.find do |accepted_language|
33
- break default_supported_language if accepted_language.equal?(wildcard)
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
- supported_languages.include?(accepted_language)
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 supported_languages
42
- other_supported_languages + Set[default_supported_language]
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.new('da, en-gb;q=0.8, en;q=0.7').call
6
- # @note Parse a raw Accept-Language header value into an ordered list of
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
- class Parser
10
- def initialize(raw_input)
11
- @string = raw_input.to_s
12
- end
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
- def call
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
- protected
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
- next result if quality.zero?
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.1.3
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-12 00:00:00.000000000 Z
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
- list of languages sorted by quality.
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
- rubygems_version: 3.0.3
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