Class: X::Trend

Inherits:
Object
  • Object
show all
Includes:
Serialization, ValueEquality, ValueMarshalling
Defined in:
x-objects/lib/x/objects/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

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(attrs) ⇒ Trend

Initialize a trend from the attributes the API reported

Examples:

Build a trend

X::Trend.new({"trend_name" => "#ruby", "tweet_count" => 1234})

Parameters:

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

    the attributes

Raises:

  • (ArgumentError) —

    if the attributes are not a Hash



81
82
83
84
# File 'x-objects/lib/x/objects/trend.rb', line 81

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", "tweet_count" => 1234}

Returns:

  • (Hash{String => Object}) —

    the attributes



35
36
37
# File 'x-objects/lib/x/objects/trend.rb', line 35

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.

Examples:

Print the topics trending in the world

X::Trend.at(1, client: client).each { |trend| puts trend.name }

Parameters:

  • woeid (Integer, String) —

    the Yahoo! Where On Earth identifier of the place, such as 1 for the world

  • client (Object) —

    the client used to make the request

  • params (Hash) —

    query parameters, such as max_trends, which is 50, the most the API returns, unless given

Returns:

  • (Array<Trend>) —

    the trends, frozen

Raises:

  • (ArgumentError) —

    if the WOEID is not a number, before a request

  • (InvalidAttribute) —

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



51
52
53
54
55
# File 'x-objects/lib/x/objects/trend.rb', line 51

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

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

Examples:

Keep the topics of more than 10,000 posts

X::Trend.at(1, client: client).select { |trend| trend in {post_count: 10_000..} }

Parameters:

  • keys (Array<Symbol>, nil) —

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

Returns:

  • (Hash{Symbol => Object}) —

    what the readers read

Raises:

  • (InvalidAttribute) —

    if the pattern asks for a number of posts the response holds as something else



119
# File 'x-objects/lib/x/objects/trend.rb', line 119

def deconstruct_keys(keys) = Utils.deconstruct(self, keys, %i[name post_count], %i[tweet_count])

#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



92
# File 'x-objects/lib/x/objects/trend.rb', line 92

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.

Examples:

Get the number of posts

trend.post_count # => 1234

Returns:

  • (Integer, nil) —

    the number of posts, or nil if the API reported none

Raises:

  • (InvalidAttribute) —

    if the response holds a number of posts that is not a number



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

def post_count = Utils.read("#{self.class}#post_count", attrs["post_count"] || attrs["tweet_count"]) { |value| Shape.integer(value) }