Module: Utopia::Controller::Respond

Defined in:
lib/utopia/controller/respond.rb

Overview

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 /.

Defined Under Namespace

Modules: ClassMethods, Handlers Classes: Responder

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.prepended(base) ⇒ Object



30
31
32
# File 'lib/utopia/controller/respond.rb', line 30

def self.prepended(base)
	base.extend(ClassMethods)
end

Instance Method Details

#process!(request, path) ⇒ Object

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



114
115
116
117
118
119
120
121
122
123
124
125
# 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) ⇒ Object



94
95
96
# File 'lib/utopia/controller/respond.rb', line 94

def respond_to(request)
	self.class.respond_to(self, request)
end

#response_for(request, original_response) ⇒ Object



98
99
100
101
102
103
104
105
106
107
108
109
110
111
# 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