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

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.prepended(base) ⇒ Object

Extend a controller class with response negotiation.



17
18
19
# File 'lib/utopia/controller/respond.rb', line 17

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

Instance Method Details

#process!(request, path) ⇒ Object

Serialize semantic controller results, while passing complete protocol responses through unchanged.



59
60
61
62
63
64
65
66
67
68
# File 'lib/utopia/controller/respond.rb', line 59

def process!(request, path)
	result = super
	
	case result
	when Result
		return self.response_for(request, result)
	else
		return result
	end
end

#response_for(request, result) ⇒ Object

Build a protocol response for a semantic controller result.

Raises:

  • (TypeError)


43
44
45
46
47
48
49
50
51
52
53
54
55
56
# File 'lib/utopia/controller/respond.rb', line 43

def response_for(request, result)
	if response = self.class.response_for(self, request, result.value)
		content_type, body = response
		headers = result.headers.dup
		
		if content_type
			headers[HTTP::CONTENT_TYPE] = content_type.to_s
		end
		
		return Utopia::Response[result.status, headers, body]
	end
	
	raise TypeError, "Could not negotiate a response for #{result.value.class}!"
end