rack-rack-contrib 0.9.1 → 0.9.2
Sign up to get free protection for your applications and to get access to all the features.
- data/README.rdoc +3 -1
- data/lib/rack/contrib.rb +2 -0
- data/lib/rack/contrib/accept_format.rb +8 -6
- data/lib/rack/contrib/csshttprequest.rb +1 -1
- data/lib/rack/contrib/jsonp.rb +1 -1
- data/lib/rack/contrib/route_exceptions.rb +20 -19
- data/rack-contrib.gemspec +2 -2
- data/test/spec_rack_accept_format.rb +35 -4
- metadata +2 -2
data/README.rdoc
CHANGED
@@ -40,7 +40,9 @@ interface:
|
|
40
40
|
absolute URLs.
|
41
41
|
* Rack::Backstage - Returns content of specified file if it exists, which makes
|
42
42
|
it convenient for putting up maintenance pages.
|
43
|
-
* Rack::
|
43
|
+
* Rack::AcceptFormat - Adds a format extension at the end of the URI when there is none, corresponding to the mime-type given in the Accept HTTP header.
|
44
|
+
* Rack::HostMeta - Configures /host-meta using a block
|
45
|
+
* Rack::Cookies - Adds simple cookie jar hash to env
|
44
46
|
|
45
47
|
=== Use
|
46
48
|
|
data/lib/rack/contrib.rb
CHANGED
@@ -7,7 +7,9 @@ module Rack
|
|
7
7
|
end
|
8
8
|
end
|
9
9
|
|
10
|
+
autoload :AcceptFormat, "rack/contrib/accept_format"
|
10
11
|
autoload :BounceFavicon, "rack/contrib/bounce_favicon"
|
12
|
+
autoload :Cookies, "rack/contrib/cookies"
|
11
13
|
autoload :CSSHTTPRequest, "rack/contrib/csshttprequest"
|
12
14
|
autoload :Deflect, "rack/contrib/deflect"
|
13
15
|
autoload :ETag, "rack/contrib/etag"
|
@@ -24,20 +24,22 @@ module Rack
|
|
24
24
|
# MIT-License - Cyril Rohr
|
25
25
|
#
|
26
26
|
class AcceptFormat
|
27
|
-
# Constants
|
28
|
-
DEFAULT_EXTENSION = ".html"
|
29
27
|
|
30
|
-
def initialize(app)
|
28
|
+
def initialize(app, default_extention = '.html')
|
29
|
+
@ext = default_extention.to_s.strip
|
30
|
+
@ext = ".#{@ext}" unless @ext[0] == ?.
|
31
31
|
@app = app
|
32
32
|
end
|
33
33
|
|
34
34
|
def call(env)
|
35
35
|
req = Rack::Request.new(env)
|
36
|
-
|
37
|
-
|
38
|
-
|
36
|
+
|
37
|
+
if ::File.extname(req.path_info).empty?
|
38
|
+
accept = env['HTTP_ACCEPT'].to_s.scan(/[^;,\s]*\/[^;,\s]*/)[0].to_s
|
39
|
+
extension = Rack::Mime::MIME_TYPES.invert[accept] || @ext
|
39
40
|
req.path_info = req.path_info+"#{extension}"
|
40
41
|
end
|
42
|
+
|
41
43
|
@app.call(env)
|
42
44
|
end
|
43
45
|
end
|
@@ -26,7 +26,7 @@ module Rack
|
|
26
26
|
end
|
27
27
|
|
28
28
|
def encode(response, assembled_body="")
|
29
|
-
response.each { |s| assembled_body << s } # call down the stack
|
29
|
+
response.each { |s| assembled_body << s.to_s } # call down the stack
|
30
30
|
return ::CSSHTTPRequest.encode(assembled_body)
|
31
31
|
end
|
32
32
|
|
data/lib/rack/contrib/jsonp.rb
CHANGED
@@ -4,45 +4,46 @@ module Rack
|
|
4
4
|
[Exception, '/error/internal']
|
5
5
|
]
|
6
6
|
|
7
|
-
|
8
|
-
|
9
|
-
|
7
|
+
PATH_INFO = 'rack.route_exceptions.path_info'.freeze
|
8
|
+
EXCEPTION = 'rack.route_exceptions.exception'.freeze
|
9
|
+
RETURNED = 'rack.route_exceptions.returned'.freeze
|
10
|
+
|
11
|
+
class << self
|
12
|
+
def route(exception, to)
|
13
|
+
ROUTES.delete_if{|k,v| k == exception }
|
14
|
+
ROUTES << [exception, to]
|
15
|
+
end
|
16
|
+
|
17
|
+
alias []= route
|
18
|
+
end
|
10
19
|
|
11
20
|
def initialize(app)
|
12
21
|
@app = app
|
13
22
|
end
|
14
23
|
|
15
24
|
def call(env, try_again = true)
|
16
|
-
|
17
|
-
|
18
|
-
response
|
25
|
+
returned = @app.call(env)
|
19
26
|
rescue Exception => exception
|
20
27
|
raise(exception) unless try_again
|
21
28
|
|
22
29
|
ROUTES.each do |klass, to|
|
23
30
|
next unless klass === exception
|
24
|
-
return route(to, env,
|
31
|
+
return route(to, env, returned, exception)
|
25
32
|
end
|
26
33
|
|
27
34
|
raise(exception)
|
28
35
|
end
|
29
36
|
|
30
|
-
def route(to, env,
|
31
|
-
|
32
|
-
|
33
|
-
|
34
|
-
|
35
|
-
|
36
|
-
env.merge!(hash)
|
37
|
+
def route(to, env, returned, exception)
|
38
|
+
env.merge!(
|
39
|
+
PATH_INFO => env['PATH_INFO'],
|
40
|
+
EXCEPTION => exception,
|
41
|
+
RETURNED => returned
|
42
|
+
)
|
37
43
|
|
38
44
|
env['PATH_INFO'] = to
|
39
45
|
|
40
46
|
call(env, try_again = false)
|
41
47
|
end
|
42
|
-
|
43
|
-
def self.route(exception, to)
|
44
|
-
ROUTES.delete_if{|k,v| k == exception }
|
45
|
-
ROUTES << [exception, to]
|
46
|
-
end
|
47
48
|
end
|
48
49
|
end
|
data/rack-contrib.gemspec
CHANGED
@@ -3,7 +3,7 @@ Gem::Specification.new do |s|
|
|
3
3
|
s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
|
4
4
|
|
5
5
|
s.name = 'rack-contrib'
|
6
|
-
s.version = '0.9.
|
6
|
+
s.version = '0.9.2'
|
7
7
|
s.date = '2009-03-07'
|
8
8
|
|
9
9
|
s.description = "Contributed Rack Middleware and Utilities"
|
@@ -74,7 +74,7 @@ Gem::Specification.new do |s|
|
|
74
74
|
s.test_files = s.files.select {|path| path =~ /^test\/spec_.*\.rb/}
|
75
75
|
|
76
76
|
s.extra_rdoc_files = %w[README.rdoc COPYING]
|
77
|
-
s.add_dependency 'rack', '
|
77
|
+
s.add_dependency 'rack', '>= 0.9.1'
|
78
78
|
s.add_dependency 'test-spec', '~> 0.9.0'
|
79
79
|
s.add_development_dependency 'tmail', '>= 1.2'
|
80
80
|
s.add_development_dependency 'json', '>= 1.1'
|
@@ -4,16 +4,41 @@ require 'rack/contrib/accept_format'
|
|
4
4
|
require 'rack/mime'
|
5
5
|
|
6
6
|
context "Rack::AcceptFormat" do
|
7
|
+
app = lambda { |env| [200, {'Content-Type' => 'text/plain'}, env['PATH_INFO']] }
|
8
|
+
|
7
9
|
specify "should do nothing when a format extension is already provided" do
|
8
|
-
app = lambda { |env| [200, {'Content-Type' => 'text/plain'}, env['PATH_INFO']] }
|
9
10
|
request = Rack::MockRequest.env_for("/resource.json")
|
10
11
|
body = Rack::AcceptFormat.new(app).call(request).last
|
11
12
|
body.should == "/resource.json"
|
12
13
|
end
|
13
14
|
|
15
|
+
context "default extention" do
|
16
|
+
specify "should allow custom default" do
|
17
|
+
request = Rack::MockRequest.env_for("/resource")
|
18
|
+
body = Rack::AcceptFormat.new(app, '.xml').call(request).last
|
19
|
+
body.should == "/resource.xml"
|
20
|
+
end
|
21
|
+
|
22
|
+
specify "should default to html" do
|
23
|
+
request = Rack::MockRequest.env_for("/resource")
|
24
|
+
body = Rack::AcceptFormat.new(app).call(request).last
|
25
|
+
body.should == "/resource.html"
|
26
|
+
end
|
27
|
+
|
28
|
+
specify "should notmalize custom extention" do
|
29
|
+
request = Rack::MockRequest.env_for("/resource")
|
30
|
+
|
31
|
+
body = Rack::AcceptFormat.new(app,'xml').call(request).last #no dot prefix
|
32
|
+
body.should == "/resource.xml"
|
33
|
+
|
34
|
+
body = Rack::AcceptFormat.new(app, :xml).call(request).last
|
35
|
+
body.should == "/resource.xml"
|
36
|
+
end
|
37
|
+
end
|
38
|
+
|
14
39
|
context "there is no format extension" do
|
15
40
|
Rack::Mime::MIME_TYPES.clear
|
16
|
-
|
41
|
+
|
17
42
|
def mime(ext, type)
|
18
43
|
ext = ".#{ext}" unless ext.to_s[0] == ?.
|
19
44
|
Rack::Mime::MIME_TYPES[ext.to_s] = type
|
@@ -22,13 +47,13 @@ context "Rack::AcceptFormat" do
|
|
22
47
|
specify "should add the default extension if no Accept header" do
|
23
48
|
request = Rack::MockRequest.env_for("/resource")
|
24
49
|
body = Rack::AcceptFormat.new(app).call(request).last
|
25
|
-
body.should == "/resource
|
50
|
+
body.should == "/resource.html"
|
26
51
|
end
|
27
52
|
|
28
53
|
specify "should add the default extension if the Accept header is not registered in the Mime::Types" do
|
29
54
|
request = Rack::MockRequest.env_for("/resource", 'HTTP_ACCEPT' => 'application/json;q=1.0, text/html;q=0.8, */*;q=0.1')
|
30
55
|
body = Rack::AcceptFormat.new(app).call(request).last
|
31
|
-
body.should == "/resource
|
56
|
+
body.should == "/resource.html"
|
32
57
|
end
|
33
58
|
|
34
59
|
specify "should add the correct extension if the Accept header is registered in the Mime::Types" do
|
@@ -38,4 +63,10 @@ context "Rack::AcceptFormat" do
|
|
38
63
|
body.should == "/resource.json"
|
39
64
|
end
|
40
65
|
end
|
66
|
+
|
67
|
+
specify "shouldn't confuse extention when there are dots in path" do
|
68
|
+
request = Rack::MockRequest.env_for("/parent.resource/resource")
|
69
|
+
body = Rack::AcceptFormat.new(app, '.html').call(request).last
|
70
|
+
body.should == "/parent.resource/resource.html"
|
71
|
+
end
|
41
72
|
end
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: rack-rack-contrib
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.9.
|
4
|
+
version: 0.9.2
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- rack-devel
|
@@ -18,7 +18,7 @@ dependencies:
|
|
18
18
|
version_requirement:
|
19
19
|
version_requirements: !ruby/object:Gem::Requirement
|
20
20
|
requirements:
|
21
|
-
- -
|
21
|
+
- - ">="
|
22
22
|
- !ruby/object:Gem::Version
|
23
23
|
version: 0.9.1
|
24
24
|
version:
|