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



13
14
15
# File 'lib/utopia/controller/respond.rb', line 13

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.



97
98
99
100
101
102
103
104
105
106
107
108
# File 'lib/utopia/controller/respond.rb', line 97

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



77
78
79
# File 'lib/utopia/controller/respond.rb', line 77

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

#response_for(request, original_response) ⇒ Object



81
82
83
84
85
86
87
88
89
90
91
92
93
94
# File 'lib/utopia/controller/respond.rb', line 81

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