Class: X::PostUsage
- Inherits:
-
Object
- Object
- X::PostUsage
- Includes:
- Serialization, ValueEquality, ValueMarshalling, Resources::Serialization, Resources::ValueEquality, Resources::ValueMarshalling
- Defined in:
- x-resources/lib/x/resources/post_usage.rb
Overview
How many posts the app's project has read, as the usage endpoint reports it
The API caps the posts a project reads each month, and bills each post read, so the usage shows both what a project has spent and how much of its cap remains.
Constant Summary collapse
- FIELDS =
Every field of the usage
%w[cap_reset_day daily_client_app_usage daily_project_usage project_cap project_id project_usage].freeze
Instance Attribute Summary collapse
-
#attrs ⇒ Hash{String => Object}
readonly
The raw attributes of the usage.
Class Method Summary collapse
-
.current(client:, **params) {|problem| ... } ⇒ PostUsage?
Look up the current post usage of the project the client's app belongs to.
-
.current!(client:, **params) ⇒ PostUsage
Look up the current post usage of the project, which must be returned.
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.
-
#cap_reset_day ⇒ Integer?
The day of the month the billing cycle, and so the usage, starts over.
-
#daily ⇒ Hash{Time => Integer}
The number of posts the project read each day.
-
#daily_by_app ⇒ Hash{Integer, nil => Hash{Time => Integer}}
The number of posts each of the project's apps read each day.
-
#deconstruct_keys(keys) ⇒ Hash{Symbol => Object}
Deconstruct the usage 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) ⇒ PostUsage
constructor
Initialize the usage 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.
-
#project_cap ⇒ Integer?
The number of posts the project may read in a billing cycle.
-
#project_id ⇒ Integer?
The identifier of the project.
-
#project_usage ⇒ Integer?
The number of posts the project has read in the current billing cycle.
-
#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) ⇒ PostUsage
Initialize the usage from the attributes the API reported
91 92 93 94 |
# File 'x-resources/lib/x/resources/post_usage.rb', line 91 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 usage
42 43 44 |
# File 'x-resources/lib/x/resources/post_usage.rb', line 42 def attrs @attrs end |
Class Method Details
.current(client:, **params) {|problem| ... } ⇒ PostUsage?
Look up the current post usage of the project the client's app belongs to
A project has one usage, so it is looked up by no identifier, as X::User.current looks up the one user a client signs in as. The usage endpoint takes app-only authentication, so a client that signs its requests with OAuth 1.0a looks the usage up with a copy that authenticates as the app. A client signed in with OAuth 2.0 as a user that holds no credentials of the app requests as the user, which the endpoint refuses with X::Forbidden.
A response that holds no usage returns nil, as X::User.current does for a users/me that holds no user, and passes the problems it reported to the block, if there is one.
63 64 65 66 67 |
# File 'x-resources/lib/x/resources/post_usage.rb', line 63 def self.current(client:, **params) body = Utils.app_client(client).get(Utils.path(ENDPOINT, {"usage.fields" => FIELDS}.merge(params)), **Utils::JSON_CLASSES) Problem.all_from(body).each { |problem| yield problem } if block_given? Hash.try_convert(body.to_h["data"])&.then { |data| new(data) } end |
.current!(client:, **params) ⇒ PostUsage
Look up the current post usage of the project, which must be returned
78 79 80 81 |
# File 'x-resources/lib/x/resources/post_usage.rb', line 78 def self.current!(client:, **params) problems = [] #: Array[Problem] current(client:, **params) { |problem| problems << problem } || raise(MissingResource.new("#{ENDPOINT} returned no usage", problems:)) 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
#cap_reset_day ⇒ Integer?
The day of the month the billing cycle, and so the usage, starts over
It is an Integer whether the response holds it as a number or as a String, as it holds the other counts.
129 |
# File 'x-resources/lib/x/resources/post_usage.rb', line 129 def cap_reset_day = integer("cap_reset_day", attrs["cap_reset_day"]) |
#daily ⇒ Hash{Time => Integer}
The number of posts the project read each day
139 |
# File 'x-resources/lib/x/resources/post_usage.rb', line 139 def daily = days("daily", Shape.dig("#{self.class}#daily", attrs, %w[daily_project_usage usage])) |
#daily_by_app ⇒ Hash{Integer, nil => Hash{Time => Integer}}
The number of posts each of the project's apps read each day
149 150 151 152 153 154 155 |
# File 'x-resources/lib/x/resources/post_usage.rb', line 149 def daily_by_app by_app = {} #: Hash[Integer?, Hash[Time, Integer]] Shape.objects("#{self.class}#daily_by_app", attrs["daily_client_app_usage"]).each do |app| by_app[integer("daily_by_app", app["client_app_id"])] = days("daily_by_app", app["usage"]) end by_app.freeze end |
#deconstruct_keys(keys) ⇒ Hash{Symbol => Object}
Deconstruct the usage into what its readers read, so it matches a hash pattern
Only the readers a pattern names are read, so one that names the counts of the project matches a usage whose days cannot be read.
168 |
# File 'x-resources/lib/x/resources/post_usage.rb', line 168 def deconstruct_keys(keys) = Utils.deconstruct(self, keys, %i[project_id project_usage project_cap cap_reset_day daily daily_by_app]) |
#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
#project_cap ⇒ Integer?
The number of posts the project may read in a billing cycle
118 |
# File 'x-resources/lib/x/resources/post_usage.rb', line 118 def project_cap = integer("project_cap", attrs["project_cap"]) |
#project_id ⇒ Integer?
The identifier of the project
102 |
# File 'x-resources/lib/x/resources/post_usage.rb', line 102 def project_id = integer("project_id", attrs["project_id"]) |
#project_usage ⇒ Integer?
The number of posts the project has read in the current billing cycle
110 |
# File 'x-resources/lib/x/resources/post_usage.rb', line 110 def project_usage = integer("project_usage", attrs["project_usage"]) |
#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