Module: Forem::APIOperations::Update

Included in:
Forem::Article, Billboard, Concept, Organization, Page, RecommendedArticlesList, RequestRedirect
Defined in:
lib/forem/api_operations/update.rb

Overview

Adds an update class method to any resource that extends this module.

Sends a PUT request to a specific resource URL with the supplied parameters as a JSON body, and returns a new resource instance constructed from the API response.

Examples:

Extending a resource class

class Forem::Article < Forem::APIResource
  extend APIOperations::Update
end

Instance Method Summary collapse

Instance Method Details

#update(id, params = {}, opts = {}) ⇒ ForemObject

Update an existing resource via the Forem API.

Sends a PUT request to {resource_path}/{id} with params serialised as a JSON body, and returns a new resource instance reflecting the updated state as returned by the server.

Examples:

Updating an article's title

article = client.articles.update(12345, article: { title: "Updated title" })
article.title  #=> "Updated title"

Parameters:

  • id (Integer, String)

    the unique identifier of the resource to update.

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

    the attributes to update (e.g. { article: { title: "New title" } }).

  • 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 resource instance populated with the updated data returned by the API.

Raises:

See Also:



40
41
42
43
44
# File 'lib/forem/api_operations/update.rb', line 40

def update(id, params = {}, opts = {})
  requestor = opts[:requestor]
  resp = request(:put, "#{resource_path}/#{id}", params, opts)
  construct_from(resp.parsed_body, requestor: requestor)
end