Class: X::List
- Extended by:
- Finders, Resources::Finders
- Defined in:
- x-resources/lib/x/resources/list.rb
Overview
A curated list of users
Constant Summary collapse
- FIELDS =
Every public list field
A minor release may add to it the fields the API adds, so that a lookup asks for them too; see Resource#hydrated? for what that means for a resource looked up with a list of fields of its own.
%w[created_at description follower_count id member_count name private].freeze
- EXPANSIONS =
Every expansion available on list endpoints
A minor release may add to it the expansions the API adds, so that a lookup asks for them too; see Resource#hydrated? for what that means for a resource looked up with a list of expansions of its own.
%w[owner_id].freeze
Instance Attribute Summary collapse
-
#created_at ⇒ Time?
readonly
The time when the list was created.
-
#description ⇒ String?
readonly
The description.
-
#follower_count ⇒ Integer?
readonly
The number of followers.
-
#member_count ⇒ Integer?
readonly
The number of members.
-
#name ⇒ String?
readonly
The name.
-
#owner_id ⇒ Integer?
readonly
The identifier of the owner.
-
#private ⇒ Boolean?
readonly
Whether the list is private.
Class Method Summary collapse
-
.create(name, client:, **params) ⇒ List
Create a list owned by the authenticated user.
-
.default_params ⇒ Hash{String => Array<String>}
The default query parameters requesting every list field and expansion.
-
.delete(list, client:) ⇒ Boolean
Delete a list as the authenticated user.
-
.find(id, client:, **params) {|problem| ... } ⇒ Resource?
extended
from Resources::Finders
Look up a resource by identifier.
-
.find!(id, client:, **params) ⇒ Resource
extended
from Resources::Finders
Look up a resource by identifier, which must exist.
-
.update(list, client:, **params) ⇒ Boolean
Update the name, description, or privacy of a list as the authenticated user.
Instance Method Summary collapse
-
#add_member(user) ⇒ Boolean
Add a member to this list as the authenticated user.
-
#delete ⇒ Boolean
Delete this list as the authenticated user.
-
#followers(**params) ⇒ Cursor
The followers of this list.
-
#member?(user, max_pages: nil) ⇒ Boolean
Check whether a user is a member of this list, scanning until one matches.
-
#members(**params) ⇒ Cursor
The members of this list.
-
#owner ⇒ User?
The owner, resolved from the includes or as a stub holding only its identifier.
-
#permalink ⇒ String
The permalink of the list.
-
#posts(**params) ⇒ Cursor
(also: #tweets)
The posts by members of this list.
-
#private? ⇒ Boolean
Check whether the list is private.
-
#remove_member(user) ⇒ Boolean
Remove a member from this list as the authenticated user.
-
#update(**params) ⇒ Boolean
Update the name, description, or privacy of this list as the authenticated user.
-
#uri ⇒ URI::Generic
The permalink of the list as a URI.
Constructor Details
This class inherits a constructor from X::Resource
Instance Attribute Details
#created_at ⇒ Time? (readonly)
The time when the list was created
135 |
# File 'x-resources/lib/x/resources/list.rb', line 135 attribute :created_at, :time |
#description ⇒ String? (readonly)
The description
127 |
# File 'x-resources/lib/x/resources/list.rb', line 127 attribute :description |
#follower_count ⇒ Integer? (readonly)
The number of followers
143 |
# File 'x-resources/lib/x/resources/list.rb', line 143 attribute :follower_count, :integer |
#member_count ⇒ Integer? (readonly)
The number of members
151 |
# File 'x-resources/lib/x/resources/list.rb', line 151 attribute :member_count, :integer |
#name ⇒ String? (readonly)
The name
119 |
# File 'x-resources/lib/x/resources/list.rb', line 119 attribute :name |
#owner_id ⇒ Integer? (readonly)
The identifier of the owner
159 |
# File 'x-resources/lib/x/resources/list.rb', line 159 attribute :owner_id, :integer |
#private ⇒ Boolean? (readonly)
Whether the list is private
167 |
# File 'x-resources/lib/x/resources/list.rb', line 167 attribute :private, :boolean |
Class Method Details
.create(name, client:, **params) ⇒ List
Create a list owned by the authenticated user
77 78 79 80 |
# File 'x-resources/lib/x/resources/list.rb', line 77 def create(name, client:, **params) body = client.post("lists", {name:, **params}, **Utils::JSON_CLASSES) created_from_response(body, "POST lists", client:) end |
.default_params ⇒ Hash{String => Array<String>}
The default query parameters requesting every list field and expansion
63 64 65 |
# File 'x-resources/lib/x/resources/list.rb', line 63 def default_params {"list.fields" => FIELDS, "user.fields" => User::FIELDS, "expansions" => EXPANSIONS} end |
.delete(list, client:) ⇒ Boolean
Delete a list as the authenticated user
107 108 109 110 |
# File 'x-resources/lib/x/resources/list.rb', line 107 def delete(list, client:) body = client.delete("lists/#{Utils.id_of(list, self)}", **Utils::JSON_CLASSES) Utils.written(body, "deleted").eql?(true) end |
.find(id, client:, **params) {|problem| ... } ⇒ Resource? Originally defined in module Resources::Finders
Look up a resource by identifier
.find!(id, client:, **params) ⇒ Resource Originally defined in module Resources::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.
.update(list, client:, **params) ⇒ Boolean
Update the name, description, or privacy of a list as the authenticated user
92 93 94 95 96 97 |
# File 'x-resources/lib/x/resources/list.rb', line 92 def update(list, client:, **params) raise ArgumentError, "a list update needs a field to change, such as name, description, or private" if params.empty? body = client.put("lists/#{Utils.id_of(list, self)}", params, **Utils::JSON_CLASSES) Utils.written(body, "updated").eql?(true) end |
Instance Method Details
#add_member(user) ⇒ Boolean
Add a member to this list as the authenticated user
266 267 268 269 |
# File 'x-resources/lib/x/resources/list.rb', line 266 def add_member(user) body = client!.post("lists/#{id}/members", {user_id: Utils.id_of(user, User)}, **Utils::JSON_CLASSES) Utils.written(body, "is_member").eql?(true) end |
#delete ⇒ Boolean
Delete this list as the authenticated user
303 304 305 |
# File 'x-resources/lib/x/resources/list.rb', line 303 def delete self.class.delete(self, client: client!) end |
#followers(**params) ⇒ Cursor
The followers of this list
202 203 204 |
# File 'x-resources/lib/x/resources/list.rb', line 202 def followers(**params) cursor(User, "lists/#{id}/followers", max_results: MAX_RESULTS, total: :follower_count, **params) end |
#member?(user, max_pages: nil) ⇒ Boolean
Check whether a user is a member of this list, scanning until one matches
The API has no lookup for a membership, so this scans either the members of the list or the lists the user is on. A private list scans its members, since a user's memberships leave private lists out. A public list scans the lists the user is on when there are fewer of them than members, as its member_count and the user's listed_count tell, looking up the list or the user first when either is a stub. The API bills every resource a scan returns, so max_pages limits the pages it reads, raising PageLimitReached rather than read past them.
236 237 238 239 240 241 |
# File 'x-resources/lib/x/resources/list.rb', line 236 def member?(user, max_pages: nil) member, max_pages = User.from_id(user), PageLimit.check!(max_pages) return PageLimit.scan(members.stubs, member, what: "List#member?", max_pages:) unless fewer_memberships?(user) PageLimit.scan(User.from_id(member, client: client!).list_memberships.stubs, self, what: "List#member?", max_pages:) end |
#members(**params) ⇒ Cursor
The members of this list
191 192 193 |
# File 'x-resources/lib/x/resources/list.rb', line 191 def members(**params) cursor(User, "lists/#{id}/members", max_results: MAX_RESULTS, total: :member_count, **params) end |
#owner ⇒ User?
The owner, resolved from the includes or as a stub holding only its identifier
182 |
# File 'x-resources/lib/x/resources/list.rb', line 182 reference :owner, :User, key: %w[owner_id] |
#permalink ⇒ String
The permalink of the list
249 |
# File 'x-resources/lib/x/resources/list.rb', line 249 def permalink = "https://x.com/i/lists/#{id}" |
#posts(**params) ⇒ Cursor Also known as: tweets
The posts by members of this list
213 214 215 |
# File 'x-resources/lib/x/resources/list.rb', line 213 def posts(**params) cursor(Post, "lists/#{id}/tweets", max_results: MAX_RESULTS, **params) end |
#private? ⇒ Boolean
Check whether the list is private
|
|
# File 'x-resources/lib/x/resources/list.rb', line 169
|
#remove_member(user) ⇒ Boolean
Remove a member from this list as the authenticated user
278 279 280 281 |
# File 'x-resources/lib/x/resources/list.rb', line 278 def remove_member(user) body = client!.delete("lists/#{id}/members/#{Utils.id_of(user, User)}", **Utils::JSON_CLASSES) Utils.written(body, "is_member").eql?(false) end |
#update(**params) ⇒ Boolean
Update the name, description, or privacy of this list as the authenticated user
The list keeps the attributes it was built with, so refresh it to read the new ones.
293 294 295 |
# File 'x-resources/lib/x/resources/list.rb', line 293 def update(**params) self.class.update(self, client: client!, **params) end |
#uri ⇒ URI::Generic
The permalink of the list as a URI
257 |
# File 'x-resources/lib/x/resources/list.rb', line 257 def uri = URI(permalink) |