Class: Utopia::Controller::Respond::Responder
- Inherits:
-
Object
- Object
- Utopia::Controller::Respond::Responder
- Defined in:
- lib/utopia/controller/respond.rb
Constant Summary
- HTTP_ACCEPT =
'HTTP_ACCEPT'.freeze
- NOT_ACCEPTABLE_RESPONSE =
[406, {}, []].freeze
Instance Method Summary collapse
-
#call(context, request, path, response) ⇒ Object
-
#freeze ⇒ Object
-
#initialize ⇒ Responder
constructor
A new instance of Responder.
-
#with(content_type, &block) ⇒ Object
Add a converter for the specified content type.
-
#with_json ⇒ Object
Add a converter for JSON when requests accept ‘application/json’.
-
#with_passthrough ⇒ Object
Constructor Details
Instance Method Details
#call(context, request, path, response) ⇒ Object
140 141 142 143 144 145 146 147 148 149 150 151 |
# File 'lib/utopia/controller/respond.rb', line 140 def call(context, request, path, response) # Parse the list of browser preferred content types and return ordered by priority: media_types = HTTP::Accept::MediaTypes.browser_preferred_media_types(request.env) converter, media_range = @converters.for(media_types) if converter converter.call(context, response, media_range) else NOT_ACCEPTABLE_RESPONSE end end |
#freeze ⇒ Object
120 121 122 123 124 |
# File 'lib/utopia/controller/respond.rb', line 120 def freeze @converters.freeze super end |
#with(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.
127 128 129 |
# File 'lib/utopia/controller/respond.rb', line 127 def with(content_type, &block) @converters << Converter::Callback.new(content_type, block) end |
#with_json ⇒ Object
Add a converter for JSON when requests accept ‘application/json’
136 137 138 |
# File 'lib/utopia/controller/respond.rb', line 136 def with_json @converters << Converter::ToJSON end |
#with_passthrough ⇒ Object
131 132 133 |
# File 'lib/utopia/controller/respond.rb', line 131 def with_passthrough @converters << Passthrough end |