Class: X::Trend
- Inherits:
-
Object
- Object
- X::Trend
- Includes:
- Serialization, ValueEquality, ValueMarshalling, Resources::Serialization, Resources::ValueEquality, Resources::ValueMarshalling
- Defined in:
- x-resources/lib/x/resources/trend.rb
Overview
A topic trending in a place, as the trends of the place report it
A trend has no identifier, and the API offers no lookup of one, so trends are read a place at a time, a place named by its Yahoo! Where On Earth identifier (WOEID), such as 1 for the whole world.
Constant Summary collapse
- FIELDS =
Every trend field
A minor release may add to it the fields the API adds, so that the trends of a place ask for them too.
%w[trend_name tweet_count].freeze
Instance Attribute Summary collapse
-
#attrs ⇒ Hash{String => Object}
readonly
The raw attributes of the trend.
Class Method Summary collapse
-
.at(woeid, client:, **params) ⇒ Array<Trend>
The topics trending in a place.
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.
-
#deconstruct_keys(keys) ⇒ Hash{Symbol => Object}
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) ⇒ Trend
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 ⇒ Integer?
(also: #tweet_count)
The number of posts about the trend.
-
#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.
Constructor Details
#initialize(attrs) ⇒ Trend
Initialize a trend from the attributes the API reported
89 90 91 92 |
# File 'x-resources/lib/x/resources/trend.rb', line 89 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
43 44 45 |
# File 'x-resources/lib/x/resources/trend.rb', line 43 def attrs @attrs end |
Class Method Details
.at(woeid, client:, **params) ⇒ Array<Trend>
The topics trending in a place
The endpoint takes app-only authentication, or OAuth 2.0 as a user, so a client that signs with OAuth 1.0a reads the trends with a copy that authenticates as the app.
59 60 61 62 63 |
# File 'x-resources/lib/x/resources/trend.rb', line 59 def self.at(woeid, client:, **params) path = "trends/by/woeid/#{woeid_of(woeid)}" body = Utils.app_client(client).get(Utils.path(path, {"trend.fields" => FIELDS, "max_trends" => MAX_TRENDS}.merge(params)), **Utils::JSON_CLASSES) Shape.objects("#{self}.at", 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
#deconstruct_keys(keys) ⇒ Hash{Symbol => Object}
Deconstruct the trend into what its readers read, so it matches a hash pattern
A pattern that asks for every key gets name and post_count, and one can ask for the number of posts as tweet_count too, as it can of a user.
127 |
# File 'x-resources/lib/x/resources/trend.rb', line 127 def deconstruct_keys(keys) = Utils.deconstruct(self, keys, %i[name post_count], %i[tweet_count]) |
#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
100 |
# File 'x-resources/lib/x/resources/trend.rb', line 100 def name = attrs["trend_name"] |
#post_count ⇒ Integer? Also known as: tweet_count
The number of posts about the trend
It is read from post_count, or from tweet_count, the name the API gives it before it names tweets posts there, as the post count of a user is.
112 |
# File 'x-resources/lib/x/resources/trend.rb', line 112 def post_count = Utils.read("#{self.class}#post_count", attrs["post_count"] || attrs["tweet_count"]) { |value| Shape.integer(value) } |
#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