Class: X::PersonalizedTrend

Inherits:
Object
  • Object
show all
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

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



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

Examples:

Get the raw attributes

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

Returns:

  • (Hash{String => Object}) —

    the attributes



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.

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



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

Examples:

Check whether the trends of the world changed since they were last read, a count included

X::Trend.at(1, client: client) == trends

Parameters:

  • other (Object) —

    the other object

Returns:

  • (Boolean) —

    true if the other is of the same class and 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

Examples:

Serialize a resource

user.as_json # => {"id" => "7505382", "username" => "sferik"}

Serialize a problem

problem.as_json # => {"title" => "Not Found Error"}

Returns:

  • (Hash{String => Object}) —

    the attributes

#category ⇒ String?

The category of the trend

Examples:

Get the category

trend.category # => "Technology"

Returns:

  • (String, nil) —

    the category



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

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



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.

Examples:

Write a value as YAML

YAML.dump(trend)

Parameters:

  • coder (Psych::Coder) —

    the coder YAML writes the value with

#hash ⇒ Integer Originally defined in module Resources::ValueEquality

The hash of the object, which equal objects share

Examples:

Collect the distinct rules the posts of a stream matched

posts.flat_map(&:matching_rules).uniq

Returns:

  • (Integer) —

    the hash

#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

Examples:

Read a value written as YAML

YAML.unsafe_load(YAML.dump(trend)).name

Parameters:

  • coder (Psych::Coder) —

    the coder YAML read the value with

Raises:

#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.

Examples:

Cache the trends of a place

Rails.cache.write("trends", X::Trend.at(1, client: client))

Returns:

  • (Array(Integer, Hash{String => Object})) —

    the number of the format, then the attributes

#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

Examples:

Read cached trends

Marshal.load(Marshal.dump(trend)).name

Parameters:

  • state (Array) —

    the state Marshal wrote

Raises:

#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



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.

Examples:

Get the number of posts

trend.post_count_text # => "12.3K posts"

Returns:

  • (String, nil) —

    the number of posts, as text



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.

Examples:

Convert a resource to a hash

user.to_h # => {"id" => "7505382", "username" => "sferik"}

Key the attributes by Symbol

user.to_h { |key, value| [key.to_sym, value] } # => {id: "7505382", username: "sferik"}

Delete the rule a post matched

streaming_client.delete_rules(post.matching_rules.first.to_h)

Yield Parameters:

  • key (String) —

    the name of each attribute

  • value (Object) —

    its value

Yield Returns:

  • (Array(Object, Object)) —

    the key and value of the attribute in the Hash

Returns:

  • (Hash) —

    the attributes, frozen, or the pairs the block returns

#to_json(state = nil) ⇒ String Originally defined in module Resources::Serialization

The attributes as JSON

Examples:

Serialize a resource

user.to_json # => "{\"id\":\"7505382\",\"username\":\"sferik\"}"

Cache a resource as JSON

Rails.cache.write("user", user.to_json)

Parameters:

  • state (JSON::State, nil) (defaults to: nil) —

    the state a JSON encoder passes, which the attributes are given

Returns:

  • (String) —

    the attributes as a JSON object

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



112
# File 'x-resources/lib/x/resources/personalized_trend.rb', line 112

def trending_since_text = attrs["trending_since"]