class Cuprum::Rails::Responders::JsonResponder

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.

@example Defining Error Responses

class CustomResponder < Cuprum::Rails::Responders::HtmlResponder
  match :failure, error: Spec::NotFound do |result|
    render_failure(result.error, status: 404)
  end

  match :failure, error: Spec::AuthorizationFailure do
    error = Cuprum::Error.new(message: "I can't let you do that, Dave")

    render_failure(error, status: 403)
  end
end

Constants

GENERIC_ERROR

Public Class Methods

new( action_name:, resource:, serializers:, matcher: nil, member_action: false, **_options ) click to toggle source

@param action_name [String, Symbol] The name of the action to match. @param matcher [Cuprum::Matcher] An optional matcher specific to the

action. This will be matched before any of the generic matchers.

@param member_action [Boolean] True if the action acts on a collection

item, not on the collection as a whole.

@param resource [Cuprum::Rails::Resource] The resource for the controller.

# File lib/cuprum/rails/responders/json_responder.rb, line 59
def initialize( # rubocop:disable Metrics/ParameterLists
  action_name:,
  resource:,
  serializers:,
  matcher:       nil,
  member_action: false,
  **_options
)
  super(
    action_name:     action_name,
    matcher:         matcher,
    member_action:   member_action,
    resource:        resource,
    root_serializer: Cuprum::Rails::Serializers::Json::Serializer.instance,
    serializers:     serializers
  )
end

Public Instance Methods

format() click to toggle source

@return [Symbol] the format of the responder.

# File lib/cuprum/rails/responders/json_responder.rb, line 81
def format
  :json
end
render(json, status: 200) click to toggle source

Creates a JsonResponse based on the given data and options.

@param json [Object] The data to serialize. @param status [Integer] The HTTP status of the response.

@return [Cuprum::Rails::Responses::JsonResponse] the response.

# File lib/cuprum/rails/responders/json_responder.rb, line 91
def render(json, status: 200)
  Cuprum::Rails::Responses::JsonResponse.new(
    data:   serialize(json),
    status: status
  )
end
render_failure(error, status: 500) click to toggle source

Creates a JsonResponse for a failed result.

@param error [Cuprum::Error] The error from the failed result. @param status [Integer] The HTTP status of the response.

@return [Cuprum::Rails::Responses::JsonResponse] the response.

# File lib/cuprum/rails/responders/json_responder.rb, line 104
def render_failure(error, status: 500)
  json = { 'ok' => false, 'error' => error }

  render(json, status: status)
end
render_success(value, status: 200) click to toggle source

Creates a JsonResponse for a successful result.

@param value [Object] The value of the successful result. @param status [Integer] The HTTP status of the response.

@return [Cuprum::Rails::Responses::JsonResponse] the response.

# File lib/cuprum/rails/responders/json_responder.rb, line 116
def render_success(value, status: 200)
  json = { 'ok' => true, 'data' => value }

  render(json, status: status)
end