mynyml-rack-supported-media-types 0.9.2 → 0.9.3

Sign up to get free protection for your applications and to get access to all the features.
data/README CHANGED
@@ -5,6 +5,10 @@ Returns '406 Not Acceptable' status when unsuported type is requested.
5
5
 
6
6
  The requested type is assumed to be the first content-type in the Accept header's list.
7
7
 
8
+ ===== Install
9
+
10
+ gem install mynyml-rack-supported-media-types --source=http://gems.github.com/
11
+
8
12
  ===== Example
9
13
 
10
14
  require 'rack'
@@ -35,6 +39,8 @@ To read the requested type from the url's extension instead of the Accept
35
39
  header (which is a more realistic use case), use Rack::AbstractFormat before
36
40
  Rack::SupportedMediaTypes.
37
41
 
42
+ # gem install mynyml-rack-abstract-format --source=http://gems.github.com/
43
+
38
44
  require 'rack'
39
45
  require 'rack/abstract_format'
40
46
  require 'rack/supported_media_types'
data/Rakefile CHANGED
@@ -22,7 +22,7 @@ end
22
22
 
23
23
  spec = Gem::Specification.new do |s|
24
24
  s.name = 'rack-supported-media-types'
25
- s.version = '0.9.2'
25
+ s.version = '0.9.3'
26
26
  s.summary = "Rack middleware to specify an app's supported media types."
27
27
  s.description = "Rack middleware to specify an app's supported media types. Returns '406 Not Acceptable' status when unsuported type is requested."
28
28
  s.author = "Martin Aumont"
@@ -31,6 +31,8 @@ spec = Gem::Specification.new do |s|
31
31
  s.has_rdoc = true
32
32
  s.require_path = "lib"
33
33
  s.files = all_except([/doc/, /pkg/])
34
+
35
+ s.add_dependency 'mynyml-rack-accept-media-types', '>= 0.6'
34
36
  end
35
37
 
36
38
  desc "Generate rdoc documentation."
@@ -1,10 +1,13 @@
1
1
  # run me with:
2
2
  # $rackup examples/recommended.ru -p 3000
3
3
  #
4
+ # and point your web browser to:
5
+ # localhost:3000/foo.html
6
+ #
4
7
  require 'pathname'
5
8
  require 'rubygems'
6
9
  require 'rack'
7
- require 'rack/abstract_format'
10
+ require 'rack/abstract_format' #gem install mynyml-rack-abstract-format --source=http://gems.github.com/
8
11
 
9
12
  root = Pathname(__FILE__).dirname.parent.expand_path
10
13
  require root + 'lib/rack/supported_media_types'
@@ -21,8 +24,8 @@ try using extensions:
21
24
  - /foo.txt
22
25
  - ...
23
26
 
