Module: Twitter::REST::Tweets

Includes:
UploadUtils, Utils, Utils
Included in:
API
Defined in:
lib/twitter/rest/tweets.rb

Overview

Methods for working with tweets

Constant Summary collapse

MAX_TWEETS_PER_REQUEST =

Maximum tweets per request

100

Constants included from Utils

Utils::DEFAULT_CURSOR

Instance Method Summary collapse

Methods included from Utils

flat_pmap, pmap

Instance Method Details

#destroy_status(*tweets) ⇒ Array<Twitter::Tweet> #destroy_status(*tweets, options) ⇒ Array<Twitter::Tweet> Also known as: destroy_tweet

Note:

The authenticating user must be the author of the specified Tweets.

Destroys the specified Tweets

Examples:

client.destroy_status(25938088801)

Overloads:

  • #destroy_status(*tweets) ⇒ Array<Twitter::Tweet>

    Parameters:

  • #destroy_status(*tweets, options) ⇒ Array<Twitter::Tweet>

    Parameters:

    • tweets (Enumerable<Integer, String, URI, Twitter::Tweet>)

      A collection of Tweet IDs, URIs, or objects.

    • options (Hash)

      A customizable set of options.

    Options Hash (options):

    • :trim_user (Boolean, String, Integer)

      Each tweet returned in a timeline will include a user object with only the author's numerical ID when set to true, 't' or 1.

Returns:

Raises:

See Also:



118
119
120
121
122
123
# File 'lib/twitter/rest/tweets.rb', line 118

def destroy_status(*args)
  arguments = Arguments.new(args)
  pmap(arguments) do |tweet|
    perform_post_with_object("/1.1/statuses/destroy/#{extract_id(tweet)}.json", arguments.options, Tweet)
  end
end

#oembed(tweet, options = {}) ⇒ Twitter::OEmbed

Returns oEmbed for a Tweet

Examples:

client.oembed(25938088801)

Parameters:

  • tweet (Integer, String, URI, Twitter::Tweet)

    A Tweet ID, URI, or object.

  • options (Hash) (defaults to: {})

    A customizable set of options.

Options Hash (options):

  • :maxwidth (Integer)

    The maximum width in pixels.

  • :hide_media (Boolean, String, Integer)

    Hide uploaded images.

  • :hide_thread (Boolean, String, Integer)

    Hide original message for replies.

  • :omit_script (Boolean, String, Integer)

    Omit widgets.js script element.

  • :align (String)

    Alignment: left, right, center, or none.

  • :related (String)

    Related users for Web Intents.

  • :lang (String)

    Language code for the rendered embed.

  • :widget_type (String)

    Set to video for video embed.

  • :hide_tweet (Boolean, String)

    Link directly to Tweet URL.

Returns:

Raises:

See Also:



286
287
288
289
290
# File 'lib/twitter/rest/tweets.rb', line 286

def oembed(tweet, options = {})
  options = options.dup
  options[:id] = extract_id(tweet)
  perform_get_with_object("/1.1/statuses/oembed.json", options, OEmbed)
end

#oembed(*tweets) ⇒ Array<Twitter::OEmbed> #oembed(*tweets, options) ⇒ Array<Twitter::OEmbed>

Returns oEmbeds for Tweets

Examples:

client.oembeds(25938088801, 25938088802)

Overloads:

  • #oembed(*tweets) ⇒ Array<Twitter::OEmbed>

    Parameters:

  • #oembed(*tweets, options) ⇒ Array<Twitter::OEmbed>

    Parameters:

    • tweets (Enumerable<Integer, String, URI, Twitter::Tweet>)

      A collection of Tweet IDs, URIs, or objects.

    • options (Hash)

      A customizable set of options.

    Options Hash (options):

    • :maxwidth (Integer)

      The maximum width in pixels that the embed should be rendered at. This value is constrained to be between 250 and 550 pixels.

    • :hide_media (Boolean, String, Integer)

      Specifies whether the embedded Tweet should automatically expand images which were uploaded via POST statuses/update_with_media. When set to either true, t or 1 images will not be expanded. Defaults to false.

    • :hide_thread (Boolean, String, Integer)

      Specifies whether the embedded Tweet should automatically show the original message in the case that the embedded Tweet is a reply. When set to either true, t or 1 the original Tweet will not be shown. Defaults to false.

    • :omit_script (Boolean, String, Integer)

      Specifies whether the embedded Tweet HTML should include a <script> element pointing to widgets.js. In cases where a page already includes widgets.js, setting this value to true will prevent a redundant script element from being included. When set to either true, t or 1 the <script> element will not be included in the embed HTML, meaning that pages must include a reference to widgets.js manually. Defaults to false.

    • :align (String)

      Specifies whether the embedded Tweet should be left aligned, right aligned, or centered in the page. Valid values are left, right, center, and none. Defaults to none, meaning no alignment styles are specified for the Tweet.

    • :related (String)

      A value for the TWT related parameter, as described in Web Intents. This value will be forwarded to all Web Intents calls.

    • :lang (String)

      Language code for the rendered embed. This will affect the text and localization of the rendered HTML.

