module Utopia::Controller::Respond

A controller layer which provides a convenient way to respond to different requested content types. The order in which you add converters matters, as it determines how the incoming Accept: header is mapped, e.g. the first converter is also defined as matching the media range /.

Public Class Methods

prepended(base) click to toggle source
# File lib/utopia/controller/respond.rb, line 30
def self.prepended(base)
        base.extend(ClassMethods)
end

Public Instance Methods

process!(request, path) click to toggle source

Invokes super. If a response is generated, format it based on the Accept: header, unless the content type was already specified.

Calls superclass method
# File lib/utopia/controller/respond.rb, line 114
def process!(request, path)
        if response = super
                headers = response[1]
                
                # Don't try to convert the response if a content type was explicitly specified.
                if headers[HTTP::CONTENT_TYPE]
                        return response
                else
                        return self.response_for(request, response)
                end
        end
end
respond_to(request) click to toggle source
# File lib/utopia/controller/respond.rb, line 94
def respond_to(request)
        self.class.respond_to(self, request)
end
response_for(request, original_response) click to toggle source
# File lib/utopia/controller/respond.rb, line 98
def response_for(request, original_response)
        response = catch(:response) do
                self.class.response_for(self, request, original_response)
                
                # If the above code did not throw a new response, we return the original:
                return original_response
        end
        
        # If the user called {Base#ignore!}, it's possible response is nil:
        if response
                # There was an updated response so merge it:
                return [original_response[0], original_response[1].merge(response[1]), response[2] || original_response[2]]
        end
end