accept_language 0.1.2 → 2.0.1

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: 3d350dc26f69d89636ac4794c3a9d678cdf44f3f849c1f640b38c959da3637f9
4
- data.tar.gz: ce3bd0c7254ebc9d3dbad9ca6d7503fd6c2dcf396232c0641a8de56872bd97fa
3
+ metadata.gz: 1ac50eb44a0efc222a6155a3b34df19367f2a53fb62abbdffebf0f65534065a3
4
+ data.tar.gz: 66bb74410f88b7fcdbddc1a1203992ff17f80c0468df1b6e9d7f6d47e600fd90
5
5
  SHA512:
6
- metadata.gz: 8a2c7f1ca2b56dc774aa2c62dc1ab58cf0ed25dc2dd02e55f9a5c7fafe162480855068222a1cc73aeeefe4ec594abb72bebdcf7adbf179b786015837ca9abb27
7
- data.tar.gz: 177e781389b91cf7e9a2bc572e318abef16fd4d6267fb83a61cc2d0f6609106b39a56f85ea0f8a5d6070fb924c49d69c0262e828b983b275fc069a725ba1885d
6
+ metadata.gz: 22dc9a87d06c5b2514baf72d635457f7cd3cb538c24366f4c585022b89e4a16589671de205752a3c0c5fb6694bd5b1f235545b16454eeff6e5926d71c33b2172
7
+ data.tar.gz: cec4f53f39df95224b3007fa173287a41f3a488102ff3b3211d24a12ff5e7f2cd0a3872698bed95e017021936669a69294b0c65d94fee7cb95d2d253022ed19c
@@ -1,6 +1,6 @@
1
1
  The MIT License (MIT)
2
2
 
