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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: a140173043fff3155b04a77f622823cf12838bc5
4
- data.tar.gz: 4dc92d7058528b3a06766be6f0e9a3dfb2408b84
3
+ metadata.gz: 9a061f83d7dbd285a45d4bbdbc9be56ebdca2ccc
4
+ data.tar.gz: 1a5a1496f0f771138858e9b09f0ed128c9ebe7c6
5
5
  SHA512:
6
- metadata.gz: 597d9faad4500046b88f07648c1372120a3c008734c7f100a098c7d2ecc568ec4bb335c1a2d94708729ef0669247a136be62491680655e52bbaae5b22155f13c
7
- data.tar.gz: 008ad53a88a1bc15b33564dc68361e116aa19c432ccf09c49529773ca293cb2f5fc7e8804562b594d5a801e2c90750ada349ba74029bf5c27335ee743b5bae02
6
+ metadata.gz: c6c86cb2c4470f793c309cd5cf742a90da0cba728551f7df3f0561f4a259d5427338e674b07069137063025c86e7503afca878428018f4afd165968675e0a10c
7
+ data.tar.gz: 3cb9ce5bdc3863a37126e6bde2e89997c05db3696bf28ad64e6ed330234464bc4c56ff57c5bef2a25ef8abb56174add04e755ff402dc0184986c003e06ddc868
data/CHANGELOG.md CHANGED
@@ -1,11 +1,17 @@
1
- ## 0.3 / November 15, 2014
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
 
@@ -49,7 +49,11 @@ module AcceptHeaders
49
49
 
50
50
  def to_s
51
51
  qvalue = (q == 0 || q == 1) ? q.to_i : q
52
- "#{primary_tag}-#{subtag};q=#{qvalue}"
52
+ "#{language_tag};q=#{qvalue}"
53
+ end
54
+
55
+ def language_tag
56
+ "#{primary_tag}-#{subtag}"
53
57
  end
54
58
 
55
59
  def match(other)
@@ -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 = "#{type}/#{subtype};q=#{qvalue}"
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
@@ -1,3 +1,3 @@
1
1
  module AcceptHeaders
2
- VERSION = "0.0.3"
2
+ VERSION = "0.0.4"
3
3
  end
@@ -64,7 +64,7 @@ module AcceptHeaders
64
64
  })
65
65
  end
66
66
 
67
- it "convers to string" do
67
+ it "converts to string" do
68
68
  s = subject.new('gzip', q: 0.9).to_s
69
69
  s.must_equal "gzip;q=0.9"
70
70
  end
@@ -82,9 +82,13 @@ module AcceptHeaders
82
82
  })
83
83
  end
84
84
 
85
- it "convers to string" do
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('*', '*')
@@ -91,9 +91,13 @@ module AcceptHeaders
91
91
  })
92
92
  end
93
93
 
94
- it "convers to string" do
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