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)
- - (Object) get_content_type(env)
-
- (Render) initialize(app, types = nil)
constructor
A new instance of Render.
- - (Object) post_process(env, status, headers, body)
Methods included from AsyncMiddleware
Constructor Details
- (Render) initialize(app, types = nil)
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
- (Object) get_content_type(env)
43 44 45 46 47 48 49 50 51 52 53 54 |
# File 'lib/goliath/rack/render.rb', line 43 def get_content_type(env) fmt = env.params['format'] fmt = fmt.last if fmt.is_a?(Array) type = if fmt.nil? || fmt =~ /^\s*$/ ::Rack::RespondTo.selected_media_type else ::Rack::RespondTo::MediaType(fmt) end "#{type}; charset=utf-8" end |
- (Object) post_process(env, status, headers, body)
21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 |
# 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| format.json { body } format.html { body } format.xml { body } format.rss { body } format.js { body } format.yaml { body } end extra = { 'Content-Type' => get_content_type(env), 'Server' => 'PostRank Goliath API Server', 'Vary' => [headers.delete('Vary'), 'Accept'].compact.join(',') } [status, extra.merge(headers), body] end |