3
- Copyright (c) 2019 Cyril Kato
3
+ Copyright (c) 2019-2021 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
@@ -5,7 +5,7 @@ A tiny library for parsing the `Accept-Language` header from browsers (as define
5
5
  ## Status
6
6
 
7
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)
8
+ [![Build Status](https://travis-ci.org/cyril/accept_language.rb.svg?branch=master)](https://travis-ci.org/cyril/accept_language.rb)
9
9
  [![Inline Docs](https://inch-ci.org/github/cyril/accept_language.rb.svg)](https://inch-ci.org/github/cyril/accept_language.rb)
10
10
 
11
11
  ## Installation
@@ -13,7 +13,7 @@ A tiny library for parsing the `Accept-Language` header from browsers (as define
13
13
  Add this line to your application's Gemfile:
14
14
 
15
15
  ```ruby
16
- gem 'accept_language'
16
+ gem "accept_language"
17
17
  ```
18
18
 
19
19
  And then execute:
@@ -28,31 +28,53 @@ Or install it yourself as:
28
28
 
29
29
  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
30
 
31
- ### Examples
31
+ 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.
32
+
33
+ Some examples:
32
34
 
33
35
  ```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, :*]
36
+ AcceptLanguage.parse("da, en-GB;q=0.8, en;q=0.7").match(:en, :da) # => :da
37
+ AcceptLanguage.parse("da, en;q=0.8, ug;q=0.9").match("en-GB", "ug-CN") # => "ug-CN"
38
+ AcceptLanguage.parse("da, en-GB;q=0.8, en;q=0.7").match(:ja) # => nil
39
+ AcceptLanguage.parse("fr-CH").match(:fr) # => nil
40
+ AcceptLanguage.parse("de, zh;q=0.4, fr;q=0").match(:fr) # => nil
41
+ AcceptLanguage.parse("de, zh;q=0.4, *;q=0.5, fr;q=0").match(:ar) # => :ar
42
+ AcceptLanguage.parse("uz-latn-uz").match("uz-Latn-UZ") # => "uz-Latn-UZ"
43
+ AcceptLanguage.parse("foo;q=0.1").match(:FoO) # => :FoO
44
+ AcceptLanguage.parse("foo").match("bar") # => nil
45
+ AcceptLanguage.parse("*").match("BaZ") # => "BaZ"
46
+ AcceptLanguage.parse("*;q=0").match("foobar") # => nil
47
+ AcceptLanguage.parse("en, en;q=0").match("en") # => nil
48
+ AcceptLanguage.parse("*, en;q=0").match("en") # => nil
36
49
  ```
37
50
 
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.
39
-
40
- ### Examples
51
+ ### Rails integration example
41
52
 
42
53
  ```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
51
- ```
54
+ # app/controllers/application_controller.rb
55
+ class ApplicationController < ActionController::Base
56
+ before_action :best_locale_from_request!
57
+
58
+ def best_locale_from_request!
59
+ I18n.locale = best_locale_from_request
60
+ end
61
+
62
+ def best_locale_from_request
63
+ return I18n.default_locale unless request.headers.key?("HTTP_ACCEPT_LANGUAGE")
52
64
 
53
- ## Contributing
65
+ string = request.headers.fetch("HTTP_ACCEPT_LANGUAGE")
66
+ locale = AcceptLanguage.parse(string).match(*I18n.available_locales)
54
67
 
55
- 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.
68
+ # If the server cannot serve any matching language,
69
+ # it can theoretically send back a 406 (Not Acceptable) error code.
70
+ # But, for a better user experience, this is rarely done and more
71
+ # common way is to ignore the Accept-Language header in this case.
72
+ return I18n.default_locale if locale.nil?
73
+
74
+ locale
75
+ end
76
+ end
77
+ ```
56
78
 
57
79
  ## Versioning
58
80
 
@@ -61,7 +83,3 @@ __AcceptLanguage__ uses [Semantic Versioning 2.0.0](https://semver.org/)
61
83
  ## License
62
84
 
63
85
  The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
64
-
65
- ## Code of Conduct
66
-
67
- 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).
@@ -1,15 +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
5
- def self.intersection(raw_input, *supported_languages, truncate: true)
6
- Intersection.new(raw_input, *supported_languages, truncate: truncate).call
7
- end
8
-
9
- def self.parse(raw_input)
10
- Parser.new(raw_input).call
8
+ # @note Parse an Accept-Language header field into a language range.
9
+ # @example
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)
11
14
  end
12
15
  end
13
16
 
14
- require_relative 'accept_language/intersection'
15
- require_relative 'accept_language/parser'
17
+ require_relative "accept_language/parser"
@@ -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,34 +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
- # 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.
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}>
8
7
  # @see https://tools.ietf.org/html/rfc2616#section-14.4
9
8
  class Parser
10
- def initialize(raw_input)
11
- @string = raw_input.to_s
12
- end
9
+ attr_reader :languages_range
13
10
 
14
- def call
15
- preferences.sort { |lang_a, lang_b| lang_b.fetch(1) <=> lang_a.fetch(1) }
16
- .map(&:first)
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)
17
15
  end
18
16
 
19
- protected
20
-
21
- def preferences
22
- @string.delete(' ').split(',').inject({}) do |result, language|
23
- tag, quality = language.split(/;q=/i)
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
24
25
 
25
- tag = tag.downcase
26
- quality = quality.nil? ? 1.0 : quality.to_f
26
+ private
27
27
 
28
- next result if quality.zero?
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?
29
36
 
30
- result.merge(tag.to_sym => quality)
37
+ quality = quality.nil? ? BigDecimal("1") : BigDecimal(quality)
38
+ hash.merge(tag => quality)
31
39
  end
32
40
  end
33
41
  end
34
42
  end
43
+
44
+ require "bigdecimal"
45
+ require_relative "matcher"
metadata CHANGED
@@ -1,124 +1,166 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: accept_language
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.2
4
+ version: 2.0.1
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-12 00:00:00.000000000 Z
11
+ date: 2021-05-03 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
42
  name: rspec
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
- list of languages sorted by quality.
99
- email:
100
- - contact@cyril.email
154
+ hash of languages and qualities.
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:
@@ -132,14 +174,14 @@ required_ruby_version: !ruby/object:Gem::Requirement
132
174
  requirements:
133
175
  - - ">="
134
176
  - !ruby/object:Gem::Version
135
- version: '0'
177
+ version: 2.7.0
136
178
  required_rubygems_version: !ruby/object:Gem::Requirement
137
179
  requirements:
138
180
  - - ">="
139
181
  - !ruby/object:Gem::Version
140
182
  version: '0'
141
183
  requirements: []
142
- rubygems_version: 3.0.3
184
+ rubygems_version: 3.1.2
143
185
  signing_key:
144
186
  specification_version: 4
145
187
  summary: Parser for Accept-Language request HTTP header
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-12 08:15:50 +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: 19
12
-
13
- # Offense count: 1
14
- # Configuration parameters: CountComments, ExcludedMethods.
15
- # ExcludedMethods: refine
16
- Metrics/BlockLength:
17
- Max: 80
18
-
19
- # Offense count: 1
20
- # Configuration parameters: CountComments, ExcludedMethods.
21
- Metrics/MethodLength:
22
- Max: 11
23
-
24
- # Offense count: 15
25
- # Cop supports --auto-correct.
26
- # Configuration parameters: AutoCorrect, AllowHeredoc, AllowURI, URISchemes, IgnoreCopDirectives, IgnoredPatterns.
27
- # URISchemes: http, https
28
- Metrics/LineLength:
29
- Max: 110
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 (0.1.2)
5
-
6
- GEM
7
- remote: https://rubygems.org/
8
- specs:
9
- ast (2.4.0)
10
- diff-lcs (1.3)
11
- docile (1.3.1)
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.0)
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.0)
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
- 0.1.2
@@ -1,30 +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 list of languages sorted by ' \
12
- 'quality.'
13
- spec.homepage = 'https://github.com/cyril/accept_language.rb'
14
- spec.license = 'MIT'
15
-
16
- spec.files = Dir.chdir(File.expand_path(__dir__)) do
17
- `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^spec/}) }
18
- end
19
-
20
- spec.bindir = 'exe'
21
- spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
22
- spec.require_paths = ['lib']
23
-
24
- spec.add_development_dependency 'bundler', '~> 2.0'
25
- spec.add_development_dependency 'rake', '~> 10.0'
26
- spec.add_development_dependency 'rspec', '~> 3.0'
27
- spec.add_development_dependency 'rubocop', '~> 0.71'
28
- spec.add_development_dependency 'simplecov', '~> 0.16'
29
- spec.add_development_dependency 'yard', '~> 0.9'
30
- 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,52 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- module AcceptLanguage
4
- # @example
5
- # Intersection.new('da, en-gb;q=0.8, en;q=0.7', :ar, :ja, :ro).call
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 :accepted_languages, :default_supported_language, :other_supported_languages
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
17
-
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
29
- end
30
-
31
- def call
32
- accepted_languages.find do |accepted_language|
33
- break default_supported_language if accepted_language.equal?(wildcard)
34
-
35
- supported_languages.include?(accepted_language)
36
- end
37
- end
38
-
39
- protected
40
-
41
- def supported_languages
42
- other_supported_languages + Set[default_supported_language]
43
- end
44
-
45
- def wildcard
46
- :*
47
- end
48
- end
49
- end
50
-
51
- require_relative 'parser'
52
- require 'set'