Module: SimpleEndpoint

Defined in:
lib/simple_endpoint.rb,
lib/simple_endpoint/version.rb

Overview

rubocop:disable Metrics/ParameterLists

Defined Under Namespace

Modules: Controller Classes: Endpoint, OperationIsNotHandled, UnhadledResultError

Constant Summary collapse

OPERATION_IS_NOT_HANDLER_ERROR =
'Current operation result is not handled at #default_cases method'
HANDLER_ERROR_MESSAGE =
<<-LARGE_ERROR
  Please implement default_handler via case statement

  EXAMPLE:
  ###############################################

  # Can be put into ApplicationController and redefined in subclasses

  private

  def default_handler
    -> (kase, result) do
      case kase
      when :success then render :json ...
      else
        # just in case you forgot to add handler for some of case
        SimpleEndpoint::UnhadledResultError, 'Oh nooooo!!! Really???!!'
      end
    end
  end

  ###############################################

  OR

  You can move this logic to separate singleton class
LARGE_ERROR
CASES_ERROR_MESSAGE =
<<-LARGE_ERROR
  Please implement default cases conditions via hash

  EXAMPLE:
  ###############################################
  # default trailblazer-endpoint logic, you can change it
  # Can be put into ApplicationController and redefined in subclasses

  private

  def default_cases
    {
      present:         -> (result) { result.success? && result["present"] }
      success:         -> (result) { result.success? },
      created:         -> (result) { result.success? && result["model.action"] == :new }
      invalid:         -> (result) { result.failure? },
      not_found:       -> (result) { result.failure? && result["result.model"] && result["result.model"].failure? },
      unauthenticated: -> (result) { result.failure? && result["result.policy.default"] && result["result.policy.default"].failure? }
    }
  end

  ###############################################

  OR

  You can move this to separate singleton class
LARGE_ERROR
VERSION =
'1.0.0'