Class: X::List
- Extended by:
- Finders
- Defined in:
- x-objects/lib/x/objects/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.
-
.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
129 |
# File 'x-objects/lib/x/objects/list.rb', line 129 attribute :created_at, :time |
#description ⇒ String? (readonly)
The description
121 |
# File 'x-objects/lib/x/objects/list.rb', line 121 attribute :description |
#follower_count ⇒ Integer? (readonly)
The number of followers
137 |
# File 'x-objects/lib/x/objects/list.rb', line 137 attribute :follower_count, :integer |
#member_count ⇒ Integer? (readonly)
The number of members
145 |
# File 'x-objects/lib/x/objects/list.rb', line 145 attribute :member_count, :integer |
#name ⇒ String? (readonly)
The name
113 |
# File 'x-objects/lib/x/objects/list.rb', line 113 attribute :name |
#owner_id ⇒ Integer? (readonly)
The identifier of the owner
153 |
# File 'x-objects/lib/x/objects/list.rb', line 153 attribute :owner_id, :integer |
#private ⇒ Boolean? (readonly)
Whether the list is private
161 |
# File 'x-objects/lib/x/objects/list.rb', line 161 attribute :private, :boolean |
Class Method Details
.create(name, client:, **params) ⇒ List
Create a list owned by the authenticated user
71 72 73 74 |
# File 'x-objects/lib/x/objects/list.rb', line 71 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
57 58 59 |
# File 'x-objects/lib/x/objects/list.rb', line 57 def default_params {"list.fields" => FIELDS, "user.fields" => User::FIELDS, "expansions" => EXPANSIONS} end |
.delete(list, client:) ⇒ Boolean
Delete a list as the authenticated user
101 102 103 104 |
# File 'x-objects/lib/x/objects/list.rb', line 101 def delete(list, client:) body = client.delete("lists/#{Utils.id_of(list, self)}", **Utils::JSON_CLASSES) body.to_h.dig("data", "deleted").eql?(true) end |
.update(list, client:, **params) ⇒ Boolean
Update the name, description, or privacy of a list as the authenticated user
86 87 88 89 90 91 |
# File 'x-objects/lib/x/objects/list.rb', line 86 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) body.to_h.dig("data", "updated").eql?(true) end |
Instance Method Details
#add_member(user) ⇒ Boolean
Add a member to this list as the authenticated user
260 261 262 263 |
# File 'x-objects/lib/x/objects/list.rb', line 260 def add_member(user) body = client!.post("lists/#{id}/members", {user_id: Utils.id_of(user, User)}, **Utils::JSON_CLASSES) body.to_h.dig("data", "is_member").eql?(true) end |
#delete ⇒ Boolean
Delete this list as the authenticated user
297 298 299 |
# File 'x-objects/lib/x/objects/list.rb', line 297 def delete self.class.delete(self, client: client!) end |
#followers(**params) ⇒ Cursor
The followers of this list
196 197 198 |
# File 'x-objects/lib/x/objects/list.rb', line 196 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.
230 231 232 233 234 235 |
# File 'x-objects/lib/x/objects/list.rb', line 230 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
185 186 187 |
# File 'x-objects/lib/x/objects/list.rb', line 185 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
176 |
# File 'x-objects/lib/x/objects/list.rb', line 176 reference :owner, :User, key: %w[owner_id] |
#permalink ⇒ String
The permalink of the list
243 |
# File 'x-objects/lib/x/objects/list.rb', line 243 def permalink = "https://x.com/i/lists/#{id}" |
#posts(**params) ⇒ Cursor Also known as: tweets
The posts by members of this list
207 208 209 |
# File 'x-objects/lib/x/objects/list.rb', line 207 def posts(**params) cursor(Post, "lists/#{id}/tweets", max_results: MAX_RESULTS, **params) end |
#private? ⇒ Boolean
Check whether the list is private
|
|
# File 'x-objects/lib/x/objects/list.rb', line 163
|
#remove_member(user) ⇒ Boolean
Remove a member from this list as the authenticated user
272 273 274 275 |
# File 'x-objects/lib/x/objects/list.rb', line 272 def remove_member(user) body = client!.delete("lists/#{id}/members/#{Utils.id_of(user, User)}", **Utils::JSON_CLASSES) body.to_h.dig("data", "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.
287 288 289 |
# File 'x-objects/lib/x/objects/list.rb', line 287 def update(**params) self.class.update(self, client: client!, **params) end |
#uri ⇒ URI::Generic
The permalink of the list as a URI
251 |
# File 'x-objects/lib/x/objects/list.rb', line 251 def uri = URI(permalink) |