Class: X::PersonalizedTrend

Inherits:
Object
  • Object
show all
Includes:
Serialization, ValueEquality, ValueMarshalling
Defined in:
x-objects/lib/x/objects/personalized_trend.rb

Overview

A topic trending for the authenticated user, as the trends X picks for them report it

A personalized trend has no identifier, and the API offers no lookup of one, so the trends are read all at once, for the user the client authenticates as. They are described as they are shown: the number of posts, and how long the topic has trended, are text such as "12.3K posts", rather than numbers.

Constant Summary collapse

FIELDS =

Every personalized trend field

A minor release may add to it the fields the API adds, so that the trends ask for them too.

%w[category post_count trend_name trending_since].freeze

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(attrs) ⇒ PersonalizedTrend

Initialize a trend from the attributes the API reported

Examples:

Build a trend

X::PersonalizedTrend.new({"trend_name" => "#ruby", "post_count" => "12.3K posts"})

Parameters:

  • attrs (Hash{String, Symbol => Object}) —

    the attributes

Raises:

  • (ArgumentError) —

    if the attributes are not a Hash



63
64
65
66
# File 'x-objects/lib/x/objects/personalized_trend.rb', line 63

def initialize(attrs)
  @attrs = Utils.deep_freeze(Utils.attributes!(attrs))
  freeze
end

Instance Attribute Details

#attrs ⇒ Hash{String => Object} (readonly)

The raw attributes of the trend

Examples:

Get the raw attributes

trend.attrs # => {"trend_name" => "#ruby", "category" => "Technology", ...}

Returns:

  • (Hash{String => Object}) —

    the attributes



36
37
38
# File 'x-objects/lib/x/objects/personalized_trend.rb', line 36

def attrs
  @attrs
end

Class Method Details

.all(client:, **params) ⇒ Array<PersonalizedTrend>

The topics trending for the authenticated user

The endpoint names no user, so these are always the trends of the user the client authenticates as, and it takes a user's authentication alone, so a client that authenticates as the app is refused.

Examples:

Print the topics trending for the authenticated user

X::PersonalizedTrend.all(client: client).each { |trend| puts "#{trend.name}: #{trend.post_count_text}" }

Parameters:

  • client (Object) —

    the client used to make the request

  • params (Hash) —

    query parameters merged over the default parameters

Returns:

Raises:

  • (InvalidAttribute) —

    if the response holds the trends as something other than a list of objects



50
51
52
53
# File 'x-objects/lib/x/objects/personalized_trend.rb', line 50

def self.all(client:, **params)
  body = client.get(Utils.path(ENDPOINT, {"personalized_trend.fields" => FIELDS}.merge(params)), **Utils::JSON_CLASSES)
  Shape.objects("#{self}.all", body.to_h["data"]).map { |attrs| new(attrs) }.freeze
end

Instance Method Details

#category ⇒ String?

The category of the trend

Examples:

Get the category

trend.category # => "Technology"

Returns:

  • (String, nil) —

    the category



82
# File 'x-objects/lib/x/objects/personalized_trend.rb', line 82

def category = attrs["category"]

#deconstruct_keys(keys) ⇒ Hash{Symbol => String, nil}

Deconstruct the trend into what its readers read, so it matches a hash pattern

Examples:

Keep the topics of the technology category

X::PersonalizedTrend.all(client: client).select { |trend| trend in {category: "Technology"} }

Parameters:

  • keys (Array<Symbol>, nil) —

    the keys the pattern asks for, or nil for every reader

Returns:

  • (Hash{Symbol => String, nil}) —

    what the readers read



113
# File 'x-objects/lib/x/objects/personalized_trend.rb', line 113

def deconstruct_keys(keys) = Utils.deconstruct(self, keys, %i[name category post_count_text trending_since_text])

#name ⇒ String?

The name of the trend, such as a hashtag or a phrase

Examples:

Get the name

trend.name # => "#ruby"

Returns:

  • (String, nil) —

    the name



74
# File 'x-objects/lib/x/objects/personalized_trend.rb', line 74

def name = attrs["trend_name"]

#post_count_text ⇒ String?

The number of posts about the trend, as the text X shows it

It is named for the text it is, apart from the post_count of X::Trend, which is an Integer, so that code that reads both trends never takes one for the other.

Examples:

Get the number of posts

trend.post_count_text # => "12.3K posts"

Returns:

  • (String, nil) —

    the number of posts, as text



93
# File 'x-objects/lib/x/objects/personalized_trend.rb', line 93

def post_count_text = attrs["post_count"]

How long the topic has trended, as the text X shows it

It is named for the text it is, as post_count_text is, so that it is not taken for a Time, which every other reader of when something happened answers.

Examples:

Get how long the topic has trended

trend.trending_since_text # => "Trending now"

Returns:

  • (String, nil) —

    how long the topic has trended, as text



104
# File 'x-objects/lib/x/objects/personalized_trend.rb', line 104

def trending_since_text = attrs["trending_since"]