Class: Goliath::Rack::Render
- Inherits:
-
Object
- Object
- Goliath::Rack::Render
- Includes:
- AsyncMiddleware, Rack::RespondTo
- Defined in:
- lib/goliath/rack/render.rb
Overview
The render middleware will set the Content-Type of the response based on the provided HTTP_ACCEPT headers.
Instance Method Summary collapse
- #get_content_type(env) ⇒ Object
-
#initialize(app, types = nil) ⇒ Render
constructor
A new instance of Render.
- #post_process(env, status, headers, body) ⇒ Object
Methods included from AsyncMiddleware
#call, #final_response?, #hook_into_callback_chain
Methods included from Validator
Constructor Details
#initialize(app, types = nil) ⇒ Render
Returns a new instance of Render.
16 17 18 19 |
# File 'lib/goliath/rack/render.rb', line 16 def initialize(app, types = nil) @app = app ::Rack::RespondTo.media_types = [types].flatten if types end |
Instance Method Details
#get_content_type(env) ⇒ Object
39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 |
# File 'lib/goliath/rack/render.rb', line 39 def get_content_type(env) type = if env.respond_to? :params fmt = env.params['format'] fmt = fmt.last if fmt.is_a?(Array) 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 |
#post_process(env, status, headers, body) ⇒ Object
21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 |
# File 'lib/goliath/rack/render.rb', line 21 def post_process(env, status, headers, body) ::Rack::RespondTo.env = env # the respond_to block is what actually triggers the # setting of selected_media_type, so it's required respond_to do |format| ::Rack::RespondTo.media_types.each do |type| format.send(type, Proc.new { body }) end end extra = { 'Content-Type' => get_content_type(env), 'Vary' => [headers.delete('Vary'), 'Accept'].compact.join(',') } [status, extra.merge(headers), body] end |