accept_headers 0.0.3 → 0.0.4
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/CHANGELOG.md +9 -3
- data/README.md +4 -1
- data/lib/accept_headers/language.rb +5 -1
- data/lib/accept_headers/media_type.rb +5 -1
- data/lib/accept_headers/version.rb +1 -1
- data/spec/encoding_spec.rb +1 -1
- data/spec/language_spec.rb +5 -1
- data/spec/media_type/negotiator_spec.rb +18 -0
- data/spec/media_type_spec.rb +5 -1
- data/spec/support/application.csv +1105 -0
- data/spec/support/audio.csv +142 -0
- data/spec/support/image.csv +48 -0
- data/spec/support/message.csv +22 -0
- data/spec/support/model.csv +21 -0
- data/spec/support/multipart.csv +16 -0
- data/spec/support/text.csv +70 -0
- data/spec/support/video.csv +77 -0
- metadata +17 -1
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 9a061f83d7dbd285a45d4bbdbc9be56ebdca2ccc
|
4
|
+
data.tar.gz: 1a5a1496f0f771138858e9b09f0ed128c9ebe7c6
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: c6c86cb2c4470f793c309cd5cf742a90da0cba728551f7df3f0561f4a259d5427338e674b07069137063025c86e7503afca878428018f4afd165968675e0a10c
|
7
|
+
data.tar.gz: 3cb9ce5bdc3863a37126e6bde2e89997c05db3696bf28ad64e6ed330234464bc4c56ff57c5bef2a25ef8abb56174add04e755ff402dc0184986c003e06ddc868
|
data/CHANGELOG.md
CHANGED
@@ -1,11 +1,17 @@
|
|
1
|
-
## 0.
|
1
|
+
## 0.0.4 / November 15, 2014
|
2
|
+
|
3
|
+
* Add MediaType#media_range which is the type/subtype as a string.
|
4
|
+
* Add Language#language_tag which is the primary_tag/subtag as a string.
|
5
|
+
* Test all IANA registered media types against the parser.
|
6
|
+
|
7
|
+
## 0.0.3 / November 15, 2014
|
2
8
|
|
3
9
|
* Remove `Accept-Charset` support since it's obsolete.
|
4
10
|
|
5
|
-
## 0.2 / November 15, 2014
|
11
|
+
## 0.0.2 / November 15, 2014
|
6
12
|
|
7
13
|
* Add `Negotiator`s which can parse and find the best match.
|
8
14
|
|
9
|
-
## 0.1 / November 14, 2014
|
15
|
+
## 0.0.1 / November 14, 2014
|
10
16
|
|
11
17
|
* Initial release.
|
data/README.md
CHANGED
@@ -11,7 +11,8 @@ Some features of the library are:
|
|
11
11
|
* Strict adherence to [RFC 2616][rfc], specifically [section 14][rfc-sec14]
|
12
12
|
* Full support for the [Accept][rfc-sec14-1], [Accept-Encoding][rfc-sec14-3],
|
13
13
|
and [Accept-Language][rfc-sec14-4] HTTP request headers
|
14
|
-
* `Accept-Charset` is not supported because it's [obsolete](https://developer.mozilla.org/en-US/docs/Web/HTTP/Content_negotiation#The_Accept-Charset.3A_header)
|
14
|
+
* `Accept-Charset` is not supported because it's [obsolete](https://developer.mozilla.org/en-US/docs/Web/HTTP/Content_negotiation#The_Accept-Charset.3A_header)
|
15
|
+
* Parser tested against all IANA registered [media types][iana-media-types]
|
15
16
|
* A comprehensive [spec suite][spec] that covers many edge cases
|
16
17
|
|
17
18
|
This library is optimistic when parsing headers. If a specific media type, encoding, charset, or language can't be parsed, is in an invalid format, or contains invalid characters, it will skip that specific entry when constructing the sorted list. If a `q` value can't be read or is in the wrong format (more than 3 decimal places), it will default it to `0.001` so it still has a chance to match. Lack of an explicit `q` value of course defaults to 1.
|
@@ -21,6 +22,7 @@ This library is optimistic when parsing headers. If a specific media type, encod
|
|
21
22
|
[rfc-sec14-1]: http://www.w3.org/Protocols/rfc2616/rfc2616-sec14.html#sec14.1
|
22
23
|
[rfc-sec14-3]: http://www.w3.org/Protocols/rfc2616/rfc2616-sec14.html#sec14.3
|
23
24
|
[rfc-sec14-4]: http://www.w3.org/Protocols/rfc2616/rfc2616-sec14.html#sec14.4
|
25
|
+
[iana-media-types]: https://www.iana.org/assignments/media-types/media-types.xhtml
|
24
26
|
[spec]: http://github.com/kamui/accept_headers/tree/master/spec/
|
25
27
|
|
26
28
|
## Installation
|
@@ -132,6 +134,7 @@ AcceptHeaders::Language.new('en', 'us')
|
|
132
134
|
|
133
135
|
* Write rack middleware
|
134
136
|
* More edge case tests
|
137
|
+
* Add rdoc
|
135
138
|
|
136
139
|
## Contributing
|
137
140
|
|
@@ -63,13 +63,17 @@ module AcceptHeaders
|
|
63
63
|
|
64
64
|
def to_s
|
65
65
|
qvalue = (q == 0 || q == 1) ? q.to_i : q
|
66
|
-
string = "#{
|
66
|
+
string = "#{media_range};q=#{qvalue}"
|
67
67
|
if params.size > 0
|
68
68
|
params.each { |k, v| string.concat(";#{k}=#{v}") }
|
69
69
|
end
|
70
70
|
string
|
71
71
|
end
|
72
72
|
|
73
|
+
def media_range
|
74
|
+
"#{type}/#{subtype}"
|
75
|
+
end
|
76
|
+
|
73
77
|
def match(other)
|
74
78
|
if type == other.type && subtype == other.subtype
|
75
79
|
true
|
data/spec/encoding_spec.rb
CHANGED
data/spec/language_spec.rb
CHANGED
@@ -82,9 +82,13 @@ module AcceptHeaders
|
|
82
82
|
})
|
83
83
|
end
|
84
84
|
|
85
|
-
it "
|
85
|
+
it "converts to string" do
|
86
86
|
s = subject.new('en', 'us', q: 0.9).to_s
|
87
87
|
s.must_equal "en-us;q=0.9"
|
88
88
|
end
|
89
|
+
|
90
|
+
it "outputs the language tag" do
|
91
|
+
subject.new('en', 'us', q: 0.9).language_tag.must_equal "en-us"
|
92
|
+
end
|
89
93
|
end
|
90
94
|
end
|
@@ -36,6 +36,24 @@ module AcceptHeaders
|
|
36
36
|
]
|
37
37
|
end
|
38
38
|
|
39
|
+
it "supports all registered IANA media types" do
|
40
|
+
require 'csv'
|
41
|
+
# https://www.iana.org/assignments/media-types/media-types.xhtml
|
42
|
+
%w[application audio image message model multipart text video].each do |filename|
|
43
|
+
CSV.foreach("spec/support/#{filename}.csv", headers: true) do |row|
|
44
|
+
media_type = row['Template']
|
45
|
+
|
46
|
+
# audio/amr-wb+ is a typo
|
47
|
+
media_type = 'audio/amr-wb+' if media_type == 'amr-wb+'
|
48
|
+
|
49
|
+
if media_type
|
50
|
+
subject.new(media_type).list.size.must_equal 1
|
51
|
+
subject.new(media_type).list.first.media_range.must_equal media_type.downcase
|
52
|
+
end
|
53
|
+
end
|
54
|
+
end
|
55
|
+
end
|
56
|
+
|
39
57
|
it "sets media type to */* when the accept header is empty" do
|
40
58
|
subject.new('').list.must_equal [
|
41
59
|
MediaType.new('*', '*')
|
data/spec/media_type_spec.rb
CHANGED
@@ -91,9 +91,13 @@ module AcceptHeaders
|
|
91
91
|
})
|
92
92
|
end
|
93
93
|
|
94
|
-
it "
|
94
|
+
it "converts to string" do
|
95
95
|
s = subject.new('text', 'html', q: 0.9, params: { 'level' => '1' }).to_s
|
96
96
|
s.must_equal "text/html;q=0.9;level=1"
|
97
97
|
end
|
98
|
+
|
99
|
+
it "outputs the media range" do
|
100
|
+
subject.new('text', 'html', params: { 'level' => '1' }).media_range.must_equal "text/html"
|
101
|
+
end
|
98
102
|
end
|
99
103
|
end
|