Class: X::Trend

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

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



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

Examples:

Get the raw attributes

trend.attrs # => {"trend_name" => "#ruby", "tweet_count" => 1234}

Returns:

  • (Hash{String => Object}) —

    the attributes



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.

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



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

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

#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



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.

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



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.

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



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.

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