rack-conneg 0.1.3 → 0.1.4
Sign up to get free protection for your applications and to get access to all the features.
- data/README.rdoc +5 -2
- data/lib/rack/conneg.rb +36 -7
- metadata +2 -2
data/README.rdoc
CHANGED
@@ -17,12 +17,15 @@ Content negotiation for Rack applications, including a Rails-style respond_to bl
|
|
17
17
|
use(Rack::Conneg) { |conneg|
|
18
18
|
conneg.set :accept_all_extensions, false
|
19
19
|
conneg.set :fallback, :html
|
20
|
-
conneg.ignore('/
|
20
|
+
conneg.ignore('/stylesheets/')
|
21
|
+
conneg.ignore_contents_of(File.join(File.dirname(__FILE__),'public'))
|
21
22
|
conneg.provide([:json, :xml])
|
22
23
|
}
|
23
24
|
|
24
25
|
before do
|
25
|
-
|
26
|
+
if negotiated?
|
27
|
+
content_type negotiated_type
|
28
|
+
end
|
26
29
|
end
|
27
30
|
|
28
31
|
get '/hello' do
|
data/lib/rack/conneg.rb
CHANGED
@@ -5,7 +5,9 @@ module Rack #:nodoc:#
|
|
5
5
|
|
6
6
|
class Conneg
|
7
7
|
|
8
|
-
VERSION = '0.1.
|
8
|
+
VERSION = '0.1.4'
|
9
|
+
|
10
|
+
attr :ignores
|
9
11
|
|
10
12
|
def initialize(app)
|
11
13
|
@app = app
|
@@ -17,8 +19,9 @@ module Rack #:nodoc:#
|
|
17
19
|
@types = []
|
18
20
|
|
19
21
|
@app.class.module_eval {
|
20
|
-
def negotiated_ext ; @rack_conneg_ext
|
21
|
-
def negotiated_type ; @rack_conneg_type
|
22
|
+
def negotiated_ext ; @rack_conneg_ext ; end #:nodoc:#
|
23
|
+
def negotiated_type ; @rack_conneg_type ; end #:nodoc:#
|
24
|
+
def negotiated? ; not @rack_conneg_type.nil? ; end #:nodoc:#
|
22
25
|
def respond_to
|
23
26
|
wants = { '*/*' => Proc.new { raise TypeError, "No handler for #{@rack_conneg_type}" } }
|
24
27
|
def wants.method_missing(ext, *args, &handler)
|
@@ -40,7 +43,7 @@ module Rack #:nodoc:#
|
|
40
43
|
def call(env)
|
41
44
|
extension = nil
|
42
45
|
path_info = env['PATH_INFO']
|
43
|
-
unless
|
46
|
+
unless ignored?(path_info)
|
44
47
|
# First, check to see if there's an explicit type requested
|
45
48
|
# via the file extension
|
46
49
|
mime_type = Rack::Mime.mime_type(::File.extname(path_info),nil)
|
@@ -81,6 +84,11 @@ module Rack #:nodoc:#
|
|
81
84
|
end
|
82
85
|
@app.call(env) unless @app.nil?
|
83
86
|
end
|
87
|
+
|
88
|
+
# Determine if the given path matches any items in the ignore list.
|
89
|
+
def ignored?(path_info)
|
90
|
+
@ignores.find { |ignore| ignore.match(path_info) } ? true : false
|
91
|
+
end
|
84
92
|
|
85
93
|
# Should content negotiation accept any file extention passed as part of the URI path,
|
86
94
|
# even if it's not one of the registered provided types?
|
@@ -97,10 +105,30 @@ module Rack #:nodoc:#
|
|
97
105
|
# Specifies a route prefix or Regexp that should be ignored by the content negotiator. Use
|
98
106
|
# for static files or any other route that should be passed through unaltered.
|
99
107
|
def ignore(route)
|
100
|
-
route_re = route.kind_of?(Regexp) ? route : %r{^#{route}}
|
108
|
+
route_re = route.kind_of?(Regexp) ? route : %r{^#{Regexp.escape(route)}}
|
101
109
|
@ignores << route_re
|
102
110
|
end
|
103
111
|
|
112
|
+
# Specifies a directory whose contents should be considered static and therefore ignored.
|
113
|
+
# Use with caution, since it acts recursively and can therefore build a pretty big
|
114
|
+
# ignore list, slowing down each request. It's smart enough not to add anything that's
|
115
|
+
# already been ignored, so if you ignore('/javascripts/') before you ignore_contents_of('public'),
|
116
|
+
# public/javascripts/* won't be added. In short, do all of your general ignore()ing before
|
117
|
+
# you ignore_contents_of().
|
118
|
+
def ignore_contents_of(path, prefix = '')
|
119
|
+
dir = Dir.open(path)
|
120
|
+
dir.select { |f| f !~ /^\.\.?$/ }.each { |entry|
|
121
|
+
entry_path = "#{prefix}/#{entry}"
|
122
|
+
unless ignored?(entry_path)
|
123
|
+
if ::File.directory?(::File.join(dir.path,entry))
|
124
|
+
ignore_contents_of(::File.join(dir.path,entry),entry_path)
|
125
|
+
else
|
126
|
+
ignore(%r{^#{Regexp.escape(entry_path)}$})
|
127
|
+
end
|
128
|
+
end
|
129
|
+
}
|
130
|
+
end
|
131
|
+
|
104
132
|
# Register one or more content types that the application offers. Can be a content type string,
|
105
133
|
# a file extension, or a symbol (e.g., 'application/xml', '.xml', and :xml are all equivalent).
|
106
134
|
def provide(*args)
|
@@ -142,8 +170,9 @@ module Rack #:nodoc:#
|
|
142
170
|
end
|
143
171
|
|
144
172
|
class Request
|
145
|
-
def negotiated_ext ; @env['rack.conneg.ext']
|
146
|
-
def
|
173
|
+
def negotiated_ext ; @env['rack.conneg.ext'] ; end
|
174
|
+
def negotiated? ; not @env['rack.conneg.type'].nil? ; end
|
175
|
+
def negotiated_type ; @env['rack.conneg.type'] ; end
|
147
176
|
end
|
148
177
|
|
149
178
|
end
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: rack-conneg
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.1.
|
4
|
+
version: 0.1.4
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Michael B. Klein
|
@@ -9,7 +9,7 @@ autorequire:
|
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
11
|
|
12
|
-
date: 2010-03-
|
12
|
+
date: 2010-03-11 00:00:00 -08:00
|
13
13
|
default_executable:
|
14
14
|
dependencies:
|
15
15
|
- !ruby/object:Gem::Dependency
|