Returns:

Raises:

See Also:



314
315
316
317
318
319
# File 'lib/twitter/rest/tweets.rb', line 314

def oembeds(*args)
  arguments = Arguments.new(args)
  pmap(arguments) do |tweet|
    oembed(tweet, arguments.options)
  end
end

#retweet(*tweets) ⇒ Array<Twitter::Tweet> #retweet(*tweets, options) ⇒ Array<Twitter::Tweet>

Retweets the specified Tweets as the authenticating user

Examples:

client.retweet(25938088801)

Overloads:

  • #retweet(*tweets) ⇒ Array<Twitter::Tweet>

    Parameters:

  • #retweet(*tweets, options) ⇒ Array<Twitter::Tweet>

    Parameters:

    • tweets (Enumerable<Integer, String, URI, Twitter::Tweet>)

      A collection of Tweet IDs, URIs, or objects.

    • options (Hash)

      A customizable set of options.

    Options Hash (options):

    • :trim_user (Boolean, String, Integer)

      Each tweet returned in a timeline will include a user object with only the author's numerical ID when set to true, 't' or 1.

Returns:

  • (Array<Twitter::Tweet>)

    The original tweets with retweet details embedded.

Raises:

See Also:



202
203
204
205
206
207
208
209
# File 'lib/twitter/rest/tweets.rb', line 202

def retweet(*args)
  arguments = Arguments.new(args)
  pmap(arguments) do |tweet|
    post_retweet(extract_id(tweet), arguments.options)
  rescue Error::AlreadyRetweeted, Error::NotFound
    nil
  end.compact
end

#retweet!(*tweets) ⇒ Array<Twitter::Tweet> #retweet!(*tweets, options) ⇒ Array<Twitter::Tweet>

Retweets the specified Tweets and raises an error if already retweeted

Examples:

client.retweet!(25938088801)

Overloads:

  • #retweet!(*tweets) ⇒ Array<Twitter::Tweet>

    Parameters:

  • #retweet!(*tweets, options) ⇒ Array<Twitter::Tweet>

    Parameters:

    • tweets (Enumerable<Integer, String, URI, Twitter::Tweet>)

      A collection of Tweet IDs, URIs, or objects.

    • options (Hash)

      A customizable set of options.

    Options Hash (options):

    • :trim_user (Boolean, String, Integer)

      Each tweet returned in a timeline will include a user object with only the author's numerical ID when set to true, 't' or 1.

Returns:

  • (Array<Twitter::Tweet>)

    The original tweets with retweet details embedded.

Raises:

See Also:



229
230
231
232
233
234
# File 'lib/twitter/rest/tweets.rb', line 229

def retweet!(*args)
  arguments = Arguments.new(args)
  pmap(arguments) do |tweet|
    post_retweet(extract_id(tweet), arguments.options)
  end
end

#retweeters_ids(options) ⇒ Twitter::Cursor #retweeters_ids(id, options = {}) ⇒ Twitter::Cursor

Returns up to 100 user IDs who retweeted the tweet

Examples:

client.retweeters_ids(25938088801)

Overloads:

  • #retweeters_ids(options) ⇒ Twitter::Cursor

    Parameters:

    • options (Hash)

      A customizable set of options.

  • #retweeters_ids(id, options = {}) ⇒ Twitter::Cursor

    Parameters:

    • tweet (Integer, String, URI, Twitter::Tweet)

      A Tweet ID, URI, or object.

    • options (Hash) (defaults to: {})

      A customizable set of options.

Returns:

Raises:

See Also:



336
337
338
339
340
# File 'lib/twitter/rest/tweets.rb', line 336

def retweeters_ids(*args)
  arguments = Arguments.new(args)
  arguments.options[:id] ||= extract_id(arguments.first)
  perform_get_with_cursor("/1.1/statuses/retweeters/ids.json", arguments.options, :ids)
end

#retweeters_of(tweet, options = {}) ⇒ Array

