public
Rubygem
Fork of bmizerany/sinatra
Description: Classy web-development dressed in a DSL
Homepage: http://sinatrarb.com
Clone URL: git://github.com/adamwiggins/sinatra.git
lookup_mime, used by content_type and accept
adamwiggins (author)
Mon Jun 09 00:11:56 -0700 2008
commit  cfcf7863a865337ea29c764db5a192bf46f50e29
tree    2969a90c1fb9f573f11ef85a504936f85d9250c5
parent  33cbfedbcce13c924e2fbb094e20f24929d38f80
...
179
180
181
182
 
183
184
185
...
449
450
451
452
453
 
454
455
456
...
1403
1404
1405
 
 
 
 
 
 
1406
1407
1408
...
179
180
181
 
182
183
184
185
...
449
450
451
 
 
452
453
454
455
...
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
0
@@ -179,7 +179,7 @@ module Sinatra
0
         return unless host === request.host
0
       end
0
       if accept = options[:accept]
0
-        return unless request.accept.include? accept
0
+        return unless request.accept.include? lookup_mime(accept)
0
       end
0
       return unless pattern =~ request.path_info.squeeze('/')
0
       params.merge!(param_keys.zip($~.captures.map(&:from_param)).to_hash)
0
@@ -449,8 +449,7 @@ module Sinatra
0
     #   end
0
     #
0
     def content_type(type, params={})
0
-      type = Rack::File::MIME_TYPES[type.to_s] if type.kind_of?(Symbol)
0
-      fail "Invalid or undefined media_type: #{type}" if type.nil?
0
+      type = lookup_mime(type)
0
       if params.any?
0
         params = params.collect { |kv| "%s=%s" % kv }.join(', ')
0
         type = [ type, params ].join(";")
0
@@ -1403,6 +1402,12 @@ def mime(ext, type)
0
   Rack::File::MIME_TYPES[ext.to_s] = type
0
 end
0
 
0
+def lookup_mime(type)
0
+  type = Rack::File::MIME_TYPES[type.to_s] if type.kind_of?(Symbol)
0
+  fail "Invalid or undefined media_type: #{type}" if type.nil?
0
+  type
0
+end
0
+
0
 ### Misc Core Extensions
0
 
0
 module Kernel
...
217
218
219
220
 
221
222
223
224
 
225
226
 
227
228
229
...
217
218
219
 
220
221
222
223
 
224
225
 
226
227
228
229
0
@@ -217,13 +217,13 @@ context "Events in an app" do
0
 
0
   specify "filters by accept header" do
0
 
0
-    get '/', :accept => 'image/jpg' do
0
+    get '/', :accept => :xml do
0
       request.env['HTTP_ACCEPT']
0
     end
0
 
0
-    get_it '/', :env => { :accept => 'image/jpg' }
0
+    get_it '/', :env => { :accept => 'application/xml' }
0
     should.be.ok
0
-    body.should.equal 'image/jpg'
0
+    body.should.equal 'application/xml'
0
 
0
     get_it '/', :env => { :accept => 'text/html' }
0
     should.not.be.ok

Comments