Class: X::PersonalizedTrend
- Inherits:
-
Object
- Object
- X::PersonalizedTrend
- Includes:
- Serialization, ValueEquality, ValueMarshalling, Resources::Serialization, Resources::ValueEquality, Resources::ValueMarshalling
- Defined in:
- x-resources/lib/x/resources/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
-
#attrs ⇒ Hash{String => Object}
readonly
The raw attributes of the trend.
Class Method Summary collapse
-
.all(client:, **params) ⇒ Array<PersonalizedTrend>
The topics trending for the authenticated user.
Instance Method Summary collapse
-
#==(other) ⇒ Boolean
(also: #eql?)
included
from Resources::ValueEquality
Check whether another object holds the same attributes.
-
#as_json ⇒ Hash{String => Object}
included
from Resources::Serialization
The attributes, as a JSON encoder and ActiveSupport read them.
-
#category ⇒ String?
The category of the trend.
-
#deconstruct_keys(keys) ⇒ Hash{Symbol => String, nil}
Deconstruct the trend into what its readers read, so it matches a hash pattern.
-
#encode_with(coder) ⇒ void
included
from Resources::ValueMarshalling
Write the state Marshal writes as YAML.
-
#hash ⇒ Integer
included
from Resources::ValueEquality
The hash of the object, which equal objects share.
-
#init_with(coder) ⇒ void
included
from Resources::ValueMarshalling
Restore a value YAML read, frozen, as Marshal restores one.
-
#initialize(attrs) ⇒ PersonalizedTrend
constructor
Initialize a trend from the attributes the API reported.
-
#marshal_dump ⇒ Array(Integer, Hash{String => Object})
included
from Resources::ValueMarshalling
The state Marshal writes.
-
#marshal_load(state) ⇒ void
included
from Resources::ValueMarshalling
Restore a value Marshal read, frozen as the value that was written was.
-
#name ⇒ String?
The name of the trend, such as a hashtag or a phrase.
-
#post_count_text ⇒ String?
The number of posts about the trend, as the text X shows it.
-
#to_h {|key, value| ... } ⇒ Hash
included
from Resources::Serialization
The attributes, or with a block the Hash of the pairs it returns for them.
-
#to_json(state = nil) ⇒ String
included
from Resources::Serialization
The attributes as JSON.
-
#trending_since_text ⇒ String?
How long the topic has trended, as the text X shows it.
Constructor Details
#initialize(attrs) ⇒ PersonalizedTrend
Initialize a trend from the attributes the API reported
71 72 73 74 |
# File 'x-resources/lib/x/resources/personalized_trend.rb', line 71 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
44 45 46 |
# File 'x-resources/lib/x/resources/personalized_trend.rb', line 44 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.
58 59 60 61 |
# File 'x-resources/lib/x/resources/personalized_trend.rb', line 58 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
#==(other) ⇒ Boolean Also known as: eql? Originally defined in module Resources::ValueEquality
Check whether another object holds the same attributes
#as_json ⇒ Hash{String => Object} Originally defined in module Resources::Serialization
The attributes, as a JSON encoder and ActiveSupport read them
#category ⇒ String?
The category of the trend
90 |
# File 'x-resources/lib/x/resources/personalized_trend.rb', line 90 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
121 |
# File 'x-resources/lib/x/resources/personalized_trend.rb', line 121 def deconstruct_keys(keys) = Utils.deconstruct(self, keys, %i[name category post_count_text trending_since_text]) |
#encode_with(coder) ⇒ void Originally defined in module Resources::ValueMarshalling
This method returns an undefined value.
Write the state Marshal writes as YAML
YAML would write the instance variables of the value, and read them back into one that is not frozen, so it says how it is written: each part of the state Marshal writes, under its name.
#hash ⇒ Integer Originally defined in module Resources::ValueEquality
The hash of the object, which equal objects share
#init_with(coder) ⇒ void Originally defined in module Resources::ValueMarshalling
This method returns an undefined value.
Restore a value YAML read, frozen, as Marshal restores one
#marshal_dump ⇒ Array(Integer, Hash{String => Object}) Originally defined in module Resources::ValueMarshalling
The state Marshal writes
What is written is plain data, led by the number of its format, so that a value written by one release of 1.x is read by a later one: its attributes, as the API sent them.
#marshal_load(state) ⇒ void Originally defined in module Resources::ValueMarshalling
This method returns an undefined value.
Restore a value Marshal read, frozen as the value that was written was
#name ⇒ String?
The name of the trend, such as a hashtag or a phrase
82 |
# File 'x-resources/lib/x/resources/personalized_trend.rb', line 82 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.
101 |
# File 'x-resources/lib/x/resources/personalized_trend.rb', line 101 def post_count_text = attrs["post_count"] |
#to_h {|key, value| ... } ⇒ Hash Originally defined in module Resources::Serialization
The attributes, or with a block the Hash of the pairs it returns for them
With a block it is the to_h of the attributes, as a Hash builds it, rather than the attributes, the block ignored.
#to_json(state = nil) ⇒ String Originally defined in module Resources::Serialization
The attributes as JSON
#trending_since_text ⇒ String?
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.
112 |
# File 'x-resources/lib/x/resources/personalized_trend.rb', line 112 def trending_since_text = attrs["trending_since"] |