accept_language 1.0.0 → 2.0.2

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
  SHA256:
3
- metadata.gz: 803066f79c629cbd5fc0f3b2090492069bf371f94def82983665b4e0b2be27b2
4
- data.tar.gz: aa20bcefe4bc9a687b25ce4275ab721fdabf81f03c6306affc23fb8ed2b8beb3
3
+ metadata.gz: 25f4296b38885118c1d127873d887a138bbfb6615ec0dc3d2db826e14e96e7d7
4
+ data.tar.gz: 9f2fac2c3818c94c5b7c7bc6d95655e3fefcb54c5ec5a3ba4ef28d05213fa00f
5
5
  SHA512:
6
- metadata.gz: c81186ce6869f201daf1414714a6792248a3ee89e8208d6cad3d37e605330cdbdfa1ce7206ee36ae1354f191c816b8b2b050e6ef52b5af4ccc427f2f9bfb5e43
7
- data.tar.gz: 7d3c640960e56adb03937706b325492ce50be3e3d659927ffc348f8fb638b2fb515ed29c5425cb1878967bf0ccad04cb1e161f0476dc555f8a06dcae01792c66
6
+ metadata.gz: b138c701a374afec0e26b9560570b3f48490a1da1e79f42daa768aa03f038f969f4670e4af57a97c109192410df0b8c66bc39bcc33f7362cc7440be6d243b0e6
7
+ data.tar.gz: 4a149d54102620ef411b1c1966f159840e3dec65cf5a2e33e5e045154e1234ee759be0b6ddda4cc8577f778a62e753f96642e49c5fc99e22760e6335a9bfb7aa
@@ -1,6 +1,6 @@
1
1
  The MIT License (MIT)
2
2
 
3
- Copyright (c) 2019 Cyril Kato
3
+ Copyright (c) 2019-2022 Cyril Kato
4
4
 
5
5
  Permission is hereby granted, free of charge, to any person obtaining a copy
6
6
  of this software and associated documentation files (the "Software"), to deal
data/README.md CHANGED
@@ -1,52 +1,65 @@
1
- # Accept Language
1
+ # Accept Language 🌐
2
2
 
