Module: Forem::APIOperations::Request

Included in:
ForemObject
Defined in:
lib/forem/api_operations/request.rb

Overview

Provides request as both a class method and an instance method on any class that includes this module.

Including Request is the only prerequisite for a class to issue authenticated HTTP requests. It is included in Forem::APIResource and therefore inherited by every concrete resource class.

The module uses a self.included hook to also extend the including class with ClassMethods, making the class-level request available for module methods such as Create#create and List#list.

The :requestor key in opts is consumed here and never forwarded to Forem::APIRequestor#request.

Defined Under Namespace

Modules: ClassMethods

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.included(base) ⇒ void

This method returns an undefined value.

Hook called when Forem::APIOperations::Request is included in a class.

Parameters:

  • base (Class)

    the class including this module.



22
23
24
# File 'lib/forem/api_operations/request.rb', line 22

def self.included(base)
  base.extend(ClassMethods)
end

Instance Method Details

#request(method, path, params = {}, opts = {}) ⇒ ForemResponse

Issue an authenticated HTTP request on behalf of this resource instance.

Falls back to the requestor stored on the instance (set when the object was constructed by a service call) if no explicit :requestor is supplied in opts. Delegates to the class-level Forem::APIOperations::Request::ClassMethods#request for the actual transport.

Parameters:

  • method (Symbol)

    the HTTP verb.

  • path (String)

    the API path.

  • params (Hash) (defaults to: {})

    query parameters or JSON body payload.

  • opts (Hash) (defaults to: {})

    per-request options.

Options Hash (opts):

  • :requestor (APIRequestor)

    override the instance's stored requestor for this call.

Returns:

Raises:

  • (ArgumentError)

    if neither the instance nor opts supply a requestor.

  • (ForemError)

    on any API or network error.



76
77
78
79
80
# File 'lib/forem/api_operations/request.rb', line 76

def request(method, path, params = {}, opts = {})
  opts = opts.dup
  opts[:requestor] ||= @requestor if instance_variable_defined?(:@requestor)
  self.class.request(method, path, params, opts)
end