Module: CarrotRpc::RpcServer::JSONAPIResources
- Defined in:
- lib/carrot_rpc/rpc_server/jsonapi_resources.rb
Overview
Allows a CarrotRpc::RpcServer subclass to behave the same as a controller that does ‘include JSONAPI::ActsAsResourceController`
Defined Under Namespace
Modules: Actions
Instance Method Summary collapse
-
#base_meta(request) ⇒ Hash
The base “meta” to include in the top-level of all JSON API documents.
-
#base_response_links ⇒ Hash
The base “links” to include the top-level of all JSON API documents before any operation result links are added.
-
#base_response_meta ⇒ Hash
The base “meta” to include in the top-level of all JSON API documents before being merged with any request warnings in #base_meta.
-
#create_operations_processor ⇒ JSONAPI::OperationsProcessor
The operations processor in the configuration or override this to use another operations processor.
-
#create_response_document(operation_results:, request:) ⇒ JSONAPI::ResponseDocument
The JSON API Document for the ‘operation_results` and `request`.
-
#handle_exceptions(exception, request:) ⇒ Hash
Rendered, but not encoded JSON.
-
#key_formatter ⇒ JSONAPI::KeyFormatter
Control by setting in an initializer: JSONAPI.configuration.json_key_format = :camelized_key.
-
#process_request_params(params) ⇒ Hash
Processes the params as a request and renders a response.
-
#render_errors(errors, request:) ⇒ Hash
Renders the ‘errors` as a JSON API errors Document.
-
#render_results(operation_results:, request:) ⇒ Hash
Renders the ‘operation_results` as a JSON API Document.
-
#resource_serializer_klass ⇒ Class<JSONAPI::ResourceSerializer>
Class to serialize ‘JSONAPI::Resource`s to JSON API documents.
-
#route_formatter ⇒ JSONAPI::RouteFormatter
Control by setting in an initializer: JSONAPI.configuration.route = :camelized_route.
-
#serialization_options ⇒ Hash
Options passed to ‘resource_serializer_klass` instance when serializing the JSON API document.
Instance Method Details
#base_meta(request) ⇒ Hash
The base “meta” to include in the top-level of all JSON API documents.
13 14 15 16 17 18 19 |
# File 'lib/carrot_rpc/rpc_server/jsonapi_resources.rb', line 13 def (request) if request.nil? || request.warnings.empty? else .merge(warnings: request.warnings) end end |
#base_response_links ⇒ Hash
The base “links” to include the top-level of all JSON API documents before any operation result links are added.
24 25 26 |
# File 'lib/carrot_rpc/rpc_server/jsonapi_resources.rb', line 24 def base_response_links {} end |
#base_response_meta ⇒ Hash
The base “meta” to include in the top-level of all JSON API documents before being merged with any request warnings in #base_meta.
32 33 34 |
# File 'lib/carrot_rpc/rpc_server/jsonapi_resources.rb', line 32 def {} end |
#create_operations_processor ⇒ JSONAPI::OperationsProcessor
The operations processor in the configuration or override this to use another operations processor
39 40 41 |
# File 'lib/carrot_rpc/rpc_server/jsonapi_resources.rb', line 39 def create_operations_processor JSONAPI.configuration.operations_processor.new end |
#create_response_document(operation_results:, request:) ⇒ JSONAPI::ResponseDocument
The JSON API Document for the ‘operation_results` and `request`.
48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 |
# File 'lib/carrot_rpc/rpc_server/jsonapi_resources.rb', line 48 def create_response_document(operation_results:, request:) # rubocop:disable Metrics/MethodLength JSONAPI::ResponseDocument.new( operation_results, primary_resource_klass: resource_klass, include_directives: request ? request.include_directives : nil, fields: request ? request.fields : nil, base_url: base_url(operation_results, request), key_formatter: key_formatter, route_formatter: route_formatter, base_meta: (request), base_links: base_response_links, resource_serializer_klass: resource_serializer_klass, request: request, serialization_options: ) end |
#handle_exceptions(exception, request:) ⇒ Hash
Override this to process other exceptions. Be sure to either call ‘super(exception)` or handle `JSONAPI::Exceptions::Error` and `raise` unhandled exceptions.
Returns rendered, but not encoded JSON.
71 72 73 74 75 76 77 78 79 80 81 82 |
# File 'lib/carrot_rpc/rpc_server/jsonapi_resources.rb', line 71 def handle_exceptions(exception, request:) case exception when JSONAPI::Exceptions::Error render_errors(exception.errors, request: request) else internal_server_error = JSONAPI::Exceptions::InternalServerError.new(exception) logger.error { # rubocop:disable Style/BlockDelimiters "Internal Server Error: #{exception.} #{exception.backtrace.join("\n")}" } render_errors(internal_server_error.errors, request: request) end end |
#key_formatter ⇒ JSONAPI::KeyFormatter
Override if you want to set a per controller key format.
Control by setting in an initializer:
JSONAPI.configuration.json_key_format = :camelized_key
90 91 92 |
# File 'lib/carrot_rpc/rpc_server/jsonapi_resources.rb', line 90 def key_formatter JSONAPI.configuration.key_formatter end |
#process_request_params(params) ⇒ Hash
Processes the params as a request and renders a response.
101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 |
# File 'lib/carrot_rpc/rpc_server/jsonapi_resources.rb', line 101 def process_request_params(params) # rubocop:disable Metrics/MethodLength request = JSONAPI::Request.new( params, context: { rpc_request: true }, key_formatter: key_formatter, server_error_callbacks: [] ) if !request.errors.empty? render_errors(request.errors, request: request) else operation_results = create_operations_processor.process(request) render_results( operation_results: operation_results, request: request ) end rescue => e handle_exceptions(e, request: request) end |
#render_errors(errors, request:) ⇒ Hash
Renders the ‘errors` as a JSON API errors Document.
127 128 129 130 131 132 133 134 135 136 |
# File 'lib/carrot_rpc/rpc_server/jsonapi_resources.rb', line 127 def render_errors(errors, request:) operation_results = JSONAPI::OperationResults.new result = JSONAPI::ErrorsOperationResult.new(errors[0].status, errors) operation_results.add_result(result) render_results( operation_results: operation_results, request: request ) end |
#render_results(operation_results:, request:) ⇒ Hash
Renders the ‘operation_results` as a JSON API Document.
143 144 145 146 147 148 149 |
# File 'lib/carrot_rpc/rpc_server/jsonapi_resources.rb', line 143 def render_results(operation_results:, request:) response_document = create_response_document( operation_results: operation_results, request: request ) response_document.contents.as_json end |
#resource_serializer_klass ⇒ Class<JSONAPI::ResourceSerializer>
Class to serialize ‘JSONAPI::Resource`s to JSON API documents.
154 155 156 |
# File 'lib/carrot_rpc/rpc_server/jsonapi_resources.rb', line 154 def resource_serializer_klass @resource_serializer_klass ||= JSONAPI::ResourceSerializer end |
#route_formatter ⇒ JSONAPI::RouteFormatter
Control by setting in an initializer:
JSONAPI.configuration.route = :camelized_route
162 163 164 |
# File 'lib/carrot_rpc/rpc_server/jsonapi_resources.rb', line 162 def route_formatter JSONAPI.configuration.route_formatter end |
#serialization_options ⇒ Hash
Options passed to ‘resource_serializer_klass` instance when serializing the JSON API document.
169 170 171 |
# File 'lib/carrot_rpc/rpc_server/jsonapi_resources.rb', line 169 def {} end |