3
3
  A tiny library for parsing the `Accept-Language` header from browsers (as defined in [RFC 2616](https://tools.ietf.org/html/rfc2616#section-14.4)).
4
4
 
5
5
  ## Status
6
6
 
7
- [![Gem Version](https://badge.fury.io/rb/accept_language.svg)](https://badge.fury.io/rb/accept_language)
8
- [![TravisCI](https://travis-ci.org/cyril/accept_language.rb.svg?branch=master)](https://travis-ci.org/cyril/accept_language.rb)
9
- [![Inline Docs](https://inch-ci.org/github/cyril/accept_language.rb.svg)](https://inch-ci.org/github/cyril/accept_language.rb)
7
+ [![Version](https://img.shields.io/github/v/tag/cyril/accept_language.rb?label=Version&logo=github)](https://github.com/cyril/accept_language.rb/releases)
8
+ [![Yard documentation](https://img.shields.io/badge/Yard-documentation-blue.svg?logo=github)](https://rubydoc.info/github/cyril/accept_language.rb/main)
9
+ [![CI](https://github.com/cyril/accept_language.rb/workflows/CI/badge.svg?branch=main)](https://github.com/cyril/accept_language.rb/actions?query=workflow%3Aci+branch%3Amain)
10
+ [![RuboCop](https://github.com/cyril/accept_language.rb/workflows/RuboCop/badge.svg?branch=main)](https://github.com/cyril/accept_language.rb/actions?query=workflow%3Arubocop+branch%3Amain)
11
+ [![License](https://img.shields.io/github/license/cyril/accept_language.rb?label=License&logo=github)](https://github.com/cyril/accept_language.rb/raw/main/LICENSE.md)
12
+
13
+ ## Why this tool?
14
+
15
+ - Thread-safe implementation.
16
+ - Small algorithm that can handle tricky cases.
17
+ - Match strings and symbols ignoring the case.
18
+ - Works also well without Rails, Rack, i18n.
19
+ - Comes with [BCP 47](https://www.rfc-editor.org/bcp/bcp47.txt) support.
10
20
 
11
21
  ## Installation
12
22
 
13
23
  Add this line to your application's Gemfile:
14
24
 
15
25
  ```ruby
16
- gem 'accept_language'
26
+ gem "accept_language"
17
27
  ```
18
28
 
19
29
  And then execute:
20
30
 
21
- $ bundle
31
+ ```sh
32
+ bundle
33
+ ```
22
34
 
23
35
  Or install it yourself as:
24
36
 
25
- $ gem install accept_language
37
+ ```sh
38
+ gem install accept_language
39
+ ```
26
40
 
27
41
  ## Usage
28
42
 
29
43
  It's intended to be used in a Web server that supports some level of internationalization (i18n), but can be used anytime an `Accept-Language` header string is available.
30
44
 
31
- Examples:
32
-
33
- ```ruby
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
- ```
37
-
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.
45
+ In order to help facilitate better i18n, the lib try to find the intersection of the languages the user prefers and the languages your application supports.
39
46
 
40
- Examples:
47
+ Some examples:
41
48
 
42
49
  ```ruby
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
50
+ AcceptLanguage.parse("da, en-GB;q=0.8, en;q=0.7").match(:en, :da) # => :da
51
+ AcceptLanguage.parse("da, en;q=0.8, ug;q=0.9").match("en-GB", "ug-CN") # => "ug-CN"
52
+ AcceptLanguage.parse("da, en-GB;q=0.8, en;q=0.7").match(:ja) # => nil
53
+ AcceptLanguage.parse("fr-CH").match(:fr) # => nil
54
+ AcceptLanguage.parse("de, zh;q=0.4, fr;q=0").match(:fr) # => nil
55
+ AcceptLanguage.parse("de, zh;q=0.4, *;q=0.5, fr;q=0").match(:ar) # => :ar
56
+ AcceptLanguage.parse("uz-latn-uz").match("uz-Latn-UZ") # => "uz-Latn-UZ"
57
+ AcceptLanguage.parse("foo;q=0.1").match(:FoO) # => :FoO
58
+ AcceptLanguage.parse("foo").match("bar") # => nil
59
+ AcceptLanguage.parse("*").match("BaZ") # => "BaZ"
60
+ AcceptLanguage.parse("*;q=0").match("foobar") # => nil
61
+ AcceptLanguage.parse("en, en;q=0").match("en") # => nil
62
+ AcceptLanguage.parse("*, en;q=0").match("en") # => nil
50
63
  ```
51
64
 
52
65
  ### Rails integration example
@@ -61,10 +74,10 @@ class ApplicationController < ActionController::Base
61
74
  end
62
75
 
63
76
  def best_locale_from_request
64
- return I18n.default_locale unless request.headers.key?('HTTP_ACCEPT_LANGUAGE')
77
+ return I18n.default_locale unless request.headers.key?("HTTP_ACCEPT_LANGUAGE")
65
78
 
66
- string = request.headers.fetch('HTTP_ACCEPT_LANGUAGE')
67
- locale = AcceptLanguage.intersection(string, I18n.default_locale, *I18n.available_locales)
79
+ string = request.headers.fetch("HTTP_ACCEPT_LANGUAGE")
80
+ locale = AcceptLanguage.parse(string).match(*I18n.available_locales)
68
81
 
69
82
  # If the server cannot serve any matching language,
70
83
  # it can theoretically send back a 406 (Not Acceptable) error code.
@@ -77,18 +90,10 @@ class ApplicationController < ActionController::Base
77
90
  end
78
91
  ```
79
92
 
80
- ## Contributing
81
-
82
- Bug reports and pull requests are welcome on GitHub at https://github.com/cyril/accept_language.rb. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the [Contributor Covenant](http://contributor-covenant.org) code of conduct.
83
-
84
93
  ## Versioning
85
94
 
86
95
  __AcceptLanguage__ uses [Semantic Versioning 2.0.0](https://semver.org/)
87
96
 
88
97
  ## License
89
98
 
90
- The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
91
-
92
- ## Code of Conduct
93
-
94
- Everyone interacting in the AcceptLanguage project's codebases, issue trackers, chat rooms and mailing lists is expected to follow the [code of conduct](https://github.com/cyril/accept_language.rb/blob/master/CODE_OF_CONDUCT.md).
99
+ The [gem](https://rubygems.org/gems/accept_language) is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
@@ -0,0 +1,81 @@
1
+ # frozen_string_literal: true
2
+
3
+ module AcceptLanguage
4
+ # @note Compare an Accept-Language header value with your application's
5
+ # supported languages to find the common languages that could be presented
6
+ # to a user.
7
+ # @example
8
+ # Matcher.new("da" => 1.0, "en-GB" => 0.8, "en" => 0.7).call(:ug, :kk, :ru, :en) # => :en
9
+ # Matcher.new("da" => 1.0, "en-GB" => 0.8, "en" => 0.7).call(:fr, :en, :"en-GB") # => :"en-GB"
10
+ class Matcher
11
+ attr_reader :excluded_langtags, :preferred_langtags
12
+
13
+ # @param [Hash<String, BigDecimal>] languages_range A list of accepted
14
+ # languages with their respective qualities.
15
+ def initialize(**languages_range)
16
+ @excluded_langtags = Set[]
17
+ langtags = []
18
+
19
+ languages_range.select do |langtag, quality|
20
+ if quality.zero?
21
+ @excluded_langtags << langtag unless langtag.eql?("*")
22
+ else
23
+ level = (quality * 1_000).to_i
24
+ langtags[level] = langtag
25
+ end
26
+ end
27
+
28
+ @preferred_langtags = langtags.compact.reverse
29
+ end
30
+
31
+ # @param [Array<String, Symbol>] available_langtags The list of available
32
+ # languages.
33
+ # @example Uyghur, Kazakh, Russian and English languages are available.
34
+ # call(:ug, :kk, :ru, :en)
35
+ # @return [String, Symbol, nil] The language that best matches.
36
+ def call(*available_langtags)
37
+ available_langtags = drop_unacceptable(*available_langtags)
38
+
39
+ preferred_langtags.each do |preferred_tag|
40
+ if preferred_tag.eql?("*")
41
+ langtag = any_other_langtag(*available_langtags)
42
+ return langtag unless langtag.nil?
43
+ else
44
+ available_langtags.each do |available_langtag|
45
+ return available_langtag if available_langtag.match?(/\A#{preferred_tag}/i)
46
+ end
47
+ end
48
+ end
49
+
50
+ nil
51
+ end
52
+
53
+ private
54
+
55
+ def any_other_langtag(*available_langtags)
56
+ available_langtags.find do |available_langtag|
57
+ langtags = preferred_langtags - ["*"]
58
+
59
+ langtags.none? do |langtag|
60
+ available_langtag.match?(/\A#{langtag}/i)
61
+ end
62
+ end
63
+ end
64
+
65
+ def drop_unacceptable(*available_langtags)
66
+ available_langtags.inject(Set[]) do |langtags, available_langtag|
67
+ next langtags if unacceptable?(available_langtag)
68
+
69
+ langtags + Set[available_langtag]
70
+ end
71
+ end
72
+
73
+ def unacceptable?(langtag)
74
+ excluded_langtags.any? do |excluded_langtag|
75
+ langtag.match?(/\A#{excluded_langtag}/i)
76
+ end
77
+ end
78
+ end
79
+ end
80
+
81
+ require "set"
@@ -1,26 +1,45 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module AcceptLanguage
4
+ # @note Parser for Accept-Language header fields.
4
5
  # @example
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.
6
+ # 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
7
  # @see https://tools.ietf.org/html/rfc2616#section-14.4
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?
8
+ class Parser
9
+ attr_reader :languages_range
13
10
 
14
- tag = tag.downcase.to_sym
11
+ # @param [String] field The Accept-Language header field to parse.
12
+ # @see https://tools.ietf.org/html/rfc2616#section-14.4
13
+ def initialize(field)
14
+ @languages_range = import(field)
15
+ end
16
+
17
+ # @param [Array<String, Symbol>] available_langtags The list of available
18
+ # languages.
19
+ # @example Uyghur, Kazakh, Russian and English languages are available.
20
+ # match(:ug, :kk, :ru, :en)
21
+ # @return [String, Symbol, nil] The language that best matches.
22
+ def match(*available_langtags)
23
+ Matcher.new(**languages_range).call(*available_langtags)
24
+ end
15
25
 
16
- if two_letter_truncate && tag.length > 2
17
- tag = tag[0, 2].to_sym
18
- next hash if hash.key?(tag)
19
- end
26
+ private
20
27
 
21
- quality = quality.nil? ? 1.0 : quality.to_f
28
+ # @example
29
+ # import('da, en-GB;q=0.8, en;q=0.7') # => {"da"=>0.1e1, "en-GB"=>0.8e0, "en"=>0.7e0}
30
+ # @return [Hash<String, BigDecimal>] A list of accepted languages with their
31
+ # respective qualities.
32
+ def import(field)
33
+ field.delete(" ").split(",").inject({}) do |hash, lang|
34
+ tag, quality = lang.split(/;q=/i)
35
+ next hash if tag.nil?
36
+
37
+ quality = quality.nil? ? BigDecimal("1") : BigDecimal(quality)
22
38
  hash.merge(tag => quality)
23
39
  end
24
40
  end
25
41
  end
26
42
  end
43
+
44
+ require "bigdecimal"
45
+ require_relative "matcher"
@@ -1,18 +1,17 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  # Tiny library for parsing the Accept-Language header.
4
+ # @example
5
+ # AcceptLanguage.parse("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}>
6
+ # @see https://tools.ietf.org/html/rfc2616#section-14.4
4
7
  module AcceptLanguage
8
+ # @note Parse an Accept-Language header field into a language range.
5
9
  # @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
9
- end
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)
10
+ # parse("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}>
11
+ # @return [#match] a parser that responds to #match.
12
+ def self.parse(field)
13
+ Parser.new(field)
15
14
  end
16
15
  end
17
16
 
18
- require_relative 'accept_language/intersection'
17
+ require_relative "accept_language/parser"
metadata CHANGED
@@ -1,129 +1,172 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: accept_language
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.0.0
4
+ version: 2.0.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - Cyril Kato
8
8
  autorequire:
9
- bindir: exe
9
+ bindir: bin
10
10
  cert_chain: []
11
- date: 2019-06-13 00:00:00.000000000 Z
11
+ date: 2022-01-08 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler
15
15
  requirement: !ruby/object:Gem::Requirement
16
16
  requirements:
17
- - - "~>"
17
+ - - ">="
18
18
  - !ruby/object:Gem::Version
19
- version: '2.0'
19
+ version: '0'
20
20
  type: :development
21
21
  prerelease: false
22
22
  version_requirements: !ruby/object:Gem::Requirement
23
23
  requirements:
24
- - - "~>"
24
+ - - ">="
25
25
  - !ruby/object:Gem::Version
26
- version: '2.0'
26
+ version: '0'
27
27
  - !ruby/object:Gem::Dependency
28
28
  name: rake
29
29
  requirement: !ruby/object:Gem::Requirement
30
30
  requirements:
31
- - - "~>"
31
+ - - ">="
32
32
  - !ruby/object:Gem::Version
33
- version: '10.0'
33
+ version: '0'
34
34
  type: :development
35
35
  prerelease: false
36
36
  version_requirements: !ruby/object:Gem::Requirement
37
37
  requirements:
38
- - - "~>"
38
+ - - ">="
39
39
  - !ruby/object:Gem::Version
40
- version: '10.0'
40
+ version: '0'
41
41
  - !ruby/object:Gem::Dependency
42
- name: rspec
42
+ name: r_spec-clone
43
43
  requirement: !ruby/object:Gem::Requirement
44
44
  requirements:
45
- - - "~>"
45
+ - - ">="
46
46
  - !ruby/object:Gem::Version
47
- version: '3.0'
47
+ version: '0'
48
48
  type: :development
49
49
  prerelease: false
50
50
  version_requirements: !ruby/object:Gem::Requirement
51
51
  requirements:
52
- - - "~>"
52
+ - - ">="
53
53
  - !ruby/object:Gem::Version
54
- version: '3.0'
54
+ version: '0'
55
55
  - !ruby/object:Gem::Dependency
56
- name: rubocop
56
+ name: rubocop-md
57
57
  requirement: !ruby/object:Gem::Requirement
58
58
  requirements:
59
- - - "~>"
59
+ - - ">="
60
60
  - !ruby/object:Gem::Version
61
- version: '0.71'
61
+ version: '0'
62
62
  type: :development
63
63
  prerelease: false
64
64
  version_requirements: !ruby/object:Gem::Requirement
65
65
  requirements:
66
- - - "~>"
66
+ - - ">="
67
67
  - !ruby/object:Gem::Version
68
- version: '0.71'
68
+ version: '0'
69
+ - !ruby/object:Gem::Dependency
70
+ name: rubocop-performance
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'
83
+ - !ruby/object:Gem::Dependency
84
+ name: rubocop-rake
85
+ requirement: !ruby/object:Gem::Requirement
86
+ requirements:
87
+ - - ">="
88
+ - !ruby/object:Gem::Version
89
+ version: '0'
90
+ type: :development
91
+ prerelease: false
92
+ version_requirements: !ruby/object:Gem::Requirement
93
+ requirements:
94
+ - - ">="
95
+ - !ruby/object:Gem::Version
96
+ version: '0'
97
+ - !ruby/object:Gem::Dependency
98
+ name: rubocop-rspec
99
+ requirement: !ruby/object:Gem::Requirement
100
+ requirements:
101
+ - - ">="
102
+ - !ruby/object:Gem::Version
103
+ version: '0'
104
+ type: :development
105
+ prerelease: false
106
+ version_requirements: !ruby/object:Gem::Requirement
107
+ requirements:
108
+ - - ">="
109
+ - !ruby/object:Gem::Version
110
+ version: '0'
111
+ - !ruby/object:Gem::Dependency
112
+ name: rubocop-thread_safety
113
+ requirement: !ruby/object:Gem::Requirement
114
+ requirements:
115
+ - - ">="
116
+ - !ruby/object:Gem::Version
117
+ version: '0'
118
+ type: :development
119
+ prerelease: false
120
+ version_requirements: !ruby/object:Gem::Requirement
121
+ requirements:
122
+ - - ">="
123
+ - !ruby/object:Gem::Version
124
+ version: '0'
69
125
  - !ruby/object:Gem::Dependency
70
126
  name: simplecov
71
127
  requirement: !ruby/object:Gem::Requirement
72
128
  requirements:
73
- - - "~>"
129
+ - - ">="
74
130
  - !ruby/object:Gem::Version
75
- version: '0.16'
131
+ version: '0'
76
132
  type: :development
77
133
  prerelease: false
78
134
  version_requirements: !ruby/object:Gem::Requirement
79
135
  requirements:
80
- - - "~>"
136
+ - - ">="
81
137
  - !ruby/object:Gem::Version
82
- version: '0.16'
138
+ version: '0'
83
139
  - !ruby/object:Gem::Dependency
84
140
  name: yard
85
141
  requirement: !ruby/object:Gem::Requirement
86
142
  requirements:
87
- - - "~>"
143
+ - - ">="
88
144
  - !ruby/object:Gem::Version
89
- version: '0.9'
145
+ version: '0'
90
146
  type: :development
91
147
  prerelease: false
92
148
  version_requirements: !ruby/object:Gem::Requirement
93
149
  requirements:
94
- - - "~>"
150
+ - - ">="
95
151
  - !ruby/object:Gem::Version
96
- version: '0.9'
152
+ version: '0'
97
153
  description: Parses the Accept-Language header from an HTTP request and produces a
98
154
  hash of languages and qualities.
99
- email:
100
- - contact@cyril.email
155
+ email: contact@cyril.email
101
156
  executables: []
102
157
  extensions: []
103
158
  extra_rdoc_files: []
104
159
  files:
105
- - ".gitignore"
106
- - ".rspec"
107
- - ".rubocop.yml"
108
- - ".rubocop_todo.yml"
109
- - ".travis.yml"
110
- - CODE_OF_CONDUCT.md
111
- - Gemfile
112
- - Gemfile.lock
113
- - LICENSE.txt
160
+ - LICENSE.md
114
161
  - README.md
115
- - Rakefile
116
- - VERSION.semver
117
- - accept_language.gemspec
118
- - bin/console
119
- - bin/setup
120
162
  - lib/accept_language.rb
121
- - lib/accept_language/intersection.rb
163
+ - lib/accept_language/matcher.rb
122
164
  - lib/accept_language/parser.rb
123
165
  homepage: https://github.com/cyril/accept_language.rb
124
166
  licenses:
125
167
  - MIT
126
- metadata: {}
168
+ metadata:
169
+ rubygems_mfa_required: 'true'
127
170
  post_install_message:
128
171
  rdoc_options: []
129
172
  require_paths:
@@ -132,16 +175,15 @@ required_ruby_version: !ruby/object:Gem::Requirement
132
175
  requirements:
133
176
  - - ">="
134
177
  - !ruby/object:Gem::Version
135
- version: '0'
178
+ version: 2.7.5
136
179
  required_rubygems_version: !ruby/object:Gem::Requirement
137
180
  requirements:
138
181
  - - ">="
139
182
  - !ruby/object:Gem::Version
140
183
  version: '0'
141
184
  requirements: []
142
- rubyforge_project:
143
- rubygems_version: 2.7.6
185
+ rubygems_version: 3.1.6
144
186
  signing_key:
145
187
  specification_version: 4
146
- summary: Parser for Accept-Language request HTTP header
188
+ summary: "Parser for Accept-Language request HTTP header \U0001F310"
147
189
  test_files: []
data/.gitignore DELETED
@@ -1,8 +0,0 @@
1
- /.bundle/
2
- /.yardoc
3
- /_yardoc/
4
- /coverage/
5
- /doc/
6
- /pkg/
7
- /spec/reports/
8
- /tmp/
data/.rspec DELETED
@@ -1,3 +0,0 @@
1
- --format documentation
2
- --color
3
- --require spec_helper
data/.rubocop.yml DELETED
@@ -1 +0,0 @@
1
- inherit_from: .rubocop_todo.yml
data/.rubocop_todo.yml DELETED
@@ -1,29 +0,0 @@
1
- # This configuration was generated by
2
- # `rubocop --auto-gen-config`
3
- # on 2019-06-14 00:44:48 +0200 using RuboCop version 0.71.0.
4
- # The point is for the user to remove these configuration records
5
- # one by one as the offenses are removed from the code base.
6
- # Note that changes in the inspected code, or installation of new
7
- # versions of RuboCop, may require this file to be generated again.
8
-
9
- # Offense count: 1
10
- Metrics/AbcSize:
11
- Max: 18
12
-
13
- # Offense count: 2
14
- # Configuration parameters: CountComments, ExcludedMethods.
15
- # ExcludedMethods: refine
16
- Metrics/BlockLength:
17
- Max: 75
18
-
19
- # Offense count: 1
20
- # Configuration parameters: CountComments, ExcludedMethods.
21
- Metrics/MethodLength:
22
- Max: 11
23
-
24
- # Offense count: 8
25
- # Cop supports --auto-correct.
26
- # Configuration parameters: AutoCorrect, AllowHeredoc, AllowURI, URISchemes, IgnoreCopDirectives, IgnoredPatterns.
27
- # URISchemes: http, https
28
- Metrics/LineLength:
29
- Max: 112
data/.travis.yml DELETED
@@ -1,7 +0,0 @@
1
- ---
2
- sudo: false
3
- language: ruby
4
- cache: bundler
5
- rvm:
6
- - 2.6.3
7
- before_install: gem install bundler -v 2.0.1
data/CODE_OF_CONDUCT.md DELETED
@@ -1,74 +0,0 @@
1
- # Contributor Covenant Code of Conduct
2
-
3
- ## Our Pledge
4
-
5
- In the interest of fostering an open and welcoming environment, we as
6
- contributors and maintainers pledge to making participation in our project and
7
- our community a harassment-free experience for everyone, regardless of age, body
8
- size, disability, ethnicity, gender identity and expression, level of experience,
9
- nationality, personal appearance, race, religion, or sexual identity and
10
- orientation.
11
-
12
- ## Our Standards
13
-
14
- Examples of behavior that contributes to creating a positive environment
15
- include:
16
-
17
- * Using welcoming and inclusive language
18
- * Being respectful of differing viewpoints and experiences
19
- * Gracefully accepting constructive criticism
20
- * Focusing on what is best for the community
21
- * Showing empathy towards other community members
22
-
23
- Examples of unacceptable behavior by participants include:
24
-
25
- * The use of sexualized language or imagery and unwelcome sexual attention or
26
- advances
27
- * Trolling, insulting/derogatory comments, and personal or political attacks
28
- * Public or private harassment
29
- * Publishing others' private information, such as a physical or electronic
30
- address, without explicit permission
31
- * Other conduct which could reasonably be considered inappropriate in a
32
- professional setting
33
-
34
- ## Our Responsibilities
35
-
36
- Project maintainers are responsible for clarifying the standards of acceptable
37
- behavior and are expected to take appropriate and fair corrective action in
38
- response to any instances of unacceptable behavior.
39
-
40
- Project maintainers have the right and responsibility to remove, edit, or
41
- reject comments, commits, code, wiki edits, issues, and other contributions
42
- that are not aligned to this Code of Conduct, or to ban temporarily or
43
- permanently any contributor for other behaviors that they deem inappropriate,
44
- threatening, offensive, or harmful.
45
-
46
- ## Scope
47
-
48
- This Code of Conduct applies both within project spaces and in public spaces
49
- when an individual is representing the project or its community. Examples of
50
- representing a project or community include using an official project e-mail
51
- address, posting via an official social media account, or acting as an appointed
52
- representative at an online or offline event. Representation of a project may be
53
- further defined and clarified by project maintainers.
54
-
55
- ## Enforcement
56
-
57
- Instances of abusive, harassing, or otherwise unacceptable behavior may be
58
- reported by contacting the project team at contact@cyril.email. All
59
- complaints will be reviewed and investigated and will result in a response that
60
- is deemed necessary and appropriate to the circumstances. The project team is
61
- obligated to maintain confidentiality with regard to the reporter of an incident.
62
- Further details of specific enforcement policies may be posted separately.
63
-
64
- Project maintainers who do not follow or enforce the Code of Conduct in good
65
- faith may face temporary or permanent repercussions as determined by other
66
- members of the project's leadership.
67
-
68
- ## Attribution
69
-
70
- This Code of Conduct is adapted from the [Contributor Covenant][homepage], version 1.4,
71
- available at [http://contributor-covenant.org/version/1/4][version]
72
-
73
- [homepage]: http://contributor-covenant.org
74
- [version]: http://contributor-covenant.org/version/1/4/
data/Gemfile DELETED
@@ -1,5 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- source 'https://rubygems.org'
4
-
5
- gemspec
data/Gemfile.lock DELETED
@@ -1,61 +0,0 @@
1
- PATH
2
- remote: .
3
- specs:
4
- accept_language (1.0.0)
5
-
6
- GEM
7
- remote: https://rubygems.org/
8
- specs:
9
- ast (2.4.0)
10
- diff-lcs (1.3)
11
- docile (1.3.2)
12
- jaro_winkler (1.5.2)
13
- json (2.2.0)
14
- parallel (1.17.0)
15
- parser (2.6.3.0)
16
- ast (~> 2.4.0)
17
- rainbow (3.0.0)
18
- rake (10.5.0)
19
- rspec (3.8.0)
20
- rspec-core (~> 3.8.0)
21
- rspec-expectations (~> 3.8.0)
22
- rspec-mocks (~> 3.8.0)
23
- rspec-core (3.8.1)
24
- rspec-support (~> 3.8.0)
25
- rspec-expectations (3.8.4)
26
- diff-lcs (>= 1.2.0, < 2.0)
27
- rspec-support (~> 3.8.0)
28
- rspec-mocks (3.8.1)
29
- diff-lcs (>= 1.2.0, < 2.0)
30
- rspec-support (~> 3.8.0)
31
- rspec-support (3.8.2)
32
- rubocop (0.71.0)
33
- jaro_winkler (~> 1.5.1)
34
- parallel (~> 1.10)
35
- parser (>= 2.6)
36
- rainbow (>= 2.2.2, < 4.0)
37
- ruby-progressbar (~> 1.7)
38
- unicode-display_width (>= 1.4.0, < 1.7)
39
- ruby-progressbar (1.10.1)
40
- simplecov (0.16.1)
41
- docile (~> 1.1)
42
- json (>= 1.8, < 3)
43
- simplecov-html (~> 0.10.0)
44
- simplecov-html (0.10.2)
45
- unicode-display_width (1.6.0)
46
- yard (0.9.19)
47
-
48
- PLATFORMS
49
- ruby
50
-
51
- DEPENDENCIES
52
- accept_language!
53
- bundler (~> 2.0)
54
- rake (~> 10.0)
55
- rspec (~> 3.0)
56
- rubocop (~> 0.71)
57
- simplecov (~> 0.16)
58
- yard (~> 0.9)
59
-
60
- BUNDLED WITH
61
- 2.0.1
data/Rakefile DELETED
@@ -1,20 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- require 'bundler/gem_tasks'
4
- require 'rspec/core/rake_task'
5
-
6
- RSpec::Core::RakeTask.new(:spec)
7
-
8
- require 'rubocop/rake_task'
9
-
10
- RuboCop::RakeTask.new
11
-
12
- namespace :test do
13
- task :coverage do
14
- ENV['COVERAGE'] = 'true'
15
- Rake::Task['test'].invoke
16
- end
17
- end
18
-
19
- task(:doc_stats) { ruby '-S yard stats' }
20
- task default: %i[spec doc_stats rubocop]
data/VERSION.semver DELETED
@@ -1 +0,0 @@
1
- 1.0.0
@@ -1,29 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- Gem::Specification.new do |spec|
4
- spec.name = 'accept_language'
5
- spec.version = File.read('VERSION.semver').chomp
6
- spec.authors = ['Cyril Kato']
7
- spec.email = ['contact@cyril.email']
8
-
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 hash of languages and qualities.'
12
- spec.homepage = 'https://github.com/cyril/accept_language.rb'
13
- spec.license = 'MIT'
14
-
15
- spec.files = Dir.chdir(File.expand_path(__dir__)) do
16
- `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^spec/}) }
17
- end
18
-
19
- spec.bindir = 'exe'
20
- spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
21
- spec.require_paths = ['lib']
22
-
23
- spec.add_development_dependency 'bundler', '~> 2.0'
24
- spec.add_development_dependency 'rake', '~> 10.0'
25
- spec.add_development_dependency 'rspec', '~> 3.0'
26
- spec.add_development_dependency 'rubocop', '~> 0.71'
27
- spec.add_development_dependency 'simplecov', '~> 0.16'
28
- spec.add_development_dependency 'yard', '~> 0.9'
29
- end
data/bin/console DELETED
@@ -1,8 +0,0 @@
1
- #!/usr/bin/env ruby
2
- # frozen_string_literal: true
3
-
4
- require 'bundler/setup'
5
- require 'accept_language'
6
-
7
- require 'irb'
8
- IRB.start(__FILE__)
data/bin/setup DELETED
@@ -1,8 +0,0 @@
1
- #!/usr/bin/env bash
2
- # frozen_string_literal: true
3
-
4
- set -euo pipefail
5
- IFS=$'\n\t'
6
- set -vx
7
-
8
- bundle install
@@ -1,58 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- module AcceptLanguage
4
- # @example
5
- # AcceptLanguage::Intersection.new('ja, en-gb;q=0.8, en;q=0.7', :ar, :ja).call # => :ja
6
- # @note Compare an Accept-Language header value with your application's
7
- # supported languages to find the common languages that could be presented
8
- # to a user.
9
- # @see https://tools.ietf.org/html/rfc2616#section-14.4
10
- class Intersection
11
- attr_reader :preferences, :supported_langs
12
-
13
- def initialize(raw_input, *supported_langs, two_letter_truncate: true)
14
- @preferences = Parser.call(raw_input, two_letter_truncate: two_letter_truncate)
15
-
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
21
- end
22
-
23
- def call
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
31
-
32
- return tag if supported_langs.include?(tag)
33
- end
34
-
35
- nil
36
- end
37
-
38
- protected
39
-
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
50
- end
51
-
52
- def wildcard?(value)
53
- value.equal?(:*)
54
- end
55
- end
56
- end
57
-
58
- require_relative 'parser'