Shows up to 100 users who retweeted the Tweet

Examples:

client.retweeters_of(25938088801)

Parameters:

  • tweet (Integer, String, URI, Twitter::Tweet)

    A Tweet ID, URI, or object.

  • options (Hash) (defaults to: {})

    A customizable set of options.

Options Hash (options):

  • :count (Integer)

    Specifies the number of records to retrieve.

  • :trim_user (Boolean, String, Integer)

    Include only the author's ID.

  • :ids_only (Boolean)

    Only return user IDs.

Returns:

  • (Array)

Raises:

See Also:



54
55
56
57
58
59
# File 'lib/twitter/rest/tweets.rb', line 54

def retweeters_of(tweet, options = {})
  options = options.dup
  ids_only = options.delete(:ids_only)
  retweeters = retweets(tweet, options).collect(&:user)
  ids_only ? retweeters.collect(&:id) : retweeters
end

#retweets(tweet, options = {}) ⇒ Array<Twitter::Tweet>

Returns up to 100 of the first retweets of a given tweet

Examples:

client.retweets(25938088801)

Parameters:

  • tweet (Integer, String, URI, Twitter::Tweet)

    A Tweet ID, URI, or object.

  • options (Hash) (defaults to: {})

    A customizable set of options.

Options Hash (options):

  • :count (Integer)

    Specifies the number of records to retrieve.

  • :trim_user (Boolean, String, Integer)

    Include only the author's ID.

Returns:

Raises:

See Also:



35
36
37
# File 'lib/twitter/rest/tweets.rb', line 35

def retweets(tweet, options = {})
  perform_get_with_objects("/1.1/statuses/retweets/#{extract_id(tweet)}.json", options, Tweet)
end

#status(tweet, options = {}) ⇒ Twitter::Tweet

Returns a Tweet

Examples:

client.status(25938088801)

Parameters:

  • tweet (Integer, String, URI, Twitter::Tweet)

    A Tweet ID, URI, or object.

  • options (Hash) (defaults to: {})

    A customizable set of options.

Options Hash (options):

  • :trim_user (Boolean, String, Integer)

    Include only the author's ID.

Returns:

Raises:

See Also:



75
76
77
# File 'lib/twitter/rest/tweets.rb', line 75

def status(tweet, options = {})
  perform_get_with_object("/1.1/statuses/show/#{extract_id(tweet)}.json", options, Tweet)
end

#statuses(*tweets) ⇒ Array<Twitter::Tweet> #statuses(*tweets, options) ⇒ Array<Twitter::Tweet>

Returns Tweets

Examples:

client.statuses(25938088801, 25938088802)

Overloads:

  • #statuses(*tweets) ⇒ Array<Twitter::Tweet>

    Parameters:

  • #statuses(*tweets, options) ⇒ Array<Twitter::Tweet>

    Parameters:

    • tweets (Enumerable<Integer, String, URI, Twitter::Tweet>)

      A collection of Tweet IDs, URIs, or objects.

    • options (Hash)

      A customizable set of options.

    Options Hash (options):

    • :trim_user (Boolean, String, Integer)

      Each tweet returned in a timeline will include a user object with only the author's numerical ID when set to true, 't' or 1.

Returns:

See Also:



94
95
96
97
98
99
# File 'lib/twitter/rest/tweets.rb', line 94

def statuses(*args)
  arguments = Arguments.new(args)
  flat_pmap(arguments.each_slice(MAX_TWEETS_PER_REQUEST)) do |tweets|
    perform_post_with_objects("/1.1/statuses/lookup.json", arguments.options.merge(id: tweets.collect { |u| extract_id(u) }.join(",")), Tweet)
  end
end

#unretweet(*tweets) ⇒ Array<Twitter::Tweet> #unretweet(*tweets, options) ⇒ Array<Twitter::Tweet>

Untweets a retweeted status as the authenticating user

Examples:

client.unretweet(25938088801)

Overloads:

  • #unretweet(*tweets) ⇒ Array<Twitter::Tweet>

    Parameters:

  • #unretweet(*tweets, options) ⇒ Array<Twitter::Tweet>

    Parameters:

    • tweets (Enumerable<Integer, String, URI, Twitter::Tweet>)

      A collection of Tweet IDs, URIs, or objects.

    • options (Hash)

      A customizable set of options.

    Options Hash (options):

    • :trim_user (Boolean, String, Integer)

      Each tweet returned in a timeline will include a user object with only the author's numerical ID when set to true, 't' or 1.

