Module: Forem::APIOperations::Delete

Included in:
Forem::AdminConcept, Organization, Page, RequestRedirect, Segment
Defined in:
lib/forem/api_operations/delete.rb

Overview

Adds delete as both a class method and an instance method to any resource that includes this module.

The class method deletes by ID; the instance method deletes the receiver using its own Forem::APIResource#resource_url. Both methods return a resource object if the server responds with a body, otherwise they return nil (class method) or self (instance method).

The self.included hook extends the including class with ClassMethods so the class-level delete is available alongside the instance method.

Examples:

Extending a resource class

class Forem::Article < Forem::APIResource
  include APIOperations::Delete
end

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::Delete is included in a class.

Extends the including class with ClassMethods.

Parameters:

  • base (Class)

    the class including this module.



25
26
27
# File 'lib/forem/api_operations/delete.rb', line 25

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

Instance Method Details

#delete(opts = {}) ⇒ ForemObject

Delete this resource instance via the Forem API.

Sends a DELETE request to this instance's Forem::APIResource#resource_url. If the server responds with a non-empty body it is used to construct a new resource instance (representing the deleted state); otherwise self is returned.

Examples:

Deleting the current instance

article.delete

Parameters:

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

    per-request options.

Options Hash (opts):

  • :api_key (String)

    override the API key for this request.

  • :requestor (APIRequestor)

    a custom requestor to use.

Returns:

  • (ForemObject)

    a new resource instance built from the response body, or self if the response has no body.

Raises:



81
82
83
84
85
# File 'lib/forem/api_operations/delete.rb', line 81

def delete(opts = {})
  requestor = opts[:requestor] || @requestor
  resp = request(:delete, resource_url, {}, opts)
  resp.parsed_body ? self.class.construct_from(resp.parsed_body, requestor: requestor) : self
end