24
- app will only accept .html and .xml requests. others will return 406
25
- (check server's output in console to see it)
27
+ app will only accept html and xml requests. others will return 406 (check
28
+ server's output in console to see it).
26
29
  </pre>
27
30
  BODY
28
31
  [200, {'Content-Type' => 'text/html'}, [body]]
@@ -21,8 +21,8 @@ class App
21
21
  end
22
22
  end
23
23
 
24
- # given that the request comes from a webbrowser, and that it's Accept header's
25
- # first value is text/html...
24
+ # given that the request comes from a web browser, and that it's Accept
25
+ # header's highest value is text/html...
26
26
 
27
27
  map '/' do
28
28
  # ... this will allow it through to the app
@@ -1,36 +1,37 @@
1
+ gem 'rack-accept-media-types', '>= 0.6'
2
+ require 'rack/accept_media_types'
3
+
1
4
  module Rack
2
5
  class SupportedMediaTypes
3
6
 
4
- #--
5
- # NOTE
6
- # Ths reason for adding nil to the list of accepted mime types:
7
- #
8
- # "If no Accept header field is present, then it is assumed that the client accepts all media types"
9
- # http://www.w3.org/Protocols/rfc2616/rfc2616-sec14.html
10
- #
11
- # Counter intuitive?
12
- #
13
7
  def initialize(app, mime_types)
14
- @app, @mime_types = app, mime_types.push(nil)
8
+ @app, @mime_types = app, mime_types
15
9
  end
16
10
 
17
11
  #--
18
12
  # return any headers with 406?
19
13
  def call(env)
20
- @mime_types.include?(accept(env)) ?
14
+ req_type = accept(env)
15
+ !req_type.empty? && @mime_types.any? {|type| type.match(/#{req_type}/) } ?
21
16
  @app.call(env) :
22
17
  Rack::Response.new([], 406).finish
23
18
  end
24
19
 
25
20
  private
26
21
  #--
27
- # First content-type in Accept header's list, without params
22
+ # Client's prefered media type.
23
+ #
24
+ # NOTE
25
+ # glob patterns are replaced with regexp.
26
+ #
27
+ # */* -> .*/.*
28
+ # text/* -> text/.*
29
+ #
30
+ # NOTE
31
+ # Rack::AcceptMediaTypes.prefered is */* if Accept header is nil
32
+ #
28
33
  def accept(env)
29
- header = env['HTTP_ACCEPT']
30
-
31
- (header.nil? || header.empty?) ?
32
- header :
33
- header.split(',').first.split(';').first
34
+ Rack::AcceptMediaTypes.new(env['HTTP_ACCEPT']).prefered.to_s.gsub(/\*/, '.*')
34
35
  end
35
36
  end
36
37
  end
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: rack-supported-media-types
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.9.2
4
+ version: 0.9.3
5
5
  platform: ruby
6
6
  authors:
7
7
  - Martin Aumont
@@ -9,10 +9,19 @@ autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
11
 
12
- date: 2009-06-05 00:00:00 -04:00
12
+ date: 2009-06-08 00:00:00 -04:00
13
13
  default_executable:
14
- dependencies: []
15
-
14
+ dependencies:
15
+ - !ruby/object:Gem::Dependency
16
+ name: mynyml-rack-accept-media-types
17
+ type: :runtime
18
+ version_requirement:
19
+ version_requirements: !ruby/object:Gem::Requirement
20
+ requirements:
21
+ - - ">="
22
+ - !ruby/object:Gem::Version
23
+ version: "0.6"
24
+ version:
16
25
  description: Rack middleware to specify an app's supported media types. Returns '406 Not Acceptable' status when unsuported type is requested.
17
26
  email: mynyml@gmail.com
18
27
  executables: []
@@ -20,29 +20,31 @@ class SupportedMediaTypesTest < Test::Unit::TestCase
20
20
  assert_equal 200, response.status
21
21
  end
22
22
 
23
- test "requested type is assumed to be first type in Accept header's list" do
23
+ test "requested type is assumed to be highest ranking type in Accept header's list" do
24
24
  app = SMT.new(App, %w( text/html ))
25
25
  client = Rack::MockRequest.new(app)
26
26
 
27
27
  response = client.get('/', 'HTTP_ACCEPT' => 'text/html,application/xml')
28
28
  assert_equal 200, response.status
29
29
 
30
- response = client.get('/', 'HTTP_ACCEPT' => 'application/xml,text/html')
30
+ response = client.get('/', 'HTTP_ACCEPT' => 'text/html;q=0.8,application/xml;q=0.9')
31
31
  assert_equal 406, response.status
32
32
  end
33
33
 
34
- test "ignores content-type params" do
34
+ test "matches wildcard media-range" do
35
35
  app = SMT.new(App, %w( application/xml application/json ))
36
36
 
37
- response = Rack::MockRequest.new(app).get('/', 'HTTP_ACCEPT' => 'application/xml;q=0.9')
38
- assert_equal 200, response.status
37
+ assert_nothing_raised do
38
+ response = Rack::MockRequest.new(app).get('/', 'HTTP_ACCEPT' => '*/*')
39
+ assert_equal 200, response.status
40
+ end
39
41
  end
40
42
 
41
- test "nil Accept header means all types accepted" do
42
- app = SMT.new(App, %w( application/xml application/json ))
43
+ test "matches wildcard media-range subtypes" do
44
+ app = SMT.new(App, %w( text/html application/json ))
43
45
 
44
46
  assert_nothing_raised do
45
- response = Rack::MockRequest.new(app).get('/', 'HTTP_ACCEPT' => nil)
47
+ response = Rack::MockRequest.new(app).get('/', 'HTTP_ACCEPT' => 'text/*')
46
48
  assert_equal 200, response.status
47
49
  end
48
50
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: mynyml-rack-supported-media-types
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.9.2
4
+ version: 0.9.3
5
5
  platform: ruby
6
6
  authors:
7
7
  - Martin Aumont
@@ -9,10 +9,19 @@ autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
11
 
12
- date: 2009-06-04 21:00:00 -07:00
12
+ date: 2009-06-07 21:00:00 -07:00
13
13
  default_executable:
14
- dependencies: []
15
-
14
+ dependencies:
15
+ - !ruby/object:Gem::Dependency
16
+ name: mynyml-rack-accept-media-types
17
+ type: :runtime
18
+ version_requirement:
19
+ version_requirements: !ruby/object:Gem::Requirement
20
+ requirements:
21
+ - - ">="
22
+ - !ruby/object:Gem::Version
23
+ version: "0.6"
24
+ version:
16
25
  description: Rack middleware to specify an app's supported media types. Returns '406 Not Acceptable' status when unsuported type is requested.
17
26
  email: mynyml@gmail.com
18
27
  executables: []