Module: X::Resources::UserFinders
Overview
Class methods that look users up by identifier or username, and the authenticated user, extended into User
Internal to x-resources: the methods it gives User, such as X::User.find_by_username, 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
-
#current(client:, **params) {|problem| ... } ⇒ User?
Look up the authenticated user.
-
#current!(client:, **params) ⇒ User
Look up the authenticated user, who must be found.
-
#find(id_or_username, client:, **params) {|problem| ... } ⇒ User?
Look up a user by identifier or username.
-
#find!(id_or_username, client:, **params) ⇒ User
Look up a user by identifier or username, which must exist.
-
#find_all(ids_or_usernames, client:, concurrency: DEFAULT_CONCURRENCY, **params) {|problem| ... } ⇒ Array<User>
Look up many users by identifier or username, in parallel batches.
-
#find_all_by_id(ids, client:, concurrency: DEFAULT_CONCURRENCY, **params) {|problem| ... } ⇒ Array<User>
Look up many users by identifier, in parallel batches.
-
#find_all_by_username(usernames, client:, concurrency: DEFAULT_CONCURRENCY, **params) {|problem| ... } ⇒ Array<User>
Look up many users by username, in parallel batches.
-
#find_by_id(id, client:, **params) {|problem| ... } ⇒ User?
Look up a user by identifier.
-
#find_by_id!(id, client:, **params) ⇒ User
Look up a user by identifier, which must exist.
-
#find_by_username(username, client:, **params) {|problem| ... } ⇒ User?
Look up a user by username.
-
#find_by_username!(username, client:, **params) ⇒ User
Look up a user by username, which must exist.
-
#hydrate_all(resources, client:, concurrency: DEFAULT_CONCURRENCY, **params) {|problem| ... } ⇒ Array<Resource>
included
from BatchFinders
Replace the resources that are not hydrated with the full resources.
Instance Method Details
#current(client:, **params) {|problem| ... } ⇒ User?
Look up the authenticated user
Its path names no user, so a 404 to it is no user that is missing, and raises X::NotFound as any request does.
221 222 223 |
# File 'x-resources/lib/x/resources/user_finders.rb', line 221 def current(client:, **params, &) lookup("users/me", client:, **params, &) #: User? end |
#current!(client:, **params) ⇒ User
Look up the authenticated user, who must be found
234 235 236 237 |
# File 'x-resources/lib/x/resources/user_finders.rb', line 234 def current!(client:, **params) problems = [] #: Array[Problem] current(client:, **params) { |problem| problems << problem } || raise(MissingResource.new("users/me returned no user", problems:)) end |
#find(id_or_username, client:, **params) {|problem| ... } ⇒ User?
Look up a user by identifier or username
An Integer or a user is looked up by identifier, and a String by username, with or without a leading at sign.
34 35 36 37 38 |
# File 'x-resources/lib/x/resources/user_finders.rb', line 34 def find(id_or_username, client:, **params, &) return super if Utils.id?(id_or_username) find_by_username(_ = id_or_username, client:, **params, &) end |
#find!(id_or_username, client:, **params) ⇒ User
Look up a user by identifier or username, which must exist
An Integer or a user is looked up by identifier, and a String by username, as find looks them up, so the error it raises names a username as find_by_username! names one, with the at sign of a handle.
57 58 59 60 61 |
# File 'x-resources/lib/x/resources/user_finders.rb', line 57 def find!(id_or_username, client:, **params) return super if Utils.id?(id_or_username) find_by_username!(_ = id_or_username, client:, **params) end |
#find_all(ids_or_usernames, client:, concurrency: DEFAULT_CONCURRENCY, **params) {|problem| ... } ⇒ Array<User>
Look up many users by identifier or username, in parallel batches
Integers and users are looked up by identifier, and Strings by username. The users come back in the order they were asked for, one for each value that was found, so a user asked for twice, by identifier or by username, comes back twice, without the ones that were not found. Every username is checked before any request, so one that is not a username raises before the identifiers are looked up, and billed.
81 82 83 84 85 86 |
# File 'x-resources/lib/x/resources/user_finders.rb', line 81 def find_all(ids_or_usernames, client:, concurrency: DEFAULT_CONCURRENCY, **params, &) ids, usernames = ids_or_usernames.partition { |value| Utils.id?(value) } usernames.each { |username| Utils.username!(username) } found = super(ids, client:, concurrency:, **params) #: Array[User] in_order(found + find_all_by_username(_ = usernames, client:, concurrency:, **params, &), ids_or_usernames) end |
#find_all_by_id(ids, client:, concurrency: DEFAULT_CONCURRENCY, **params) {|problem| ... } ⇒ Array<User>
Look up many users by identifier, in parallel batches
A String of digits is an identifier, as it is read from a response or an environment variable, so this looks the accounts those numbers identify up, where find_all would take them for usernames. The users come back in the order they were asked for, one for each identifier that was found, as find_all returns them.
104 105 106 107 |
# File 'x-resources/lib/x/resources/user_finders.rb', line 104 def find_all_by_id(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) #: Array[User] end |
#find_all_by_username(usernames, client:, concurrency: DEFAULT_CONCURRENCY, **params) {|problem| ... } ⇒ Array<User>
Look up many users by username, in parallel batches
The users come back in the order they were asked for, one for each username that was found, whatever its case, as find_all returns them, and each username is asked for once, however often, and in whatever case, it is given.
165 166 167 168 169 |
# File 'x-resources/lib/x/resources/user_finders.rb', line 165 def find_all_by_username(usernames, client:, concurrency: DEFAULT_CONCURRENCY, **params, &) usernames = usernames.map { |username| normalize(Utils.username!(username)) } found = lookup_in_batches("users/by", :usernames, usernames, client:, concurrency:, **params, &) #: Array[User] in_order(found, usernames) end |
#find_by_id(id, client:, **params) {|problem| ... } ⇒ User?
Look up a user by identifier
A String of digits is an identifier, as it is read from a response or an environment variable, so this looks the account that number identifies up, where find would take it for a username.
126 127 128 |
# File 'x-resources/lib/x/resources/user_finders.rb', line 126 def find_by_id(id, client:, **params, &) locate("#{endpoint!}/#{Utils.id_of(id, self)}", client:, **params, &) #: User? end |
#find_by_id!(id, client:, **params) ⇒ User
Look up a user by identifier, which must exist
144 145 146 147 |
# File 'x-resources/lib/x/resources/user_finders.rb', line 144 def find_by_id!(id, client:, **params) name = Utils.id_of(id, self) locate!("#{endpoint!}/#{name}", name, client:, **params) #: User end |
#find_by_username(username, client:, **params) {|problem| ... } ⇒ User?
Look up a user by username
It looks every value up as a username, a String of digits as the account whose handle is that number, as find looks up any String, so code that reads a value from elsewhere says which it means, as find_by_id does.
188 189 190 |
# File 'x-resources/lib/x/resources/user_finders.rb', line 188 def find_by_username(username, client:, **params, &) locate("users/by/username/#{Utils.username!(username)}", client:, **params, &) #: User? end |
#find_by_username!(username, client:, **params) ⇒ User
Look up a user by username, which must exist
206 207 208 |
# File 'x-resources/lib/x/resources/user_finders.rb', line 206 def find_by_username!(username, client:, **params) locate!("users/by/username/#{Utils.username!(username)}", "@#{Utils.username(username)}", client:, **params) #: User end |
#hydrate_all(resources, client:, concurrency: DEFAULT_CONCURRENCY, **params) {|problem| ... } ⇒ Array<Resource> Originally defined in module BatchFinders
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.