Module: Forem::APIOperations::List
- Included in:
- Forem::AdminConcept, Forem::AgentSession, Forem::Article, Billboard, Comment, Concept, Organization, Page, PodcastEpisode, ReadingList, RecommendedArticlesList, RequestRedirect, Segment, Survey, Tag, Trend, Video
- Defined in:
- lib/forem/api_operations/list.rb
Overview
Adds a list class method to any resource that extends this module.
Fetches a paginated collection from the resource's path and wraps the result in a ListObject that supports manual and automatic pagination.
Instance Method Summary collapse
-
#list(params = {}, opts = {}) ⇒ ListObject
Retrieve a paginated list of resources from the Forem API.
Instance Method Details
#list(params = {}, opts = {}) ⇒ ListObject
Retrieve a paginated list of resources from the Forem API.
Sends a GET request to Forem::APIResource.resource_path with params
appended as query-string parameters. The response array is converted
into an array of resource instances and wrapped in a ListObject that
exposes pagination helpers.
Pagination defaults: page 1, per_page 30 (matching Forem API
defaults). These can be overridden via params.
52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 |
# File 'lib/forem/api_operations/list.rb', line 52 def list(params = {}, opts = {}) requestor = opts[:requestor] resp = request(:get, resource_path, params, opts) data = (resp.parsed_body || []).map { |item| construct_from(item, requestor: requestor) } per_page = params[:per_page] || params["per_page"] || 30 page = params[:page] || params["page"] || 1 ListObject.new( data: data, current_page: page.to_i, per_page: per_page.to_i, resource_class: self, filters: params.reject { |k, _| [:page, :per_page, "page", "per_page"].include?(k) }, requestor: requestor ) end |