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
:accept option to filter on HTTP accept header
adamwiggins (author)
Sun Jun 08 02:17:17 -0700 2008
commit  9c413351c79f35a72f93cf74f3f516cab3bda4e5
tree    4814239e11aeea44a02f6d0acb0f028b0c25120c
parent  ea3239c88f8a85f82cc891589bc6ad1c339719e8
...
55
56
57
 
 
 
 
58
59
60
...
174
175
176
 
 
 
177
178
179
...
55
56
57
58
59
60
61
62
63
64
...
178
179
180
181
182
183
184
185
186
0
@@ -55,6 +55,10 @@ module Rack #:nodoc:
0
       @env['HTTP_USER_AGENT']
0
     end
0
 
0
+    def accept
0
+      @env['HTTP_ACCEPT'].split(',').map { |a| a.strip }
0
+    end
0
+
0
     private
0
 
0
       # Return truthfully if and only if the following conditions are met: 1.) the
0
@@ -174,6 +178,9 @@ module Sinatra
0
       if host = options[:host] 
0
         return unless host === request.host
0
       end
0
+      if accept = options[:accept]
0
+        return unless request.accept.include? accept
0
+      end
0
       return unless pattern =~ request.path_info.squeeze('/')
0
       params.merge!(param_keys.zip($~.captures.map(&:from_param)).to_hash)
0
       splats = params.select { |k, v| k =~ /^_splat_\d+$/ }.sort.map(&:last)
...
191
192
193
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
194
195
196
...
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
0
@@ -191,6 +191,21 @@ context "Events in an app" do
0
 
0
   end
0
 
0
+  specify "filters by accept header" do
0
+
0
+    get '/', :accept => 'image/jpg' do
0
+      request.env['HTTP_ACCEPT']
0
+    end
0
+
0
+    get_it '/', :env => { :accept => 'image/jpg' }
0
+    should.be.ok
0
+    body.should.equal 'image/jpg'
0
+
0
+    get_it '/', :env => { :accept => 'text/html' }
0
+    should.not.be.ok
0
+
0
+  end
0
+
0
   specify "can use regex to get parts of user-agent" do
0
     
0
     get '/', :agent => /Windows (NT)/ do

Comments