class Responder
Negotiates response content types and invokes the matching handler.
Nested
Definitions
def initialize
Initialize an empty content-type handler map.
Implementation
def initialize
@handlers = HTTP::Accept::MediaTypes::Map.new
end
def freeze
Freeze this object and its internal state.
Signature
-
returns
self This object.
Implementation
def freeze
@handlers.freeze
super
end
def call(context, request, *arguments, **options)
Negotiate the request's accepted media types and invoke the best handler.
Signature
-
parameter
contextObject The context.
-
parameter
requestRack::Request The request.
-
parameter
argumentsArray The arguments.
-
parameter
optionsHash The options.
-
returns
Object | Nil The selected handler's result, or
nilif none matches.
Implementation
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
def handle(content_type, &block)
Add a converter for the specified content type. Call the block with the response content if the request accepts the specified content_type.
Implementation
def handle(content_type, &block)
@handlers << Handler.new(content_type, block)
end
def respond_to(context, request)
Bind this responder to a context and request.
Signature
-
parameter
contextController::Base The controller context.
-
parameter
requestRack::Request The request.
-
returns
Responds The bound responder.
Implementation
def respond_to(context, request)
Responds.new(self, context, request)
end
def with_json
Register the default JSON handler.
Signature
-
returns
HTTP::Accept::MediaTypes::Map The updated handler map.
Implementation
def with_json
@handlers << Handlers::JSON
end
def with_passthrough
Register the wildcard passthrough handler.
Signature
-
returns
HTTP::Accept::MediaTypes::Map The updated handler map.
Implementation
def with_passthrough
@handlers << Handlers::Passthrough
end
def with(content_type, &block)
Invoke the responder with the given object.
Signature
-
parameter
content_typeString The content type.
-
returns
HTTP::Accept::MediaTypes::Map The updated handler map.
Implementation
def with(content_type, &block)
handle(content_type, &block)
end