Module: Forem::APIOperations::Save

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

Overview

Adds a save instance method to any resource that includes this module.

save is the instance-level complement to the class-level Update#update. It persists the current resource to the API by issuing a PUT request to the instance's own Forem::APIResource#resource_url, and returns a new resource object reflecting the server's response.

Examples:

Including in a resource class

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

Instance Method Summary collapse

Instance Method Details

#save(params = {}, **opts) ⇒ ForemObject

Save (update) this resource instance via the Forem API.

Sends a PUT request to Forem::APIResource#resource_url with params serialised as a JSON body. The server response is used to construct and return a new resource instance representing the updated state.

Unlike Forem::APIResource#refresh, which reloads in-place, save returns a new instance and does not mutate the receiver.

Examples:

Updating a fetched article

article = client.articles.retrieve(12345)
updated = article.save(article: { title: "Brand new title" })
updated.title  #=> "Brand new title"

Parameters:

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

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

  • opts (Hash)

    per-request keyword 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 populated with the updated data returned by the API.

Raises:

See Also:



46
47
48
49
50
# File 'lib/forem/api_operations/save.rb', line 46

def save(params = {}, **opts)
  requestor = opts[:requestor] || @requestor
  resp = request(:put, resource_url, params, opts)
  self.class.construct_from(resp.parsed_body, requestor: requestor)
end