Class: Utopia::Responder

Inherits:
Object
  • Object
show all
Defined in:
lib/utopia/responder.rb

Direct Known Subclasses

Controller::Respond::Responder

Defined Under Namespace

Classes: Handler, Responds

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeResponder

Returns a new instance of Responder.



27
28
29
# File 'lib/utopia/responder.rb', line 27

def initialize
	@handlers = HTTP::Accept::MediaTypes::Map.new
end

Instance Attribute Details

#handlersObject (readonly)

Returns the value of attribute handlers.



31
32
33
# File 'lib/utopia/responder.rb', line 31

def handlers
  @handlers
end

Instance Method Details

#call(context, request, *arguments, **options) ⇒ Object



39
40
41
42
43
44
45
46
47
48
# File 'lib/utopia/responder.rb', line 39

def call(context, request, *arguments, **options)
	# Parse the list of browser preferred content types and return ordered by priority:
	media_types = HTTP::Accept::MediaTypes.browser_preferred_media_types(request.env)
	
	handler, media_range = @handlers.for(media_types)
	
	if handler
		handler.call(context, request, media_range, *arguments, **options)
	end
end

#freezeObject



33
34
35
36
37
# File 'lib/utopia/responder.rb', line 33

def freeze
	@handlers.freeze
	
	super
end

#handle(content_type, &block) ⇒ Object

Add a converter for the specified content type. Call the block with the response content if the request accepts the specified content_type.



51
52
53
# File 'lib/utopia/responder.rb', line 51

def handle(content_type, &block)
	@handlers << Handler.new(content_type, block)
end

#respond_to(context, request) ⇒ Object



55
56
57
# File 'lib/utopia/responder.rb', line 55

def respond_to(context, request)
	Responds.new(self, context, request)
end