Returns:

  • (Array<Twitter::Tweet>)

    The original tweets with retweet details embedded.

Raises:

See Also:



358
359
360
361
362
363
364
365
# File 'lib/twitter/rest/tweets.rb', line 358

def unretweet(*args)
  arguments = Arguments.new(args)
  pmap(arguments) do |tweet|
    post_unretweet(extract_id(tweet), arguments.options)
  rescue Error::NotFound
    nil
  end.compact
end

#update(status, options = {}) ⇒ Twitter::Tweet

Note:

Duplicate statuses are ignored to prevent duplicates.

Updates the authenticating user's status

Examples:

client.update('I just posted a status update!')

Parameters:

  • status (String)

    The text of your status update, up to 280 characters.

  • options (Hash) (defaults to: {})

    A customizable set of options.

Options Hash (options):

  • :possibly_sensitive (Boolean, String, Integer)

    Sensitive content flag.

  • :in_reply_to_status (Twitter::Tweet)

    An existing status to reply to.

  • :in_reply_to_status_id (Integer)

    The ID of a status to reply to.

  • :lat (Float)

    The latitude of the location.

  • :long (Float)

    The longitude of the location.

  • :place (Twitter::Place)

    A place in the world.

  • :place_id (String)

    A place ID.

  • :display_coordinates (String)

    Pin on exact coordinates.

  • :trim_user (Boolean, String, Integer)

    Include only the author's ID.

Returns:

Raises:

See Also:



151
152
153
154
155
# File 'lib/twitter/rest/tweets.rb', line 151

def update(status, options = {})
  update!(status, options)
rescue Error::DuplicateStatus
  user_timeline(count: 1).first # steep:ignore NoMethod
end

#update!(status, options = {}) ⇒ Twitter::Tweet

Updates the authenticating user's status, raising an error on duplicate

Examples:

client.update!('I just posted a status update!')

Parameters:

  • status (String)

    The text of your status update, up to 280 characters.

  • options (Hash) (defaults to: {})

    A customizable set of options.

Options Hash (options):

  • :possibly_sensitive (Boolean, String, Integer)

    Sensitive content flag.

  • :in_reply_to_status (Twitter::Tweet)

    An existing status to reply to.

  • :in_reply_to_status_id (Integer)

    The ID of a status to reply to.

  • :lat (Float)

    The latitude of the location.

  • :long (Float)

    The longitude of the location.

  • :place (Twitter::Place)

    A place in the world.

  • :place_id (String)

    A place ID.

  • :display_coordinates (String)

    Pin on exact coordinates.

  • :trim_user (Boolean, String, Integer)

    Include only the author's ID.

Returns:

Raises:

See Also:



179
180
181
182
183
184
# File 'lib/twitter/rest/tweets.rb', line 179

def update!(status, options = {})
  hash = options.dup
  hash[:in_reply_to_status_id] = hash.delete(:in_reply_to_status).id unless hash[:in_reply_to_status].nil?
  hash[:place_id] = hash.delete(:place).woeid unless hash[:place].nil?
  perform_post_with_object("/1.1/statuses/update.json", hash.merge(status:), Tweet)
end

#update_with_media(status, media, options = {}) ⇒ Twitter::Tweet

Updates the authenticating user's status with media

Examples:

client.update_with_media('I just posted a photo!', File.new('/path/to/image.png'))

Parameters:

  • status (String)

    The text of your status update, up to 280 characters.

  • media (File, Array<File>)

    An image file or array of image files.

  • options (Hash) (defaults to: {})

    A customizable set of options.

Options Hash (options):

  • :possibly_sensitive (Boolean, String, Integer)

    Sensitive content flag.

  • :in_reply_to_status (Twitter::Tweet)

    An existing status to reply to.

  • :in_reply_to_status_id (Integer)

    The ID of a status to reply to.

  • :lat (Float)

    The latitude of the location.

  • :long (Float)

    The longitude of the location.

  • :place (Twitter::Place)

    A place in the world.

  • :place_id (String)

    A place ID.

  • :display_coordinates (String)

    Pin on exact coordinates.

  • :trim_user (Boolean, String, Integer)

    Include only the author's ID.

Returns:

Raises:

See Also:



258
259
260
261
262
263
# File 'lib/twitter/rest/tweets.rb', line 258

def update_with_media(status, media, options = {})
  media_ids = pmap(array_wrap(media)) do |medium|
    upload(medium).fetch(:media_id)
  end
  update!(status, options.merge(media_ids: media_ids.join(",")))
end