Class: X::MatchingRule

Inherits:
Object
  • Object
show all
Includes:
Serialization, ValueEquality, ValueMarshalling, Resources::Serialization, Resources::ValueEquality, Resources::ValueMarshalling
Defined in:
x-resources/lib/x/resources/matching_rule.rb

Overview

A rule of the filtered stream that a post the stream delivered matched: the identifier the API gave the rule, and the tag it is labelled with

The stream names a rule a post matched by its identifier and its tag alone, not by the value it matches, so this is not an X::StreamRule of x-streams, which holds that value. It names the rule to X::StreamingClient#delete_rules by its identifier, as rule.id, or as rule.to_h, and it is found among the rules X::StreamingClient#rules reads by the identifier they share. x-resources depends on x-streams for none of this: a stream builds each post with X::Post.from_response, as a request does.

It is frozen, compares equal to a rule of the same attributes, and matches a pattern of its identifier and tag, as in rule in {tag: "ruby"}. Its attributes are what the stream sent of the rule, as those of a resource are, so to_h, as_json, and to_json give them as the stream did, whatever the stream comes to send of a rule beside its identifier and tag.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(attrs) ⇒ MatchingRule

Initialize a rule a post matched from the attributes the stream sent of it

The identifier and tag are read as it is built, so that a rule that holds either as something else raises here, rather than from its readers.

Examples:

Build a rule a post matched

X::MatchingRule.new({"id" => "1165037377523306498", "tag" => "ruby"})

Parameters:

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

    the attributes, which name the identifier as id, as an Integer or as the String the stream sends, and the tag as tag, unless the rule has none

Raises:

  • (ArgumentError) —

    if the attributes are not a Hash, the identifier is not one, or the tag is neither a String nor nil



61
62
63
64
65
66
67
# File 'x-resources/lib/x/resources/matching_rule.rb', line 61

def initialize(attrs)
  @attrs = Utils.deep_freeze(Utils.attributes!(attrs))
  id
  raise ArgumentError, "tag must be a String, not #{tag.inspect}" unless tag.nil? || tag.is_a?(String)

  freeze
end

Instance Attribute Details

#attrs ⇒ Hash{String => Object} (readonly)

The attributes of the rule, as the stream sends them

The identifier is a String, as the stream sends it, and a rule without a tag holds none.

Examples:

Get the attributes

rule.attrs # => {"id" => "1165037377523306498", "tag" => "ruby"}

Returns:

  • (Hash{String => Object}) —

    the attributes, frozen



46
47
48
# File 'x-resources/lib/x/resources/matching_rule.rb', line 46

def attrs
  @attrs
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 => Integer, String, nil}

The identifier and tag of the rule, which a pattern matches against

Examples:

Keep the posts that matched the rule tagged ruby

post.matching_rules.any? { |rule| rule in {tag: "ruby"} }

Parameters:

  • keys (Array<Symbol>, nil) —

    the keys the pattern asks for, or nil for both

Returns:

  • (Hash{Symbol => Integer, String, nil}) —

    the identifier and tag the pattern asks for



96
# File 'x-resources/lib/x/resources/matching_rule.rb', line 96

def deconstruct_keys(keys) = Utils.deconstruct(self, keys, %i[id tag])

#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

#id ⇒ Integer

The identifier the API gave the rule

The API sends it as a String, and it is read as an Integer, as the identifier of a stream rule is, and as strictly as any identifier is read: a String of digits alone, with no sign, underscore, or whitespace.

Examples:

Get the identifier

rule.id # => 1165037377523306498

Returns:

  • (Integer) —

    the identifier

Raises:

  • (ArgumentError) —

    if the attributes hold no identifier, or one that is neither an Integer that is not negative nor a String of digits



80
# File 'x-resources/lib/x/resources/matching_rule.rb', line 80

def id = Shape.integer(attrs["id"]) || raise(ArgumentError, "a matching rule needs an id")

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

#inspect ⇒ String

Summarize the rule for the console

Examples:

Inspect a rule

rule.inspect # => #<X::MatchingRule id=1165037377523306498 tag="ruby">

Returns:

  • (String) —

    the class name, identifier, and tag



104
# File 'x-resources/lib/x/resources/matching_rule.rb', line 104

def inspect = "#<#{self.class} id=#{id} tag=#{tag.inspect}>"

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

#tag ⇒ String?

The tag the rule is labelled with

Examples:

Get the tag

rule.tag # => "ruby"

Returns:

  • (String, nil) —

    the tag, or nil for a rule without one



87
# File 'x-resources/lib/x/resources/matching_rule.rb', line 87

def tag = attrs["tag"]

#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