Class: Utopia::Controller::Responder
- Inherits:
-
Object
- Object
- Utopia::Controller::Responder
- Defined in:
- lib/utopia/controller/responder.rb
Overview
Negotiates response content types and invokes the matching handler.
Defined Under Namespace
Classes: Handler
Instance Attribute Summary collapse
-
#handlers ⇒ Object
readonly
Returns the value of attribute handlers.
Instance Method Summary collapse
-
#call(context, request, *arguments, **options) ⇒ Object
Negotiate the request's accepted media types and invoke the best handler.
-
#freeze ⇒ Object
Freeze this responder and compile its handler map.
-
#handle(content_type, &block) ⇒ Object
Add a serializer for the specified content type.
-
#initialize(handlers = Protocol::Media::Map.new, passthrough = nil) ⇒ Responder
constructor
Initialize a responder with a handler map.
-
#with(content_type, &block) ⇒ Object
Add a serializer for the specified content type.
-
#with_json ⇒ Object
Register the default JSON handler.
-
#with_passthrough ⇒ Object
Register the wildcard passthrough handler.
Constructor Details
#initialize(handlers = Protocol::Media::Map.new, passthrough = nil) ⇒ Responder
Initialize a responder with a handler map.
85 86 87 88 |
# File 'lib/utopia/controller/responder.rb', line 85 def initialize(handlers = Protocol::Media::Map.new, passthrough = nil) @handlers = handlers @passthrough = passthrough end |
Instance Attribute Details
#handlers ⇒ Object (readonly)
Returns the value of attribute handlers.
90 91 92 |
# File 'lib/utopia/controller/responder.rb', line 90 def handlers @handlers end |
Instance Method Details
#call(context, request, *arguments, **options) ⇒ Object
Negotiate the request's accepted media types and invoke the best handler.
139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 |
# File 'lib/utopia/controller/responder.rb', line 139 def call(context, request, *arguments, **) accept = request.headers["accept"] # An absent or empty Accept header accepts any media type: if accept.nil? || accept.empty? media_ranges = [Handlers::Passthrough::WILDCARD] else media_ranges = accept.preferred_media_ranges end if match = @handlers.for(media_ranges) handler, media_range = match elsif @passthrough handler = @passthrough media_range = media_ranges.first end if handler return handler.content_type, handler.call(context, request, media_range, *arguments, **) end return nil end |
#freeze ⇒ Object
Freeze this responder and compile its handler map.
94 95 96 97 98 99 100 |
# File 'lib/utopia/controller/responder.rb', line 94 def freeze return self if frozen? @handlers.freeze return super end |
#handle(content_type, &block) ⇒ Object
Add a serializer for the specified content type.
106 107 108 109 |
# File 'lib/utopia/controller/responder.rb', line 106 def handle(content_type, &block) @handlers[content_type] = Handler.new(content_type, block).freeze return self end |
#with(content_type, &block) ⇒ Object
Add a serializer for the specified content type.
129 130 131 |
# File 'lib/utopia/controller/responder.rb', line 129 def with(content_type, &block) return handle(content_type, &block) end |
#with_json ⇒ Object
Register the default JSON handler.
113 114 115 116 |
# File 'lib/utopia/controller/responder.rb', line 113 def with_json @handlers[Handlers::JSON::APPLICATION_JSON] = Handlers::JSON return self end |
#with_passthrough ⇒ Object
Register the wildcard passthrough handler.
120 121 122 123 |
# File 'lib/utopia/controller/responder.rb', line 120 def with_passthrough @passthrough = Handlers::Passthrough return self end |