Permalink
Browse files

make Render middleware take explicit formats, instead of requiring extra

middleware requires later for each format
  • Loading branch information...
1 parent 2309ca2 commit ae6ee39476afe70d4a24b2ae83ea8a589758d266 @igrigorik igrigorik committed Jun 23, 2011
@@ -21,8 +21,7 @@ class User < ActiveRecord::Base
class Srv < Goliath::API
use Goliath::Rack::Params
use Goliath::Rack::DefaultMimeType
- use Goliath::Rack::Formatters::JSON
- use Goliath::Rack::Render
+ use Goliath::Rack::Render, 'json'
use Goliath::Rack::Validation::RequiredParam, {:key => 'id', :type => 'ID'}
use Goliath::Rack::Validation::NumericRange, {:key => 'id', :min => 1}
View
@@ -7,8 +7,7 @@
class AsyncUpload < Goliath::API
use Goliath::Rack::Params # parse & merge query and body parameters
use Goliath::Rack::DefaultMimeType # cleanup accepted media types
- use Goliath::Rack::Formatters::JSON # JSON output formatter
- use Goliath::Rack::Render # auto-negotiate response format
+ use Goliath::Rack::Render, 'json' # auto-negotiate response format
def on_headers(env, headers)
env.logger.info 'received headers: ' + headers.inspect
View
@@ -11,9 +11,7 @@
class ConfTest < Goliath::API
use Goliath::Rack::Params
- use Goliath::Rack::DefaultMimeType
- use Goliath::Rack::Formatters::JSON
- use Goliath::Rack::Render
+ use Goliath::Rack::Render, 'json'
def options_parser(opts, options)
options[:test] = 0
@@ -12,10 +12,9 @@
# rabbitmqctl set_permissions -p /test test ".*" ".*" ".*"
class ContentStream < Goliath::API
- use Goliath::Rack::Formatters::JSON
use Goliath::Rack::Params
- use Goliath::Rack::Render
+ use Goliath::Rack::Render, 'json'
use Goliath::Rack::Heartbeat
use Goliath::Rack::Validation::RequestMethod, %w(GET)
View
@@ -11,8 +11,7 @@
class Echo < Goliath::API
use Goliath::Rack::Tracer # log trace statistics
use Goliath::Rack::DefaultMimeType # cleanup accepted media types
- use Goliath::Rack::Formatters::JSON # JSON output formatter
- use Goliath::Rack::Render # auto-negotiate response format
+ use Goliath::Rack::Render, 'json' # auto-negotiate response format
use Goliath::Rack::Params # parse & merge query and body parameters
use Goliath::Rack::Heartbeat # respond to /status with 200, OK (monitoring, etc)
View
@@ -27,8 +27,7 @@ class Gziped < Goliath::API
end
use Goliath::Rack::Params # parse & merge query and body parameters
- use Goliath::Rack::Formatters::JSON # JSON output formatter
- use Goliath::Rack::Render # auto-negotiate response format
+ use Goliath::Rack::Render, 'json' # auto-negotiate response format
use Goliath::Rack::Validation::RequestMethod, %w(GET) # allow GET requests only
use Goliath::Rack::Validation::RequiredParam, {:key => 'echo'} # must provide ?echo= query or body param
View
@@ -98,8 +98,7 @@ class TestRig < Goliath::API
use Goliath::Rack::Params # parse & merge query and body parameters
#
use Goliath::Rack::DefaultMimeType # cleanup accepted media types
- use Goliath::Rack::Formatters::JSON # JSON output formatter
- use Goliath::Rack::Render # auto-negotiate response format
+ use Goliath::Rack::Render, 'json' # auto-negotiate response format
#
use ExceptionHandler # turn raised errors into HTTP responses
use MiddlewareFailure # make response fail if 'fail' param
View
@@ -57,6 +57,14 @@ def middlewares
# @param block A block to pass to the middleware
def use(name, *args, &block)
@middlewares ||= []
+
+ if name == Goliath::Rack::Render
+ [args].flatten.each do |type|
+ type = Goliath::Rack::Formatters.const_get type.upcase
+ @middlewares << [type, nil, nil]
+ end
+ end
+
@middlewares << [name, args, block]
end
View
@@ -25,12 +25,9 @@ def post_process(env, status, headers, body)
# setting of selected_media_type, so it's required
respond_to do |format|
- format.json { body }
- format.html { body }
- format.xml { body }
- format.rss { body }
- format.js { body }
- format.yaml { body }
+ ::Rack::RespondTo.media_types.each do |type|
+ format.send(type, Proc.new { body })
+ end
end
extra = { 'Content-Type' => get_content_type(env),
@@ -45,13 +42,14 @@ def get_content_type(env)
fmt = env.params['format']
fmt = fmt.last if fmt.is_a?(Array)
- if fmt.nil? || fmt =~ /^\s*$/
- ::Rack::RespondTo.selected_media_type
- else
+ if !fmt.nil? && fmt !~ /^\s*$/
::Rack::RespondTo::MediaType(fmt)
end
end
+ type = ::Rack::RespondTo.env['HTTP_ACCEPT'] if type.nil?
+ type = ::Rack::RespondTo.selected_media_type if type == '*/*'
+
"#{type}; charset=utf-8"
end
end

0 comments on commit ae6ee39

Please sign in to comment.