Class: Forem::Trend

Inherits:
APIResource show all
Extended by:
APIOperations::List, APIOperations::Retrieve
Defined in:
lib/forem/resources/trend.rb

Overview

Represents a trend detected across a Forem instance's content.

Trends are algorithmically-derived topic clusters ("hot and recent" subject areas) made up of related articles. They are read-only via the public API — there is no create/update/delete surface, only listing, retrieval, and a nested endpoint for the articles that make up a trend.

Available operations (via mixins):

- +List+     — GET /api/trends
- +Retrieve+ — GET /api/trends/:id_or_slug

Trend Fields

  • id (Integer)
  • name (String)
  • slug (String)
  • description (String)
  • key_questions (Array) — Questions the trend's content clusters around
  • score (Float) — Relative trend strength/ranking score
  • articles_count (Integer) — Number of articles associated with the trend
  • cover_image (String) — URL of the trend's cover image
  • first_observed_at (String) — ISO 8601 timestamp
  • last_observed_at (String) — ISO 8601 timestamp
  • created_at (String) — ISO 8601 timestamp
  • updated_at (String) — ISO 8601 timestamp
  • type_of (String) — Always "trend"

The show response additionally includes top_articles, an array of up to three of the trend's highest-scoring published articles, each with id, title, slug, score, and published_at.

Examples:

List hot and recent trends

trends = client.trends.list(per_page: 10)
trends.data.each { |t| puts "#{t.name} (#{t.score})" }

Retrieve a trend by ID or slug

trend = client.trends.retrieve("ai-agents")
puts trend.description
trend.top_articles.each { |a| puts a["title"] }

List the articles that make up a trend

articles = client.trends.articles("ai-agents", per_page: 20, sort: "score")
articles.each { |a| puts a.title }

See Also:

Constant Summary collapse

OBJECT_NAME =
"trend"
RESOURCE_PATH =
"/api/trends"

Instance Attribute Summary

Attributes inherited from ForemObject

#requestor

Class Method Summary collapse

Methods included from APIOperations::List

list

Methods included from APIOperations::Retrieve

retrieve

Methods inherited from APIResource

#refresh, resource_path, #resource_url

Methods inherited from ForemObject

#==, #[], #[]=, construct_from, cursor_list, #initialize, #inspect, #method_missing, paginated_list, #respond_to_missing?, #to_hash

Methods included from APIOperations::Request

included, #request

Constructor Details

This class inherits a constructor from Forem::ForemObject

Dynamic Method Handling

This class handles dynamic methods through the method_missing method in the class Forem::ForemObject

Class Method Details

.articles(id_or_slug, params = {}, opts = {}) ⇒ Array<Forem::Article>

Return the articles belonging to a trend.

Sends a GET request to /api/trends/:id_or_slug/articles. Unlike list and retrieve, this is a class method that takes the trend's ID or slug directly, so it can be called without first retrieving the trend itself.

Examples:

Forem::Trend.articles("ai-agents", per_page: 5, requestor: requestor)

Parameters:

  • id_or_slug (Integer, String)

    the trend's numeric ID or slug

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

    query parameters

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

    per-request options

Options Hash (params):

  • :page (Integer)

    page number (default: 1)

  • :per_page (Integer)

    number of results per page (default: 10)

  • :sort (String)

    "score" to sort purely by article score; omit for the default ordering (trend membership distance, then score)

Options Hash (opts):

  • :api_key (String)

    override the API key for this request.

  • :requestor (APIRequestor)

    a custom requestor to use.

Returns:

See Also:



74
75
76
77
78
# File 'lib/forem/resources/trend.rb', line 74

def self.articles(id_or_slug, params = {}, opts = {})
  requestor = opts[:requestor]
  resp = request(:get, "#{resource_path}/#{id_or_slug}/articles", params, opts)
  (resp.parsed_body || []).map { |item| Forem::Article.construct_from(item, requestor: requestor) }
end