public
Fork of bmizerany/sinatra
Description: Classy web-development dressed in a DSL
Homepage: http://sinatra.rubyforge.org
Clone URL: git://github.com/sr/sinatra.git
bring back #mime and #content_type
sr (author)
Thu Aug 28 16:42:58 -0700 2008
commit  4ca3885709ba5a620e3ee24020d028c482d2b002
tree    031fb39325b7ff43b3dc5a07477d41490c7bd532
parent  fca8758a492e3767d91f87f8fc172caa15696ade
...
374
375
376
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
377
378
379
...
466
467
468
 
469
470
471
...
768
769
770
 
 
 
 
 
771
772
773
...
786
787
788
789
 
790
791
792
...
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
...
489
490
491
492
493
494
495
...
792
793
794
795
796
797
798
799
800
801
802
...
815
816
817
 
818
819
820
821
0
@@ -374,6 +374,29 @@ module Sinatra
0
     end
0
     alias :header :headers        
0
 
0
+    # Set the content type of the response body (HTTP 'Content-Type' header).
0
+    #
0
+    # The +type+ argument may be an internet media type (e.g., 'text/html',
0
+    # 'application/xml+atom', 'image/png') or a Symbol key into the
0
+    # Rack::File::MIME_TYPES table.
0
+    #
0
+    # Media type parameters, such as "charset", may also be specified using the
0
+    # optional hash argument:
0
+    #
0
+    #   get '/foo.html' do
0
+    #     content_type 'text/html', :charset => 'utf-8'
0
+    #     "<h1>Hello World</h1>"
0
+    #   end
0
+    #
0
+    def content_type(type, params={})
0
+      type = options[:mime_types][type.to_s] if type.kind_of?(Symbol)
0
+      fail "Invalid or undefined media_type: #{type}" if type.nil?
0
+      if params.any?
0
+        params = params.collect { |kv| "%s=%s" % kv }.join(', ')
0
+        type = [ type, params ].join(";")
0
+      end
0
+      response.header['Content-Type'] = type
0
+    end
0
   end
0
 
0
   ##
0
@@ -466,6 +489,7 @@ module Sinatra
0
         :public => root + '/public',
0
         :sessions => false,
0
         :logging => true,
0
+        :mime_types => Rack::File::MIME_TYPES.dup,
0
         :app_file => $0,
0
         :error_logging => true,
0
         :raise_errors => false,
0
@@ -768,6 +792,11 @@ module Sinatra
0
         opts.each { |key| set(key, false) }
0
       end
0
 
0
+      # TODO: doc
0
+      def mime(ext, type)
0
+        options.mime_types[ext.to_s] = type
0
+      end
0
+
0
       # Determine whether the application is in the process of being
0
       # reloaded.
0
       def reloading?
0
@@ -786,7 +815,7 @@ module Sinatra
0
   module DelegatingDSL
0
     
0
     FORWARDABLE_METHODS = [ :get, :post, :put, :delete, :head, :error,
0
-                            :set, :set_option, :enable, :disable,
0
+                            :set, :set_option, :enable, :disable, :mime,
0
                             :configure, :configures, :use, :helpers ]
0
     
0
     FORWARDABLE_METHODS.each do |method|
...
111
112
113
 
 
 
 
 
 
 
 
 
114
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
...
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
0
@@ -111,4 +111,60 @@ context "A Sinatra::Application when falling" do
0
   
0
 end
0
 
0
+context "A Sinatra::Application with custom Content-Type" do
0
+  before do
0
+    @app = Sinatra::Application.new do
0
+      mime :nwa, 'rap/westcoast'
0
+
0
+      get '/shortcut' do
0
+        content_type :xml
0
+        '<xml />'
0
+      end
0
 
0
+      get '/full' do
0
+        content_type 'text/foo'
0
+        'hey'
0
+      end
0
+
0
+      get '/with_params' do
0
+        content_type 'foo/bar', :bla => 'a', :nas => 'awesome'
0
+        "shoot 'em up"
0
+      end
0
+
0
+      get '/a_classic' do
0
+        content_type :nwa
0
+        "one less bitch"
0
+      end
0
+    end
0
+
0
+    @request = Rack::MockRequest.new(@app)
0
+  end
0
+
0
+  specify "Content-Type should be easily settable using shortcut" do
0
+    @response = @request.get('/shortcut')
0
+    @response.should.be.ok
0
+    @response.headers['Content-Type'].should.equal 'text/xml'
0
+    @response.body.should.equal '<xml />'
0
+  end
0
+
0
+  specify "Content-Type should be settable using full mime-type" do
0
+    @response = @request.get('/full')
0
+    @response.should.be.ok
0
+    @response.headers['Content-Type'].should.equal 'text/foo'
0
+    @response.body.should.equal 'hey'
0
+  end
0
+
0
+  specify "#content_type should append any other param" do
0
+    @response = @request.get('/with_params')
0
+    @response.should.be.ok
0
+    @response.headers['Content-Type'].should.equal 'foo/bar;bla=a, nas=awesome'
0
+    @response.body.should.equal "shoot 'em up"
0
+  end
0
+
0
+  specify "can create its own shortcut" do
0
+    @response = @request.get('/a_classic')
0
+    @response.should.be.ok
0
+    @response.headers['Content-Type'].should.equal 'rap/westcoast'
0
+    @response.body.should.equal "one less bitch"
0
+  end
0
+end

Comments