rack-supported-media-types 0.9.4
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- data/.gitignore +3 -0
- data/LICENSE +19 -0
- data/Manifest +11 -0
- data/README +67 -0
- data/Rakefile +26 -0
- data/examples/recommended.ru +40 -0
- data/examples/simple.ru +36 -0
- data/lib/rack/supported_media_types.rb +36 -0
- data/rack-supported-media-types.gemspec +18 -0
- data/test/test_helper.rb +17 -0
- data/test/test_supported_media_types.rb +53 -0
- metadata +94 -0
data/.gitignore
ADDED
data/LICENSE
ADDED
@@ -0,0 +1,19 @@
|
|
1
|
+
Copyright © 2009 Martin Aumont (mynyml)
|
2
|
+
|
3
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy of
|
4
|
+
this software and associated documentation files (the "Software"), to deal in
|
5
|
+
the Software without restriction, including without limitation the rights to
|
6
|
+
use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies
|
7
|
+
of the Software, and to permit persons to whom the Software is furnished to do
|
8
|
+
so, subject to the following conditions:
|
9
|
+
|
10
|
+
The above copyright notice and this permission notice shall be included in all
|
11
|
+
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 THE
|
19
|
+
SOFTWARE.
|
data/Manifest
ADDED
data/README
ADDED
@@ -0,0 +1,67 @@
|
|
1
|
+
===== Summary
|
2
|
+
|
3
|
+
Rack middleware to specify an app's supported media types.
|
4
|
+
Returns '406 Not Acceptable' status when unsuported type is requested.
|
5
|
+
|
6
|
+
===== Install
|
7
|
+
|
8
|
+
gem install rack-supported-media-types
|
9
|
+
|
10
|
+
===== Example
|
11
|
+
|
12
|
+
require 'rack'
|
13
|
+
require 'rack/supported_media_types'
|
14
|
+
|
15
|
+
use Rack::SupportedMediaTypes, %w( application/xml application/json )
|
16
|
+
run App.new
|
17
|
+
|
18
|
+
# will give you:
|
19
|
+
|
20
|
+
GET /foo
|
21
|
+
Accept: text/html,text/plain
|
22
|
+
#=> 406 Not Acceptable
|
23
|
+
|
24
|
+
GET /foo
|
25
|
+
Accept: application/xml,text/html
|
26
|
+
#=> 200 OK
|
27
|
+
|
28
|
+
GET /foo
|
29
|
+
Accept: application/json,text/html
|
30
|
+
#=> 200 OK
|
31
|
+
|
32
|
+
see examples/simple.ru for example code.
|
33
|
+
|
34
|
+
===== Tip
|
35
|
+
|
36
|
+
To read the requested type from the url's extension instead of the Accept
|
37
|
+
header (which is a more realistic use case), use Rack::AbstractFormat before
|
38
|
+
Rack::SupportedMediaTypes.
|
39
|
+
|
40
|
+
# gem install rack-abstract-format
|
41
|
+
|
42
|
+
require 'rack'
|
43
|
+
require 'rack/abstract_format'
|
44
|
+
require 'rack/supported_media_types'
|
45
|
+
|
46
|
+
use Rack::AbstractFormat
|
47
|
+
use Rack::SupportedMediaTypes, %w( application/xml application/json )
|
48
|
+
run App.new
|
49
|
+
|
50
|
+
# will result in:
|
51
|
+
|
52
|
+
GET /foo.html
|
53
|
+
#=> 406 Not Acceptable
|
54
|
+
|
55
|
+
GET /foo.xml
|
56
|
+
#=> 200 OK
|
57
|
+
|
58
|
+
GET /foo.json
|
59
|
+
#=> 200 OK
|
60
|
+
|
61
|
+
see examples/recommended.ru for example code.
|
62
|
+
|
63
|
+
===== Links
|
64
|
+
|
65
|
+
code:: http://github.com/mynyml/rack-supported-media-types
|
66
|
+
docs:: http://docs.github.com/mynyml/rack-supported-media-types
|
67
|
+
|
data/Rakefile
ADDED
@@ -0,0 +1,26 @@
|
|
1
|
+
def gem_opt
|
2
|
+
defined?(Gem) ? "-rubygems" : ""
|
3
|
+
end
|
4
|
+
|
5
|
+
# --------------------------------------------------
|
6
|
+
# Tests
|
7
|
+
# --------------------------------------------------
|
8
|
+
task(:default => :test)
|
9
|
+
|
10
|
+
desc "Run tests"
|
11
|
+
task(:test) do
|
12
|
+
exit system("ruby #{gem_opt} test/test_supported_media_types.rb")
|
13
|
+
end
|
14
|
+
|
15
|
+
# --------------------------------------------------
|
16
|
+
# Docs
|
17
|
+
# --------------------------------------------------
|
18
|
+
desc "Generate YARD Documentation"
|
19
|
+
task(:yardoc) do
|
20
|
+
require 'yard'
|
21
|
+
files = %w( lib/**/*.rb )
|
22
|
+
options = %w( -o doc/yard --readme README --files LICENSE )
|
23
|
+
YARD::CLI::Yardoc.run *(options + files)
|
24
|
+
end
|
25
|
+
|
26
|
+
|
@@ -0,0 +1,40 @@
|
|
1
|
+
# install required dependency:
|
2
|
+
# $ gem install rack-abstract-format
|
3
|
+
#
|
4
|
+
# run with:
|
5
|
+
# $ rackup examples/recommended.ru -p 3000
|
6
|
+
#
|
7
|
+
# and point your web browser to:
|
8
|
+
# localhost:3000/foo.html
|
9
|
+
#
|
10
|
+
require 'pathname'
|
11
|
+
require 'rack'
|
12
|
+
require 'rack/abstract_format'
|
13
|
+
|
14
|
+
$:.unshift(Pathname(__FILE__).dirname.parent + 'lib')
|
15
|
+
require 'rack/supported_media_types'
|
16
|
+
|
17
|
+
class App
|
18
|
+
def call(env)
|
19
|
+
body = <<-BODY
|
20
|
+
<pre>
|
21
|
+
try using extensions:
|
22
|
+
|
23
|
+
- /foo.html
|
24
|
+
- /foo.xml
|
25
|
+
- /foo.json
|
26
|
+
- /foo.txt
|
27
|
+
- ...
|
28
|
+
|
29
|
+
app will only accept html and xml requests. others will return 406 (check
|
30
|
+
server's output in console to see it).
|
31
|
+
</pre>
|
32
|
+
BODY
|
33
|
+
[200, {'Content-Type' => 'text/html'}, [body]]
|
34
|
+
end
|
35
|
+
end
|
36
|
+
|
37
|
+
use Rack::AbstractFormat
|
38
|
+
use Rack::SupportedMediaTypes, %w( text/html application/xml )
|
39
|
+
run App.new
|
40
|
+
|
data/examples/simple.ru
ADDED
@@ -0,0 +1,36 @@
|
|
1
|
+
# run me with:
|
2
|
+
# $ rackup examples/simple.ru -p 3000
|
3
|
+
#
|
4
|
+
# then point your browser to:
|
5
|
+
# localhost:3000/
|
6
|
+
# localhost:3000/foo
|
7
|
+
#
|
8
|
+
# and look for the server's output in the console
|
9
|
+
# (pay attention to status codes)
|
10
|
+
#
|
11
|
+
require 'pathname'
|
12
|
+
require 'rack'
|
13
|
+
|
14
|
+
$:.unshift(Pathname(__FILE__).dirname.parent + 'lib')
|
15
|
+
require 'rack/supported_media_types'
|
16
|
+
|
17
|
+
class App
|
18
|
+
def call(env)
|
19
|
+
[200, {'Content-Type' => 'text/html'}, ['now try <strong>/foo</strong> (will return 406)']]
|
20
|
+
end
|
21
|
+
end
|
22
|
+
|
23
|
+
# given that the request comes from a web browser, and that it's Accept
|
24
|
+
# header's highest value is text/html...
|
25
|
+
|
26
|
+
map '/' do
|
27
|
+
# ... this will allow it through to the app
|
28
|
+
use Rack::SupportedMediaTypes, %w( text/html application/xml )
|
29
|
+
run App.new
|
30
|
+
end
|
31
|
+
|
32
|
+
map '/foo' do
|
33
|
+
# ... this will abort the request and return 406 Not Acceptable
|
34
|
+
use Rack::SupportedMediaTypes, %w( application/json )
|
35
|
+
run App.new
|
36
|
+
end
|
@@ -0,0 +1,36 @@
|
|
1
|
+
require 'rack/accept_media_types'
|
2
|
+
|
3
|
+
module Rack
|
4
|
+
class SupportedMediaTypes
|
5
|
+
|
6
|
+
def initialize(app, mime_types)
|
7
|
+
@app, @mime_types = app, mime_types
|
8
|
+
end
|
9
|
+
|
10
|
+
#--
|
11
|
+
# return any headers with 406?
|
12
|
+
def call(env)
|
13
|
+
req_type = accept(env)
|
14
|
+
!req_type.empty? && @mime_types.any? {|type| type.match(/#{req_type}/) } ?
|
15
|
+
@app.call(env) :
|
16
|
+
Rack::Response.new([], 406).finish
|
17
|
+
end
|
18
|
+
|
19
|
+
private
|
20
|
+
#--
|
21
|
+
# Client's prefered media type.
|
22
|
+
#
|
23
|
+
# NOTE
|
24
|
+
# glob patterns are replaced with regexp.
|
25
|
+
#
|
26
|
+
# */* -> .*/.*
|
27
|
+
# text/* -> text/.*
|
28
|
+
#
|
29
|
+
# NOTE
|
30
|
+
# Rack::AcceptMediaTypes.prefered is */* if Accept header is nil
|
31
|
+
#
|
32
|
+
def accept(env)
|
33
|
+
Rack::AcceptMediaTypes.new(env['HTTP_ACCEPT']).prefered.to_s.gsub(/\*/, '.*')
|
34
|
+
end
|
35
|
+
end
|
36
|
+
end
|
@@ -0,0 +1,18 @@
|
|
1
|
+
Gem::Specification.new do |s|
|
2
|
+
s.name = 'rack-supported-media-types'
|
3
|
+
s.version = "0.9.4"
|
4
|
+
s.summary = "Rack middleware to specify an app's supported media types."
|
5
|
+
s.description = "Rack middleware to specify an app's supported media types. Returns '406 Not Acceptable' status when unsuported type is requested."
|
6
|
+
s.author = "mynyml"
|
7
|
+
s.email = "mynyml@gmail.com"
|
8
|
+
s.homepage = "http://github.com/mynyml/rack-supported-media-types"
|
9
|
+
s.rubyforge_project = "rack-supported-media-types"
|
10
|
+
s.has_rdoc = false
|
11
|
+
s.require_path = "lib"
|
12
|
+
s.files = File.read("Manifest").strip.split("\n")
|
13
|
+
|
14
|
+
s.add_dependency 'rack-accept-media-types', '>= 0.6'
|
15
|
+
s.add_development_dependency 'rack'
|
16
|
+
s.add_development_dependency 'nanotest'
|
17
|
+
end
|
18
|
+
|
data/test/test_helper.rb
ADDED
@@ -0,0 +1,17 @@
|
|
1
|
+
require 'pathname'
|
2
|
+
|
3
|
+
require 'rack'
|
4
|
+
require 'nanotest'
|
5
|
+
begin
|
6
|
+
require 'ruby-debug'
|
7
|
+
require 'redgreen'
|
8
|
+
require 'nanotest/stats'
|
9
|
+
require 'nanotest/focus'
|
10
|
+
rescue LoadError, RuntimeError
|
11
|
+
end
|
12
|
+
|
13
|
+
$:.unshift Pathname(__FILE__).dirname.parent + 'lib'
|
14
|
+
require 'rack/supported_media_types'
|
15
|
+
|
16
|
+
include Nanotest
|
17
|
+
|
@@ -0,0 +1,53 @@
|
|
1
|
+
require 'test/test_helper'
|
2
|
+
|
3
|
+
App = lambda {|env| [200, {'Content-Type' => 'text/html'}, ['content']] }
|
4
|
+
SMT = Rack::SupportedMediaTypes
|
5
|
+
|
6
|
+
|
7
|
+
# test: returns 406 Not Acceptable
|
8
|
+
app = SMT.new(App, %w( application/xml application/json ))
|
9
|
+
response = Rack::MockRequest.new(app).get('/', 'HTTP_ACCEPT' => 'text/html')
|
10
|
+
|
11
|
+
assert { response.status == 406 }
|
12
|
+
assert { response.body.empty? }
|
13
|
+
|
14
|
+
|
15
|
+
# test: lets request through when media type is supported
|
16
|
+
app = SMT.new(App, %w( text/html application/xml ))
|
17
|
+
response = Rack::MockRequest.new(app).get('/', 'HTTP_ACCEPT' => 'text/html')
|
18
|
+
|
19
|
+
assert { response.status == 200 }
|
20
|
+
|
21
|
+
|
22
|
+
# test: requested type is assumed to be highest ranking type in Accept header's list
|
23
|
+
app = SMT.new(App, %w( text/html ))
|
24
|
+
client = Rack::MockRequest.new(app)
|
25
|
+
|
26
|
+
response = client.get('/', 'HTTP_ACCEPT' => 'text/html,application/xml')
|
27
|
+
assert { response.status == 200 }
|
28
|
+
|
29
|
+
response = client.get('/', 'HTTP_ACCEPT' => 'text/html;q=0.8,application/xml;q=0.9')
|
30
|
+
assert { response.status == 406 }
|
31
|
+
|
32
|
+
|
33
|
+
# test: matches wildcard media-range subtypes
|
34
|
+
app = SMT.new(App, %w( text/html application/json ))
|
35
|
+
|
36
|
+
begin
|
37
|
+
response = Rack::MockRequest.new(app).get('/', 'HTTP_ACCEPT' => 'text/*')
|
38
|
+
assert { response.status == 200 }
|
39
|
+
rescue Exception => e
|
40
|
+
assert("Expected not to raise error, got:\n#{e.message}") { false }
|
41
|
+
end
|
42
|
+
|
43
|
+
|
44
|
+
# test: empty Accept header
|
45
|
+
app = SMT.new(App, %w( application/xml application/json ))
|
46
|
+
|
47
|
+
begin
|
48
|
+
response = Rack::MockRequest.new(app).get('/', 'HTTP_ACCEPT' => '')
|
49
|
+
assert { response.status == 406 }
|
50
|
+
rescue Exception => e
|
51
|
+
assert("Expected not to raise error, got:\n#{e.message}") { false }
|
52
|
+
end
|
53
|
+
|
metadata
ADDED
@@ -0,0 +1,94 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: rack-supported-media-types
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.9.4
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- mynyml
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
|
12
|
+
date: 2010-01-06 00:00:00 -05:00
|
13
|
+
default_executable:
|
14
|
+
dependencies:
|
15
|
+
- !ruby/object:Gem::Dependency
|
16
|
+
name: 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:
|
25
|
+
- !ruby/object:Gem::Dependency
|
26
|
+
name: rack
|
27
|
+
type: :development
|
28
|
+
version_requirement:
|
29
|
+
version_requirements: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - ">="
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: "0"
|
34
|
+
version:
|
35
|
+
- !ruby/object:Gem::Dependency
|
36
|
+
name: nanotest
|
37
|
+
type: :development
|
38
|
+
version_requirement:
|
39
|
+
version_requirements: !ruby/object:Gem::Requirement
|
40
|
+
requirements:
|
41
|
+
- - ">="
|
42
|
+
- !ruby/object:Gem::Version
|
43
|
+
version: "0"
|
44
|
+
version:
|
45
|
+
description: Rack middleware to specify an app's supported media types. Returns '406 Not Acceptable' status when unsuported type is requested.
|
46
|
+
email: mynyml@gmail.com
|
47
|
+
executables: []
|
48
|
+
|
49
|
+
extensions: []
|
50
|
+
|
51
|
+
extra_rdoc_files: []
|
52
|
+
|
53
|
+
files:
|
54
|
+
- .gitignore
|
55
|
+
- LICENSE
|
56
|
+
- Manifest
|
57
|
+
- README
|
58
|
+
- Rakefile
|
59
|
+
- examples/recommended.ru
|
60
|
+
- examples/simple.ru
|
61
|
+
- lib/rack/supported_media_types.rb
|
62
|
+
- rack-supported-media-types.gemspec
|
63
|
+
- test/test_helper.rb
|
64
|
+
- test/test_supported_media_types.rb
|
65
|
+
has_rdoc: false
|
66
|
+
homepage: http://github.com/mynyml/rack-supported-media-types
|
67
|
+
licenses: []
|
68
|
+
|
69
|
+
post_install_message:
|
70
|
+
rdoc_options: []
|
71
|
+
|
72
|
+
require_paths:
|
73
|
+
- lib
|
74
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
75
|
+
requirements:
|
76
|
+
- - ">="
|
77
|
+
- !ruby/object:Gem::Version
|
78
|
+
version: "0"
|
79
|
+
version:
|
80
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
81
|
+
requirements:
|
82
|
+
- - ">="
|
83
|
+
- !ruby/object:Gem::Version
|
84
|
+
version: "0"
|
85
|
+
version:
|
86
|
+
requirements: []
|
87
|
+
|
88
|
+
rubyforge_project: rack-supported-media-types
|
89
|
+
rubygems_version: 1.3.5
|
90
|
+
signing_key:
|
91
|
+
specification_version: 3
|
92
|
+
summary: Rack middleware to specify an app's supported media types.
|
93
|
+
test_files: []
|
94
|
+
|