Module: TwitterLabsAPI::Resources::Tweet

Included in:
Client
Defined in:
lib/twitter_labs_api/resources/tweet.rb

Constant Summary collapse

DEFAULT_TWEET_FIELDS =
%w[id author_id created_at lang public_metrics].freeze
DEFAULT_FIELDS =
{ tweet: DEFAULT_TWEET_FIELDS }.freeze

Instance Method Summary collapse

Instance Method Details

#get_tweet(id:, fields: DEFAULT_FIELDS) ⇒ Object

Returns a variety of information about a single Tweet specified by the requested ID.

Parameters:

  • :id (String)

    the ID of the requested Tweet

  • :fields (Hash)

    a hash for fields to include in the response payload;

    e.g.: { tweet: %w[id username], media: %w }

Returns:

  • Hash an object with requested tweet fields



13
14
15
16
17
18
# File 'lib/twitter_labs_api/resources/tweet.rb', line 13

def get_tweet(id:, fields: DEFAULT_FIELDS)
  url = "https://api.twitter.com/labs/2/tweets/#{id}"
  params = ParamsService.from_fields(fields)

  make_request(url: url, params: params)
end

#get_tweets(ids:, fields: DEFAULT_FIELDS) ⇒ Array<Hash>

Returns a variety of information about the Tweet specified by the requested ID or list of IDs.

Parameters:

  • :ids (Array<String>)

    the collection of requested Tweet IDs

  • :fields (Hash)

    a hash for fields to include in the response payload;

    e.g.: { tweet: %w[id username], media: %w }

Returns:

  • (Array<Hash>)

    of tweet objects with the requested tweet fields



26
27
28
29
30
31
32
# File 'lib/twitter_labs_api/resources/tweet.rb', line 26

def get_tweets(ids:, fields: DEFAULT_FIELDS)
  url = 'https://api.twitter.com/labs/2/tweets'
  params = ParamsService.from_fields(fields)
  params.merge!({ ids: ids.join(',') })

  make_request(url: url, params: params, is_collection: true)
end

#hide_reply(id:) ⇒ Object

Hides or unhides a reply to a Tweet.

Parameters:

  • :id (String)

    the ID of the requested Tweet; must belong to a conversation by the authenticated user

Returns:

  • boolean indicating the hidden status of the requested tweet



37
38
39
40
41
# File 'lib/twitter_labs_api/resources/tweet.rb', line 37

def hide_reply(id:)
  url = "https://api.twitter.com/labs/2/tweets/#{id}/hidden"

  make_request(url: url, method: :put)[:hidden]
end

#search(query:, fields: DEFAULT_FIELDS) ⇒ Array<Hash>

The Labs recent search endpoint returns Tweets from the last 7 days that match a search query.

Parameters:

  • :query (String)

    the search query

  • :fields (Hash)

    a hash for fields to include in the response payload;

    e.g.: { tweet: %w[id username], media: %w }

Returns:

  • (Array<Hash>)

    of tweet objects with the requested tweet fields



49
50
51
52
53
54
55
# File 'lib/twitter_labs_api/resources/tweet.rb', line 49

def search(query:, fields: DEFAULT_FIELDS)
  url = 'https://api.twitter.com/labs/2/tweets/search'
  params = ParamsService.from_fields(fields)
  params.merge!({ query: query })

  make_request(url: url, params: params, is_collection: true)
end