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

Instance Method Details

#base_meta(request) ⇒ Hash

The base “meta” to include in the top-level of all JSON API documents.

Parameters:

  • request (JSONAPI::Request)

    the current request. ‘JSONAPI::Request#warnings` are merged into the #base_response_meta.

Returns:

  • (Hash)


13
14
15
16
17
18
19
# File 'lib/carrot_rpc/rpc_server/jsonapi_resources.rb', line 13

def base_meta(request)
  if request.nil? || request.warnings.empty?
    base_response_meta
  else
    base_response_meta.merge(warnings: request.warnings)
  end
end

The base “links” to include the top-level of all JSON API documents before any operation result links are added.

Returns:

  • (Hash)

    Defaults to ‘{}`.



24
25
26
# File 'lib/carrot_rpc/rpc_server/jsonapi_resources.rb', line 24

def base_response_links
  {}
end

#base_response_metaHash

The base “meta” to include in the top-level of all JSON API documents before being merged with any request warnings in #base_meta.

Returns:

  • (Hash)

    Defaults to ‘{}`.



32
33
34
# File 'lib/carrot_rpc/rpc_server/jsonapi_resources.rb', line 32

def base_response_meta
  {}
end

#create_operations_processorJSONAPI::OperationsProcessor

The operations processor in the configuration or override this to use another operations processor

Returns:

  • (JSONAPI::OperationsProcessor)


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`.

Parameters:

  • operation_results (JSONAPI::OperationResults)

    The result of processing the ‘request`.

  • request (JSONAPI::Request)

    the request to respond to.

Returns:

  • (JSONAPI::ResponseDocument)


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: base_meta(request),
    base_links: base_response_links,
    resource_serializer_klass: resource_serializer_klass,
    request: request,
    serialization_options: serialization_options
  )
end

#handle_exceptions(exception, request:) ⇒ Hash

Note:

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.

Parameters:

  • exception (Exception)

    the original, exception that was caught

  • request (JSONAPI::Request)

    the request that triggered the ‘exception`

Returns:

  • (Hash)

    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.message} #{exception.backtrace.join("\n")}"
    }
    render_errors(internal_server_error.errors, request: request)
  end
end

#key_formatterJSONAPI::KeyFormatter

Note:

Override if you want to set a per controller key format.

Control by setting in an initializer:

JSONAPI.configuration.json_key_format = :camelized_key

Returns:

  • (JSONAPI::KeyFormatter)


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.

Parameters:

  • params (ActionController::Parameter{action: Symbol, controller: String})

    MUST set ‘:action` to the action name so that `JSONAPI::Request#setup_action` can dispatch to the correct `setup_*_action` method. MUST set `:controller` to a URL name for the controller, such as `“api/v1/partner”`, so that the resource can be looked up by the controller name.

Returns:

  • (Hash)

    rendered, but not encoded JSON.



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.

Parameters:

  • errors (Array<JSONAPI::Error>)

    errors to use in a JSON API errors Document

  • request (JSONAPI::Request)

    the request that caused the ‘errors`.

Returns:

  • (Hash)

    rendered, but not encoded JSON



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.

Parameters:

  • operation_results (JSONAPI::OperationResults)

    a collection of results from various operations.

  • request (JSONAPI::Request)

    the original request that generated the ‘operation_results`.

Returns:

  • (Hash)

    rendered, but not encoded JSON



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_klassClass<JSONAPI::ResourceSerializer>

Class to serialize ‘JSONAPI::Resource`s to JSON API documents.

Returns:

  • (Class<JSONAPI::ResourceSerializer>)


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_formatterJSONAPI::RouteFormatter

Control by setting in an initializer:

JSONAPI.configuration.route = :camelized_route

Returns:

  • (JSONAPI::RouteFormatter)


162
163
164
# File 'lib/carrot_rpc/rpc_server/jsonapi_resources.rb', line 162

def route_formatter
  JSONAPI.configuration.route_formatter
end

#serialization_optionsHash

Options passed to ‘resource_serializer_klass` instance when serializing the JSON API document.

Returns:

  • (Hash)

    Defaults to ‘{}`



169
170
171
# File 'lib/carrot_rpc/rpc_server/jsonapi_resources.rb', line 169

def serialization_options
  {}
end