Module: Forem::APIOperations::Retrieve

Overview

Adds a retrieve class method to any resource that extends this module.

Fetches a single resource by its ID with a GET request and returns a resource instance populated from the API response.

Examples:

Extending a resource class

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

Instance Method Summary collapse

Instance Method Details

#retrieve(id, opts = {}) ⇒ ForemObject

Retrieve a single resource by its ID from the Forem API.

Sends a GET request to {resource_path}/{id} and constructs a resource instance from the response body.

Examples:

Fetching an article by ID

article = client.articles.retrieve(12345)
article.title   #=> "Hello, world!"
article.id      #=> 12345

Parameters:

  • id (Integer, String)

    the unique identifier of the resource to fetch.

  • 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)

    the resource instance populated with data from the API response.

Raises:

See Also:



36
37
38
39
40
# File 'lib/forem/api_operations/retrieve.rb', line 36

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