http-accept 1.3.0 → 1.4.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: 326c4ed19982fc140d5b4d9c691b99333f12e679
4
- data.tar.gz: 5bacffff48f9a58e8eca3fb045591ef35d787398
3
+ metadata.gz: ce16687c7850ca9d1dd0be4c680adb9b97ee4da6
4
+ data.tar.gz: d499d0388301d0a70af43c4c1a6cb434c20ec603
5
5
  SHA512:
6
- metadata.gz: fff4f229ca636f688d921a81f8f62fa585eda081680827dea0a63ade5f834c9620438afdbff51301619417ce3da5dcffe12a46cad9a40d57305bbe1f999e73a7
7
- data.tar.gz: 3b11ffa81002bac08ab011ad45a41ce598b3254d56c690951405901d0514bb337dd5fcfbf834e23665a2ca9f771e61616da44aaa9be999f966c51093810bdcf0
6
+ metadata.gz: 222edb80960c3dbee1d005edff20f74397ed570bf78f96a23630a6755aecc78918fcff1710d8a5b592b8061629951841c8affcd111d790145667a5a18f2ff6b4
7
+ data.tar.gz: 16dc4e102473806c8b00b24ec29a246f9009d61681a09847d328e81bab6125d1376a3db913b58700f894d04d626c522c39d4795182093fba1fa1d2c23a8ea2db
data/.rspec CHANGED
@@ -1,2 +1,4 @@
1
- --format documentation
2
1
  --color
2
+ --format documentation
3
+ --backtrace
4
+ --warnings
data/README.md CHANGED
@@ -52,6 +52,11 @@ Normally, you'd want to match the media types against some set of available mime
52
52
  HTTP::Accept::ContentType.new("application/json", charset: 'utf-8')
53
53
  end
54
54
 
55
+ # Used for inserting into map.
56
+ def split(*args)
57
+ content_type.split(*args)
58
+ end
59
+
55
60
  def convert(object, options)
56
61
  object.to_json
57
62
  end
@@ -24,58 +24,17 @@ require_relative 'parse_error'
24
24
  require_relative 'quoted_string'
25
25
  require_relative 'sort'
26
26
 
27
+ require_relative 'media_types/map'
28
+
27
29
  module HTTP
28
30
  module Accept
31
+ # Parse and process the HTTP Accept: header.
29
32
  module MediaTypes
30
33
  # According to https://tools.ietf.org/html/rfc7231#section-5.3.2
