Module: X::Resources::PostWrites

Included in:
Post
Defined in:
x-resources/lib/x/resources/post_writes.rb

Overview

Creating and deleting posts, and hiding replies, as the authenticated user

Internal to x-resources: the methods it gives Post, such as X::Post.create, 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

Instance Method Details

#create(text = nil, client:, reply_to: nil, quote: nil, media_ids: nil, community: nil, **params) ⇒ Post

Create a post as the authenticated user

The API bills each post created, and bills a post whose text holds a URL more than ten times as much. A post needs no text when it has something else to show, such as media.

Examples:

Create a post

X::Post.create("Hello, World!", client: client)

Post an image without text

X::Post.create(client: client, media_ids: media)

Reply to a post with an image

X::Post.create("Hello!", client: client, reply_to: post, media_ids: [media["id"]])

Reply without the users a thread would otherwise mention

X::Post.create("Hello!", client: client, reply_to: post, reply: {exclude_reply_user_ids: ["7505382"]})

Post in a community

X::Post.create("Hello, Rubyists!", client: client, community: community)

Quote a post

X::Post.create("Worth reading", client: client, quote: post)

Parameters:

  • text (String, nil) (defaults to: nil) —

    the text of the post, or nil for a post without text, such as one of media alone

  • client (Object) —

    the client used to make the request

  • reply_to (Post, String, Integer, nil) (defaults to: nil) —

    the post to reply to or its identifier

  • quote (Post, String, Integer, nil) (defaults to: nil) —

    the post to quote or its identifier

  • media_ids (Array<String, Integer, #fetch, Media>, String, Integer, #fetch, Media, nil) (defaults to: nil) —

    the identifiers or media keys of uploaded media to attach, what the uploads returned, or media, such as that of a post, one or many; an empty list attaches nothing, as nil does

  • community (Community, String, Integer, nil) (defaults to: nil) —

    the community to post in or its identifier

  • params (Hash) —

    additional request body fields, such as poll or reply_settings, among them reply and media, whose other fields reply_to and media_ids are merged into, each named by a String or a Symbol

Returns:

  • (Post) —

    the created post, holding only its identifier and text

Raises:

  • (ArgumentError) —

    if the post has neither text nor any other field, which an empty media_ids is not

  • (MissingResource) —

    if the API answers without the post



46
47
48
49
50
51
52
# File 'x-resources/lib/x/resources/post_writes.rb', line 46

def create(text = nil, client:, reply_to: nil, quote: nil, media_ids: nil, community: nil, **params)
  params = Utils.fields(params)
  fields = {text:, **params, **referenced(params, reply_to:, quote:, media_ids:, community:)}.compact
  raise ArgumentError, "a post needs text, or something else to show, such as media_ids" if fields.empty?

  created_from_response(client.post("tweets", fields, **Utils::JSON_CLASSES), "POST tweets", client:)
end

#delete(post, client:) ⇒ Boolean

Delete a post as the authenticated user

Examples:

Delete a post

X::Post.delete("1234567890", client: client)

Parameters:

  • post (Post, String, Integer) —

    the post or its identifier

  • client (Object) —

    the client used to make the request

Returns:

  • (Boolean) —

    true if the post was deleted



62
63
64
65
# File 'x-resources/lib/x/resources/post_writes.rb', line 62

def delete(post, client:)
  body = client.delete("tweets/#{Utils.id_of(post, Post)}", **Utils::JSON_CLASSES)
  Utils.written(body, "deleted").eql?(true)
end

#hide_reply(post, client:) ⇒ Boolean

Hide a reply to a post of the authenticated user

X still shows a hidden reply, behind a notice that its author was hidden.

Examples:

Hide a reply

X::Post.hide_reply("1234567890", client: client)

Parameters:

  • post (Post, String, Integer) —

    the reply or its identifier

  • client (Object) —

    the client used to make the request

Returns:

  • (Boolean) —

    true if the reply is now hidden



77
78
79
# File 'x-resources/lib/x/resources/post_writes.rb', line 77

def hide_reply(post, client:)
  change_visibility(post, true, client:).eql?(true)
end

#unhide_reply(post, client:) ⇒ Boolean

Show a reply that was hidden, as the author of the post it replies to

Examples:

Show a hidden reply

X::Post.unhide_reply("1234567890", client: client)

Parameters:

  • post (Post, String, Integer) —

    the reply or its identifier

  • client (Object) —

    the client used to make the request

Returns:

  • (Boolean) —

    true if the reply is no longer hidden



89
90
91
# File 'x-resources/lib/x/resources/post_writes.rb', line 89

def unhide_reply(post, client:)
  change_visibility(post, false, client:).eql?(false)
end