Class: X::Post
- Extended by:
- BatchFinders, PostCounts, PostSearch, PostWrites
- Includes:
- PostCollections, References
- Defined in:
- x-objects/lib/x/objects/post.rb
Overview
A post, also known as a tweet
Constant Summary collapse
- FIELDS =
Every public post field; the identifiers of referenced resources come with their expansions
The metrics that only the author, or an advertiser, may read, non_public_metrics, organic_metrics, and promoted_metrics, are left out, since a field that depends on who is authenticated would make every request fail for a client that cannot read it, as are the fields of Community Notes and of suggested sources, which the API gives to the programs they belong to. So is source, which the API has deprecated: a field it stops taking would make every request that asks for it fail. Nor does a post read it; a request that names it in its post.fields finds it in Resource#attrs.
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[article article_title attachments card_uri community_id context_annotations conversation_id created_at display_text_range edit_controls entities geo id lang media_metadata note_post paid_partnership possibly_sensitive public_metrics reply_settings scopes text withheld].freeze
- EXPANSIONS =
The expansions of the resources a post refers to that the object layer resolves
The identifiers of a post's edit history come with every post, so the edit_history_post_ids expansion, which would include each version of the post again, including the post itself, is left out, as is entities.mentions.username, which would include each user the post mentions, since nothing reads them.
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[attachments.media_keys attachments.media_source_tweet attachments.poll_ids author_id geo.place_id in_reply_to_user_id referenced_posts].freeze
Instance Attribute Summary collapse
-
#article ⇒ Hash?
readonly
The article the post publishes, with its title.
-
#article_title ⇒ Hash?
readonly
What the API describes of the title of the article the post publishes.
-
#attachments ⇒ Hash?
readonly
The attachment keys and identifiers.
-
#author_id ⇒ Integer?
readonly
The identifier of the author.
-
#bookmark_count ⇒ Integer?
readonly
The number of bookmarks.
-
#card_uri ⇒ String?
readonly
The URI of the card the post shows.
-
#community_id ⇒ Integer?
readonly
The identifier of the community the post was made in.
-
#context_annotations ⇒ Array<Hash>
readonly
The context annotations.
-
#conversation_id ⇒ Integer?
readonly
The identifier of the first post in the conversation.
-
#coordinates ⇒ Array<Numeric>?
readonly
The longitude and latitude the post was tagged with.
-
#created_at ⇒ Time?
readonly
The time when the post was created.
-
#display_text_range ⇒ Range<Integer>?
readonly
The range of characters of the text the API gives the post that is shown.
-
#edit_controls ⇒ Hash?
readonly
The edit controls.
-
#edit_history_post_ids ⇒ Array<Integer>
readonly
The identifiers of every version of the post.
-
#geo ⇒ Hash?
readonly
The tagged place and coordinates.
-
#impression_count ⇒ Integer?
readonly
The number of impressions.
-
#in_reply_to_user_id ⇒ Integer?
readonly
The identifier of the user being replied to.
-
#lang ⇒ String?
readonly
The BCP 47 language tag.
-
#like_count ⇒ Integer?
readonly
The number of likes.
-
#media_metadata ⇒ Array<Hash>
readonly
What the post describes of the media it attaches.
-
#note_post ⇒ Hash?
readonly
The full text and entities of a long post.
-
#paid_partnership ⇒ Boolean?
readonly
Whether the post is a paid partnership.
-
#possibly_sensitive ⇒ Boolean?
readonly
Whether the post may contain sensitive content.
-
#public_metrics ⇒ Hash?
readonly
The public metrics.
-
#quote_count ⇒ Integer?
readonly
The number of quotes.
-
#referenced_posts ⇒ Array<Hash>
readonly
The referenced posts with their types and identifiers.
-
#reply_count ⇒ Integer?
readonly
The number of replies.
-
#reply_settings ⇒ String?
readonly
Who can reply: everyone, mentionedUsers, or following.
-
#repost_count ⇒ Integer?
readonly
The number of reposts.
-
#scopes ⇒ Hash?
readonly
Who may see the post.
-
#withheld ⇒ Hash?
readonly
The withholding details.
Class Method Summary collapse
-
.default_params ⇒ Hash{String => Array<String>}
The default query parameters requesting every post field and expansion.
Instance Method Summary collapse
-
#author ⇒ User?
The author, resolved from the includes or as a stub holding only its identifier.
-
#community ⇒ Community?
The community the post was made in, as a stub holding only its identifier.
-
#delete ⇒ Boolean
Delete this post as the authenticated user.
-
#entities ⇒ Hash?
The entities found in the full text.
-
#expanded_text ⇒ String?
The text with every shortened link replaced by the URL it stands for.
-
#hide_reply ⇒ Boolean
Hide this reply, as the author of the post it replies to.
-
#in_reply_to_user ⇒ User?
The user being replied to, resolved from the includes or built as a stub.
-
#matching_rules ⇒ Array<MatchingRule>
The rules of the filtered stream this post matched.
-
#media ⇒ Array<Media>
The attached media, from the includes or as stubs holding only their keys.
-
#media_source_posts ⇒ Array<Post>
(also: #media_source_tweets)
The posts the attached media was first posted with.
-
#paid_partnership? ⇒ Boolean
Check whether the post is a paid partnership.
-
#permalink ⇒ String
The permalink of the post, by the author's username when known.
-
#place ⇒ Place?
The tagged place, from the includes or as a stub holding only its identifier.
-
#polls ⇒ Array<Poll>
The attached polls, from the includes or as stubs holding only their identifiers.
-
#possibly_sensitive? ⇒ Boolean
Check whether the post may contain sensitive content.
-
#text ⇒ String?
The full text.
-
#unhide_reply ⇒ Boolean
Show this reply after hiding it, as the author of the post it replies to.
-
#uri ⇒ URI::Generic
The permalink of the post as a URI.
-
#urls ⇒ Array<Hash>
The links in the full text, each with its shortened url and its expanded_url.
Constructor Details
This class inherits a constructor from X::Resource
Instance Attribute Details
#article ⇒ Hash? (readonly)
The article the post publishes, with its title
235 |
# File 'x-objects/lib/x/objects/post.rb', line 235 attribute :article, :object |
#article_title ⇒ Hash? (readonly)
What the API describes of the title of the article the post publishes
243 |
# File 'x-objects/lib/x/objects/post.rb', line 243 attribute :article_title, :object |
#attachments ⇒ Hash? (readonly)
The attachment keys and identifiers
332 |
# File 'x-objects/lib/x/objects/post.rb', line 332 attribute :attachments, :object |
#author_id ⇒ Integer? (readonly)
The identifier of the author
132 |
# File 'x-objects/lib/x/objects/post.rb', line 132 attribute :author_id, :integer |
#bookmark_count ⇒ Integer? (readonly)
The number of bookmarks
412 |
# File 'x-objects/lib/x/objects/post.rb', line 412 attribute :bookmark_count, :integer, key: %w[public_metrics bookmark_count] |
#card_uri ⇒ String? (readonly)
The URI of the card the post shows
227 |
# File 'x-objects/lib/x/objects/post.rb', line 227 attribute :card_uri |
#community_id ⇒ Integer? (readonly)
The identifier of the community the post was made in
148 |
# File 'x-objects/lib/x/objects/post.rb', line 148 attribute :community_id, :integer |
#context_annotations ⇒ Array<Hash> (readonly)
The context annotations
315 |
# File 'x-objects/lib/x/objects/post.rb', line 315 attribute :context_annotations, :objects |
#conversation_id ⇒ Integer? (readonly)
The identifier of the first post in the conversation
140 |
# File 'x-objects/lib/x/objects/post.rb', line 140 attribute :conversation_id, :integer |
#coordinates ⇒ Array<Numeric>? (readonly)
The longitude and latitude the post was tagged with
340 |
# File 'x-objects/lib/x/objects/post.rb', line 340 attribute :coordinates, key: %w[geo coordinates coordinates] |
#created_at ⇒ Time? (readonly)
The time when the post was created
124 |
# File 'x-objects/lib/x/objects/post.rb', line 124 attribute :created_at, :time |
#display_text_range ⇒ Range<Integer>? (readonly)
The range of characters of the text the API gives the post that is shown
It leaves out the mentions a reply begins with and the link to media it ends with. It is a range of the text the post holds itself, which for a long post is the text cut short, not the full text #text reads.
208 |
# File 'x-objects/lib/x/objects/post.rb', line 208 attribute :display_text_range, :range |
#edit_controls ⇒ Hash? (readonly)
The edit controls
195 |
# File 'x-objects/lib/x/objects/post.rb', line 195 attribute :edit_controls, :object |
#edit_history_post_ids ⇒ Array<Integer> (readonly)
The identifiers of every version of the post
187 |
# File 'x-objects/lib/x/objects/post.rb', line 187 attribute :edit_history_post_ids, :integers, tweet_key: %w[edit_history_tweet_ids] |
#geo ⇒ Hash? (readonly)
The tagged place and coordinates
348 |
# File 'x-objects/lib/x/objects/post.rb', line 348 attribute :geo, :object |
#impression_count ⇒ Integer? (readonly)
The number of impressions
420 |
# File 'x-objects/lib/x/objects/post.rb', line 420 attribute :impression_count, :integer, key: %w[public_metrics impression_count] |
#in_reply_to_user_id ⇒ Integer? (readonly)
The identifier of the user being replied to
156 |
# File 'x-objects/lib/x/objects/post.rb', line 156 attribute :in_reply_to_user_id, :integer |
#lang ⇒ String? (readonly)
The BCP 47 language tag
116 |
# File 'x-objects/lib/x/objects/post.rb', line 116 attribute :lang |
#like_count ⇒ Integer? (readonly)
The number of likes
396 |
# File 'x-objects/lib/x/objects/post.rb', line 396 attribute :like_count, :integer, key: %w[public_metrics like_count] |
#media_metadata ⇒ Array<Hash> (readonly)
What the post describes of the media it attaches
251 |
# File 'x-objects/lib/x/objects/post.rb', line 251 attribute :media_metadata, :objects |
#note_post ⇒ Hash? (readonly)
The full text and entities of a long post
364 |
# File 'x-objects/lib/x/objects/post.rb', line 364 attribute :note_post, :object, tweet_key: %w[note_tweet] |
#paid_partnership ⇒ Boolean? (readonly)
Whether the post is a paid partnership
259 |
# File 'x-objects/lib/x/objects/post.rb', line 259 attribute :paid_partnership, :boolean |
#possibly_sensitive ⇒ Boolean? (readonly)
Whether the post may contain sensitive content
164 |
# File 'x-objects/lib/x/objects/post.rb', line 164 attribute :possibly_sensitive, :boolean |
#public_metrics ⇒ Hash? (readonly)
The public metrics
372 |
# File 'x-objects/lib/x/objects/post.rb', line 372 attribute :public_metrics, :object |
#quote_count ⇒ Integer? (readonly)
The number of quotes
404 |
# File 'x-objects/lib/x/objects/post.rb', line 404 attribute :quote_count, :integer, key: %w[public_metrics quote_count] |
#referenced_posts ⇒ Array<Hash> (readonly)
The referenced posts with their types and identifiers
323 |
# File 'x-objects/lib/x/objects/post.rb', line 323 attribute :referenced_posts, :objects, tweet_key: %w[referenced_tweets] |
#reply_count ⇒ Integer? (readonly)
The number of replies
388 |
# File 'x-objects/lib/x/objects/post.rb', line 388 attribute :reply_count, :integer, key: %w[public_metrics reply_count] |
#reply_settings ⇒ String? (readonly)
Who can reply: everyone, mentionedUsers, or following
179 |
# File 'x-objects/lib/x/objects/post.rb', line 179 attribute :reply_settings |
#repost_count ⇒ Integer? (readonly)
The number of reposts
380 |
# File 'x-objects/lib/x/objects/post.rb', line 380 attribute :repost_count, :integer, key: %w[public_metrics repost_count], tweet_key: %w[public_metrics retweet_count] |
#scopes ⇒ Hash? (readonly)
Who may see the post
A post its author shared with followers alone holds {"followers" => true}.
219 |
# File 'x-objects/lib/x/objects/post.rb', line 219 attribute :scopes, :object |
#withheld ⇒ Hash? (readonly)
The withholding details
356 |
# File 'x-objects/lib/x/objects/post.rb', line 356 attribute :withheld, :object |
Class Method Details
.default_params ⇒ Hash{String => Array<String>}
The default query parameters requesting every post field and expansion
87 88 89 90 |
# File 'x-objects/lib/x/objects/post.rb', line 87 def default_params {"post.fields" => FIELDS, "user.fields" => User::FIELDS, "media.fields" => Media::FIELDS, "poll.fields" => Poll::FIELDS, "place.fields" => Place::FIELDS, "expansions" => EXPANSIONS} end |
Instance Method Details
#author ⇒ User?
The author, resolved from the includes or as a stub holding only its identifier
428 |
# File 'x-objects/lib/x/objects/post.rb', line 428 reference :author, :User, key: %w[author_id] |
#community ⇒ Community?
The community the post was made in, as a stub holding only its identifier
444 |
# File 'x-objects/lib/x/objects/post.rb', line 444 reference :community, :Community, key: %w[community_id] |
#delete ⇒ Boolean
Delete this post as the authenticated user
532 533 534 |
# File 'x-objects/lib/x/objects/post.rb', line 532 def delete self.class.delete(self, client: client!) end |
#entities ⇒ Hash?
The entities found in the full text
The entities, such as links, mentions, and hashtags, come from note_post for a long post, whose note the API gives entities when the full text has any, and from the post itself for a short one, so that each lies where #text holds it. A long post whose note holds none has none: the entities the post holds itself, such as its annotations, lie in the text cut short, which attrs holds.
280 |
# File 'x-objects/lib/x/objects/post.rb', line 280 def entities = Shape.read_object("#{self.class}#entities", full["entities"]) |
#expanded_text ⇒ String?
The text with every shortened link replaced by the URL it stands for
A link the API expanded to no URL is left as it is. A link without a url to replace is not one the API documents, so it raises, as any value of a response that cannot be read does, rather than being passed over.
Every link is replaced in one pass over the text, so a URL a link stands for is never read for the links after it, and holds what it holds, even the shortened url of another link of the post. A url that begins another, longer one is not read in it.
The rest of the text is as #text reads it, HTML-escaped as the API sends it, and each URL is HTML-escaped as it is put in, since the API sends a URL as it is, so the whole text is escaped alike: unescape it to display it.
521 522 523 524 |
# File 'x-objects/lib/x/objects/post.rb', line 521 def expansions = urls.to_h { |link| Utils.read("#{self.class}#expanded_text", link) { expansion(link) } } text&.gsub(Regexp.union(expansions.keys.sort_by { |url| -url.length }), expansions) end |
#hide_reply ⇒ Boolean
Hide this reply, as the author of the post it replies to
542 543 544 |
# File 'x-objects/lib/x/objects/post.rb', line 542 def hide_reply self.class.hide_reply(self, client: client!) end |
#in_reply_to_user ⇒ User?
The user being replied to, resolved from the includes or built as a stub
436 |
# File 'x-objects/lib/x/objects/post.rb', line 436 reference :in_reply_to_user, :User, key: %w[in_reply_to_user_id] |
#matching_rules ⇒ Array<MatchingRule>
The rules of the filtered stream this post matched
A post the filtered stream delivers names the rules it matched, and any other post names none. The streaming client of x-streaming builds the posts of a stream given X::Post as its object_class.
302 303 304 305 |
# File 'x-objects/lib/x/objects/post.rb', line 302 def matching_rules reader = "#{self.class}#matching_rules" Shape.objects(reader, attrs["matching_rules"]).map { |rule| Utils.read(reader, rule) { MatchingRule.new(rule) } }.freeze end |
#media ⇒ Array<Media>
The attached media, from the includes or as stubs holding only their keys
460 |
# File 'x-objects/lib/x/objects/post.rb', line 460 references :media, :Media, key: %w[attachments media_keys] |
#media_source_posts ⇒ Array<Post> Also known as: media_source_tweets
The posts the attached media was first posted with
A post that attaches media another post was made with, as one that shares a video does, names that post as the source of the media. Each is read from the includes, or built as a stub holding only its identifier.
480 |
# File 'x-objects/lib/x/objects/post.rb', line 480 references :media_source_posts, :Post, key: %w[attachments media_source_tweet_id] |
#paid_partnership? ⇒ Boolean
Check whether the post is a paid partnership
|
|
# File 'x-objects/lib/x/objects/post.rb', line 261
|
#permalink ⇒ String
The permalink of the post, by the author's username when known
494 |
# File 'x-objects/lib/x/objects/post.rb', line 494 def permalink = "https://x.com/#{&.username || "i"}/status/#{id}" |
#place ⇒ Place?
The tagged place, from the includes or as a stub holding only its identifier
452 |
# File 'x-objects/lib/x/objects/post.rb', line 452 reference :place, :Place, key: %w[geo place_id] |
#polls ⇒ Array<Poll>
The attached polls, from the includes or as stubs holding only their identifiers
468 |
# File 'x-objects/lib/x/objects/post.rb', line 468 references :polls, :Poll, key: %w[attachments poll_ids] |
#possibly_sensitive? ⇒ Boolean
Check whether the post may contain sensitive content
|
|
# File 'x-objects/lib/x/objects/post.rb', line 166
|
#text ⇒ String?
The full text
A long post, of more than 280 characters, holds its full text in note_post, and a text cut short with an ellipsis and a link to the post; this reads the full text.
It is the text as the API sends it, which escapes &, <, and > as &, <, and >, and it stays so throughout 1.x, so unescape it to display it.
108 |
# File 'x-objects/lib/x/objects/post.rb', line 108 def text = full["text"] |
#unhide_reply ⇒ Boolean
Show this reply after hiding it, as the author of the post it replies to
552 553 554 |
# File 'x-objects/lib/x/objects/post.rb', line 552 def unhide_reply self.class.unhide_reply(self, client: client!) end |
#uri ⇒ URI::Generic
The permalink of the post as a URI
502 |
# File 'x-objects/lib/x/objects/post.rb', line 502 def uri = URI(permalink) |
#urls ⇒ Array<Hash>
The links in the full text, each with its shortened url and its expanded_url
289 |
# File 'x-objects/lib/x/objects/post.rb', line 289 def urls = Shape.objects("#{self.class}#urls", entities&.[]("urls")) |