module JSONAPI::Utils::Request

Public Instance Methods

check_request() click to toggle source

Render an error response if the parsed request got any error.

@api public

# File lib/jsonapi/utils/request.rb, line 33
def check_request
  @request.errors.blank? || jsonapi_render_errors(json: @request)
end
jsonapi_request_handling() click to toggle source

Setup and check request before action gets actually evaluated.

@api public

# File lib/jsonapi/utils/request.rb, line 6
def jsonapi_request_handling
  setup_request
  check_request
rescue JSONAPI::Exceptions::InvalidResource,
       JSONAPI::Exceptions::InvalidField,
       JSONAPI::Exceptions::InvalidInclude,
       JSONAPI::Exceptions::InvalidSortCriteria => err
  jsonapi_render_errors(json: err)
end
process_request() click to toggle source

Override the JSONAPI::ActsAsResourceController#process_request method.

It might be removed when the following line on JR is fixed: github.com/cerebris/jsonapi-resources/blob/release-0-8/lib/jsonapi/acts_as_resource_controller.rb#L62

@return [String]

@api public

# File lib/jsonapi/utils/request.rb, line 45
def process_request
  operations = @request.operations
  unless JSONAPI.configuration.resource_cache.nil?
    operations.each {|op| op.options[:cache_serializer] = resource_serializer }
  end
  results = process_operations(operations)
  render_results(results)
rescue => e
  handle_exceptions(e)
end
relationship_params() click to toggle source

Helper to get params for relationship params.

@return [Hash]

@api public

# File lib/jsonapi/utils/request.rb, line 70
def relationship_params
  build_params_for(:relationship)
end
resource_params() click to toggle source

Helper to get params for the main resource.

@return [Hash]

@api public

# File lib/jsonapi/utils/request.rb, line 61
def resource_params
  build_params_for(:resource)
end
setup_request() click to toggle source

Instantiate the request object.

@return [JSONAPI::RequestParser]

@api public

# File lib/jsonapi/utils/request.rb, line 21
def setup_request
  @request ||= JSONAPI::RequestParser.new(
    params,
    context: context,
    key_formatter: key_formatter,
    server_error_callbacks: (self.class.server_error_callbacks || [])
  )
end

Private Instance Methods

build_params_for(param_type) click to toggle source

Extract params from request and build a Hash with params for either the main resource or relationships.

@return [Hash]

@api private

# File lib/jsonapi/utils/request.rb, line 82
def build_params_for(param_type)
  return {} if @request.operations.empty?

  keys = %i(attributes to_one to_many)
  operation = @request.operations.find { |e| e.options[:data].keys & keys == keys }

  if operation.nil?
    {}
  elsif param_type == :relationship
    operation.options[:data].values_at(:to_one, :to_many).compact.reduce(&:merge)
  else
    operation.options[:data][:attributes]
  end
end