Module: X::Resources::BatchFinders
- Includes:
- Finders
- Included in:
- Media, Post, UserFinders, Space
- Defined in:
- x-resources/lib/x/resources/batch_finders.rb
Overview
Class methods that look resources up many at a time, extended into each resource class the API can look up that way
A resource the API looks up only one at a time, such as a list, a community, or a direct message event, extends Finders alone, and so answers neither find_all nor hydrate_all, rather than answer them only to raise.
Internal to x-resources: the methods it gives a resource class, such as X::Post.find_all, are public API, but the module is only how they are shared, and which classes extend or include it can change within 1.x.
Instance Method Summary collapse
-
#find(id, client:, **params) {|problem| ... } ⇒ Resource?
included
from Finders
Look up a resource by identifier.
-
#find!(id, client:, **params) ⇒ Resource
included
from Finders
Look up a resource by identifier, which must exist.
-
#find_all(ids, client:, concurrency: DEFAULT_CONCURRENCY, **params) {|problem| ... } ⇒ Array<Resource>
Look up many resources by identifier, in parallel batches.
-
#hydrate_all(resources, client:, concurrency: DEFAULT_CONCURRENCY, **params) {|problem| ... } ⇒ Array<Resource>
Replace the resources that are not hydrated with the full resources.
Instance Method Details
#find(id, client:, **params) {|problem| ... } ⇒ Resource? Originally defined in module Finders
Look up a resource by identifier
#find!(id, client:, **params) ⇒ Resource Originally defined in module Finders
Look up a resource by identifier, which must exist
The error it raises names the identifier looked up, whether the identifier or a resource was given. Its cause is the X::NotFound of a lookup the API answered with a 404 that reports the resource as not found.
#find_all(ids, client:, concurrency: DEFAULT_CONCURRENCY, **params) {|problem| ... } ⇒ Array<Resource>
Look up many resources by identifier, in parallel batches
The resources come back in the order of the identifiers they were asked for by, whatever order the batches were answered in, one for each identifier that was found, so an identifier asked for twice comes back twice, as hydrate_all keeps a resource it is given twice. Each identifier is asked for once, however often it is given.
97 98 99 100 |
# File 'x-resources/lib/x/resources/batch_finders.rb', line 97 def find_all(ids, client:, concurrency: DEFAULT_CONCURRENCY, **params, &) ids = ids.map { |id| Utils.id_of(id, self) } in_order_of(lookup_in_batches(endpoint!, batch_key, ids, client:, concurrency:, **params, &), ids) end |
#hydrate_all(resources, client:, concurrency: DEFAULT_CONCURRENCY, **params) {|problem| ... } ⇒ Array<Resource>
Replace the resources that are not hydrated with the full resources
A resource that hydrate would look up is looked up, which is a stub and also a resource a response included without every field, so what comes back is hydrated throughout. A resource that was not found is dropped, as is nil, which a reference to no resource reads as, such as the author of a post whose response named none, and a hydrated resource is kept as it is, so resources that are all hydrated need no lookup. What was found is stored in each original, so hydrating one of them afterwards costs no request, unless params override a default field or expansion parameter: what such a lookup found is not the full resource, so it is returned without being stored, and hydrating an original fetches the full resource. A resource that holds what hydrate returns, because hydrate or an earlier hydrate_all stored it, is replaced with that, and looked up again by no request, since the API bills each resource a lookup returns.
67 68 69 70 71 72 73 |
# File 'x-resources/lib/x/resources/batch_finders.rb', line 67 def hydrate_all(resources, client:, concurrency: DEFAULT_CONCURRENCY, **params, &) resources = resources.compact validate_class!(resources) partial = resources.reject { |resource| settled?(resource) } replace = replacer(find_all(partial, client:, concurrency:, **params, &), params) resources.filter_map { |resource| settled?(resource) ? resource.hydrate : replace.call(resource) }.freeze end |