class Apia::Endpoint

Public Class Methods

catch_errors(response) { || ... } click to toggle source

Catch any runtime errors and update the given response with the appropriate values.

@param response [Apia::Response] @return [void]

# File lib/apia/endpoint.rb, line 82
def catch_errors(response)
  yield
rescue Apia::RuntimeError => e
  catch_errors(response) do
    response.body = { error: e.hash }
    response.status = e.http_status
    response.headers['x-api-schema'] = 'json-error'
  end
end
collate_objects(set) click to toggle source

Collate all objects that this endpoint references and add them to the given object set

@param set [Apia::ObjectSet] @return [void]

# File lib/apia/endpoint.rb, line 30
def collate_objects(set)
  set.add_object(definition.argument_set)

  definition.potential_errors.each do |error|
    set.add_object(error)
  end

  definition.fields.each_value do |field|
    set.add_object(field.type.klass) if field.type.usable_for_field?
  end
end
definition() click to toggle source

Return the definition object for the endpoint

@return [Apia::Definitions::Endpoint]

# File lib/apia/endpoint.rb, line 21
def definition
  @definition ||= Definitions::Endpoint.new(Helpers.class_name_to_id(name))
end
execute(request) click to toggle source

Run this request by providing a request to execute it with.

@param request [Apia::Request] @return [Apia::Response]

# File lib/apia/endpoint.rb, line 46
def execute(request)
  response = Response.new(request, self)
  environment = RequestEnvironment.new(request, response)

  catch_errors(response) do
    # Determine an authenticator and execute it before the request happens
    request.authenticator = definition.authenticator || request.controller&.definition&.authenticator || request.api&.definition&.authenticator
    request.authenticator&.execute(environment)

    # Determine if we're permitted to run the action based on the endpoint's scopes
    if request.authenticator && !request.authenticator.authorized_scope?(environment, definition.scopes)
      environment.raise_error Apia::ScopeNotGrantedError, scopes: definition.scopes
    end

    # Process arguments into the request. This happens after the authentication
    # stage because a) authenticators shouldn't be using endpoint specific args
    # and b) the argument conditions may need to know the identity.
    request.arguments = definition.argument_set.create_from_request(request)

    # Call the action for the endpoint
    endpoint_instance = new(environment)
    endpoint_instance.call_with_error_handling

    # We're going to call this here because we want to cache the actual values of
    # the output within the catch_errors block.
    response.hash
  end

  response
end
include_field?(*args) click to toggle source

Should a given field be included

# File lib/apia/endpoint.rb, line 94
def include_field?(*args)
  definition.fields.spec.include_field?(*args)
end
test() { |request| ... } click to toggle source

Allow an endpoint to be executed with a mocked request.

# File lib/apia/endpoint.rb, line 100
def test
  request = Apia::MockRequest.empty
  request.endpoint = self
  yield request if block_given?
  execute(request)
end