31
34
  MIME_TYPE = /(#{TOKEN})\/(#{TOKEN})/
32
35
  PARAMETER = /\s*;\s*(?<key>#{TOKEN})=((?<value>#{TOKEN})|(?<quoted_value>#{QUOTED_STRING}))/
33
36
 
34
- # Map a set of mime types to objects.
35
- class Map
36
- WILDCARD = "*/*".freeze
37
-
38
- def initialize
39
- @media_types = {}
40
- end
41
-
42
- def freeze
43
- unless frozen?
44
- @media_types.freeze
45
- @media_types.each{|key,value| value.freeze}
46
-
47
- super
48
- end
49
- end
50
-
51
- # Given a list of content types (e.g. from browser_preferred_content_types), return the best converter.
52
- def for(media_types)
53
- media_types.each do |media_range|
54
- mime_type = case media_range
55
- when String then media_range
56
- else media_range.mime_type
57
- end
58
-
59
- if object = @media_types[mime_type]
60
- return object, media_range
61
- end
62
- end
63
-
64
- return nil
65
- end
66
-
67
- # Add a converter to the collection. A converter can be anything that responds to #content_type.
68
- def << object
69
- type, subtype = object.content_type.split('/')
70
-
71
- @media_types[WILDCARD] = object if @media_types.empty?
72
- @media_types["#{type}/*"] ||= object
73
- @media_types["#{type}/#{subtype}"] ||= object
74
-
75
- return self
76
- end
77
- end
78
-
37
+ # A single entry in the Accept: header, which includes a mime type and associated parameters.
79
38
  MediaRange = Struct.new(:mime_type, :parameters) do
80
39
  def parameters_string
81
40
  return '' if parameters == nil or parameters.empty?
@@ -94,7 +53,7 @@ module HTTP
94
53
  end
95
54
 
96
55
  def to_s
97
- @to_s || "#{mime_type}#{parameters_string}"
56
+ "#{mime_type}#{parameters_string}"
98
57
  end
99
58
 
100
59
  alias to_str to_s
@@ -151,7 +110,7 @@ module HTTP
151
110
  HTTP_ACCEPT = 'HTTP_ACCEPT'.freeze
152
111
  WILDCARD_MEDIA_RANGE = MediaRange.new("*/*", {}).freeze
153
112
 
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.
113
+ # 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 (according to RFC).
155
114
  def self.browser_preferred_media_types(env)
156
115
  if accept_content_types = env[HTTP_ACCEPT]
157
116
  accept_content_types.strip!
@@ -0,0 +1,86 @@
1
+ # Copyright, 2016, by Samuel G. D. Williams. <http://www.codeotaku.com>
2
+ #
3
+ # Permission is hereby granted, free of charge, to any person obtaining a copy
4
+ # of this software and associated documentation files (the "Software"), to deal
5
+ # in the Software without restriction, including without limitation the rights
6
+ # to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
7
+ # copies of the Software, and to permit persons to whom the Software is
8
+ # furnished to do so, subject to the following conditions:
9
+ #
10
+ # The above copyright notice and this permission notice shall be included in
11
+ # all copies or substantial portions of the Software.
12
+ #
13
+ # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
14
+ # IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
15
+ # FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
16
+ # AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
17
+ # LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
18
+ # OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
19
+ # THE SOFTWARE.
20
+
21
+ module HTTP
22
+ module Accept
23
+ module MediaTypes
24
+ # Map a set of mime types to objects.
25
+ class Map
26
+ WILDCARD = "*/*".freeze
27
+
28
+ def initialize
29
+ @media_types = {}
30
+ end
31
+
32
+ def freeze
33
+ unless frozen?
34
+ @media_types.freeze
35
+ @media_types.each{|key,value| value.freeze}
36
+
37
+ super
38
+ end
39
+ end
40
+
41
+ # Given a list of content types (e.g. from browser_preferred_content_types), return the best converter. Media types can be an array of MediaRange or String values.
42
+ def for(media_types)
43
+ media_types.each do |media_range|
44
+ mime_type = case media_range
45
+ when String then media_range
46
+ else media_range.mime_type
47
+ end
48
+
49
+ if object = @media_types[mime_type]
50
+ return object, media_range
51
+ end
52
+ end
53
+
54
+ return nil
55
+ end
56
+
57
+ def []= media_range, object
58
+ @media_types[media_range] = object
59
+ end
60
+
61
+ def [] media_range
62
+ @media_types[media_range]
63
+ end
64
+
65
+ # Add a converter to the collection. A converter can be anything that responds to #content_type. Objects will be considered in the order they are added, subsequent objects cannot override previously defined media types. `object` must respond to #split('/', 2) which should give the type and subtype.
66
+ def << object
67
+ type, subtype = object.split('/', 2)
68
+
69
+ # We set the default if not specified already:
70
+ @media_types[WILDCARD] = object if @media_types.empty?
71
+
72
+ if type != '*'
73
+ @media_types["#{type}/*"] ||= object
74
+
75
+ if subtype != '*'
76
+ @media_types["#{type}/#{subtype}"] ||= object
77
+ end
78
+ end
79
+
80
+ return self
81
+ end
82
+ end
83
+ end
84
+ end
85
+ end
86
+
@@ -21,6 +21,7 @@
21
21
  module HTTP
22
22
  module Accept
23
23
  module Sort
24
+ # This sorts items with higher priority first, and keeps items with the same priority in the same relative order.
24
25
  def self.by_quality_factor(items)
25
26
  # We do this to get a stable sort:
26
27
  items.sort_by.with_index{|object, index| [-object.quality_factor, index]}
@@ -20,6 +20,6 @@
20
20
 
21
21
  module HTTP
22
22
  module Accept
23
- VERSION = "1.3.0"
23
+ VERSION = "1.4.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.3.0
4
+ version: 1.4.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-18 00:00:00.000000000 Z
11
+ date: 2016-02-19 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler
@@ -71,6 +71,7 @@ files:
71
71
  - lib/http/accept/content_type.rb
72
72
  - lib/http/accept/languages.rb
73
73
  - lib/http/accept/media_types.rb
74
+ - lib/http/accept/media_types/map.rb
74
75
  - lib/http/accept/parse_error.rb
75
76
  - lib/http/accept/quoted_string.rb
76
77
  - lib/http/accept/sort.rb