http-accept 1.2.0 → 1.3.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
  SHA1:
3
- metadata.gz: 4850286afbb8944881e7bbf281c13b68cf2b3ec5
4
- data.tar.gz: 8d35dbe069f01a8c15777dca2f3aaeee88188106
3
+ metadata.gz: 326c4ed19982fc140d5b4d9c691b99333f12e679
4
+ data.tar.gz: 5bacffff48f9a58e8eca3fb045591ef35d787398
5
5
  SHA512:
6
- metadata.gz: bd96bc2dd0c9bf3f757d8c655d3ee493d2099e3f0995ee873b0df189714ce71316b4ab80460ba05af62feb3fcb1d9a7f4354280a559cd0b3cd974103cac38afb
7
- data.tar.gz: fcc95a0eb6068af7715c5d8c9bac5ca74d91a2a5bef2cb7e0aaa51ef47e9bfaa58c33e0019219e49452ea6702657315c2c95d0e5cb80a7d9f2f819cc5d609d1f
6
+ metadata.gz: fff4f229ca636f688d921a81f8f62fa585eda081680827dea0a63ade5f834c9620438afdbff51301619417ce3da5dcffe12a46cad9a40d57305bbe1f999e73a7
7
+ data.tar.gz: 3b11ffa81002bac08ab011ad45a41ce598b3254d56c690951405901d0514bb337dd5fcfbf834e23665a2ca9f771e61616da44aaa9be999f966c51093810bdcf0
data/README.md CHANGED
@@ -49,7 +49,7 @@ Normally, you'd want to match the media types against some set of available mime
49
49
 
50
50
  module ToJSON
51
51
  def content_type
52
- "application/json"
52
+ HTTP::Accept::ContentType.new("application/json", charset: 'utf-8')
53
53
  end
54
54
 
55
55
  def convert(object, options)
@@ -76,7 +76,7 @@ module HTTP
76
76
  end
77
77
  end
78
78
 
79
- class LanguageRange < Struct.new(:locale, :q)
79
+ LanguageRange = Struct.new(:locale, :q) do
80
80
  def quality_factor
81
81
  (q || 1.0).to_f
82
82
  end
@@ -76,7 +76,7 @@ module HTTP
76
76
  end
77
77
  end
78
78
 
79
- class MediaRange < Struct.new(:mime_type, :parameters)
79
+ MediaRange = Struct.new(:mime_type, :parameters) do
80
80
  def parameters_string
81
81
  return '' if parameters == nil or parameters.empty?
82
82
 
@@ -107,22 +107,28 @@ module HTTP
107
107
  mime_type.split(on, count)
108
108
  end
109
109
 
110
+ def self.parse_parameters(scanner, normalize_whitespace)
111
+ parameters = {}
112
+
113
+ while scanner.scan(PARAMETER)
114
+ key = scanner[:key]
115
+
116
+ # If the regular expression PARAMETER matched, it must be one of these two:
117
+ if value = scanner[:value]
118
+ parameters[key] = value
119
+ elsif quoted_value = scanner[:quoted_value]
120
+ parameters[key] = QuotedString.unquote(quoted_value, normalize_whitespace)
121
+ end
122
+ end
123
+
124
+ return parameters
125
+ end
126
+
110
127
  def self.parse(scanner, normalize_whitespace = true)
111
128
  return to_enum(:parse, scanner, normalize_whitespace) unless block_given?
112
129
 
113
130
  while mime_type = scanner.scan(MIME_TYPE)
114
- parameters = {}
115
-
116
- while scanner.scan(PARAMETER)
117
- key = scanner[:key]
118
-
119
- # If the regular expression PARAMETER matched, it must be one of these two:
120
- if value = scanner[:value]
121
- parameters[key] = value
122
- elsif quoted_value = scanner[:quoted_value]
123
- parameters[key] = QuotedString.unquote(quoted_value, normalize_whitespace)
124
- end
125
- end
131
+ parameters = parse_parameters(scanner, normalize_whitespace)
126
132
 
127
133
  yield self.new(mime_type, parameters)
128
134
 
@@ -141,6 +147,25 @@ module HTTP
141
147
 
142
148
  return Sort.by_quality_factor(media_types)
143
149
  end
150
+
151
+ HTTP_ACCEPT = 'HTTP_ACCEPT'.freeze
152
+ WILDCARD_MEDIA_RANGE = MediaRange.new("*/*", {}).freeze
153
+
154
+ # Parse the list of browser preferred content types and return ordered by priority. If no `Accept:` header is specified, the behaviour is the same as if `Accept: */*` was provided.
155
+ def self.browser_preferred_media_types(env)
156
+ if accept_content_types = env[HTTP_ACCEPT]
157
+ accept_content_types.strip!
158
+
159
+ unless accept_content_types.empty?
160
+ return HTTP::Accept::MediaTypes.parse(accept_content_types)
161
+ end
162
+ end
163
+
164
+ # According to http://tools.ietf.org/html/rfc7231#section-5.3.2:
165
+ # A request without any Accept header field implies that the user agent will accept any media type in response.
166
+ # You should treat a non-existent Accept header as */*.
167
+ return [WILDCARD_MEDIA_RANGE]
168
+ end
144
169
  end
145
170
  end
146
171
  end
@@ -20,6 +20,6 @@
20
20
 
21
21
  module HTTP
22
22
  module Accept
23
- VERSION = "1.2.0"
23
+ VERSION = "1.3.0"
24
24
  end
25
25
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: http-accept
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.2.0
4
+ version: 1.3.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Samuel Williams
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2016-02-16 00:00:00.000000000 Z
11
+ date: 2016-02-18 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler