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.



44
45
46
# File 'lib/utopia/responder.rb', line 44

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

Instance Attribute Details

#handlersObject (readonly)

Returns the value of attribute handlers.



48
49
50
# File 'lib/utopia/responder.rb', line 48

def handlers
  @handlers
end

Instance Method Details

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



56
57
58
59
60
61
62
63
64
65
# File 'lib/utopia/responder.rb', line 56

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



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

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.



68
69
70
# File 'lib/utopia/responder.rb', line 68

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

#respond_to(context, request) ⇒ Object



72
73
74
# File 'lib/utopia/responder.rb', line 72

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