Class: Cuprum::Rails::Responders::JsonResponder
- Inherits:
-
BaseResponder
- Object
- BaseResponder
- Cuprum::Rails::Responders::JsonResponder
- Includes:
- Actions, Matching, Serialization
- Defined in:
- lib/cuprum/rails/responders/json_responder.rb
Overview
Provides a DSL for defining responses to JSON requests.
By default, responds to any successful result by serializing the result value and generating a JSON object of the form { ‘ok’ => true, ‘data’ => serialized_value }.
For a failing result, it generates and serializes a generic error and generates a JSON object of the form { ‘ok’ => false, ‘data’ => serialized_error }. This is to prevent leaks of internal states that might help an adversary access your system. Use the .match class method to define more useful responses for whitelisted errors.
Direct Known Subclasses
Instance Attribute Summary
Attributes included from Serialization
Attributes included from Matching
Attributes inherited from BaseResponder
#action_name, #controller, #controller_name, #request, #resource, #result
Instance Method Summary collapse
-
#call(result) ⇒ #call
Finds and calls the response clause that matches the given result.
-
#format ⇒ Symbol
The format of the responder.
-
#generic_error ⇒ Cuprum::Error
A generic error for generating failure responses.
-
#initialize(action_name:, controller:, request:, serializers:, member_action: false, **options) ⇒ JsonResponder
constructor
A new instance of JsonResponder.
-
#render(json, status: 200) ⇒ Cuprum::Rails::Responses::JsonResponse
Creates a JsonResponse based on the given data and options.
-
#render_failure(error, status: 500) ⇒ Cuprum::Rails::Responses::JsonResponse
Creates a JsonResponse for a failed result.
-
#render_success(value, status: 200) ⇒ Cuprum::Rails::Responses::JsonResponse
Creates a JsonResponse for a successful result.
Methods included from Serialization
Methods included from Actions
Methods included from Matching
Methods inherited from BaseResponder
Constructor Details
#initialize(action_name:, controller:, request:, serializers:, member_action: false, **options) ⇒ JsonResponder
Returns a new instance of JsonResponder.
67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 |
# File 'lib/cuprum/rails/responders/json_responder.rb', line 67 def initialize( # rubocop:disable Metrics/ParameterLists action_name:, controller:, request:, serializers:, member_action: false, ** ) super( action_name: action_name, controller: controller, matcher: matcher, member_action: member_action, resource: resource, request: request, serializers: serializers, ** ) end |
Instance Method Details
#call(result) ⇒ #call
Finds and calls the response clause that matches the given result.
-
Checks for an exact match (the result status, value, and error all match) in the given matcher (if any), then the responder class, then each ancestor of the responder class in ascending order.
-
If a match is not found, checks for a partial match (the result status, and either the value or the error match) in the same order.
-
If there is still no match found, checks for a generic match (the result status matches, and the match clause does not specify either an error or a value.
-
If there is no matching response clause, raises an exception.
If the responder defines an action matcher that matches the given action name, that matcher is matched against the result before any match clauses defined directly on the responder.
|
# File 'lib/cuprum/rails/responders/json_responder.rb', line 87
|
#format ⇒ Symbol
Returns the format of the responder.
91 92 93 |
# File 'lib/cuprum/rails/responders/json_responder.rb', line 91 def format :json end |
#generic_error ⇒ Cuprum::Error
Returns a generic error for generating failure responses.
96 97 98 |
# File 'lib/cuprum/rails/responders/json_responder.rb', line 96 def generic_error GENERIC_ERROR end |
#render(json, status: 200) ⇒ Cuprum::Rails::Responses::JsonResponse
Creates a JsonResponse based on the given data and options.
106 107 108 109 110 111 |
# File 'lib/cuprum/rails/responders/json_responder.rb', line 106 def render(json, status: 200) Cuprum::Rails::Responses::JsonResponse.new( data: serialize(json), status: status ) end |
#render_failure(error, status: 500) ⇒ Cuprum::Rails::Responses::JsonResponse
Creates a JsonResponse for a failed result.
119 120 121 122 123 |
# File 'lib/cuprum/rails/responders/json_responder.rb', line 119 def render_failure(error, status: 500) json = { 'ok' => false, 'error' => error } render(json, status: status) end |
#render_success(value, status: 200) ⇒ Cuprum::Rails::Responses::JsonResponse
Creates a JsonResponse for a successful result.
131 132 133 134 135 |
# File 'lib/cuprum/rails/responders/json_responder.rb', line 131 def render_success(value, status: 200) json = { 'ok' => true, 'data' => value } render(json, status: status) end |