Module: Twitter::REST::Tweets

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

Constant Summary collapse

MAX_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

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:

Rate Limited?:

  • No

Authentication:

  • Requires user context


100
101
102
103
104
105
# File 'lib/twitter/rest/tweets.rb', line 100

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

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

Returns oEmbed for a Tweet

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 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.

  • :widget_type (String)

    Set to video to return a Twitter Video embed for the given Tweet.

  • :hide_tweet (Boolean, String)

    Applies to video type only. Set to 1 or true to link directly to the Tweet URL instead of displaying a Tweet overlay when a viewer clicks on the Twitter bird logo.

Returns:

Raises:

See Also:

Rate Limited?:

  • Yes

Authentication:

  • Requires user context


252
253
254
255
256
# File 'lib/twitter/rest/tweets.rb', line 252

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

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

Returns oEmbeds for Tweets

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:

Rate Limited?:

  • Yes

Authentication:

  • Requires user context


277
278
279
280
281
282
# File 'lib/twitter/rest/tweets.rb', line 277

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

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

Retweets the specified Tweets as the authenticating user

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:

Rate Limited?:

  • Yes

Authentication:

  • Requires user context


173
174
175
176
177
178
179
180
181
182
# File 'lib/twitter/rest/tweets.rb', line 173

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

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

Retweets the specified Tweets as the authenticating user and raises an error if one has already been retweeted

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:

Rate Limited?:

  • Yes

Authentication:

  • Requires user context


199
200
201
202
203
204
# File 'lib/twitter/rest/tweets.rb', line 199

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

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

Returns a collection of up to 100 user IDs belonging to users who have retweeted the tweet specified by the id parameter.

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:

Rate Limited?:

  • Yes

Authentication:

  • Required


296
297
298
299
300
# File 'lib/twitter/rest/tweets.rb', line 296

def retweeters_ids(*args)
  arguments = Twitter::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

Show up to 100 users who retweeted the Tweet

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. Must be less than or equal to 100.

  • :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.

  • :ids_only (Boolean) — default: 'false'

    Only return user IDs instead of full user objects.

Returns:

  • (Array)

Raises:

See Also:

Rate Limited?:

  • Yes

Authentication:

  • Requires user context


45
46
47
48
49
50
# File 'lib/twitter/rest/tweets.rb', line 45

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

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. Must be less than or equal to 100.

  • :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:

Rate Limited?:

  • Yes

Authentication:

  • Requires user context


29
30
31
# File 'lib/twitter/rest/tweets.rb', line 29

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

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

Returns a Tweet

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)

    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:

Rate Limited?:

  • Yes

Authentication:

  • Requires user context


63
64
65
# File 'lib/twitter/rest/tweets.rb', line 63

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

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

Returns Tweets

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:

Rate Limited?:

  • Yes

Authentication:

  • Required


79
80
81
82
83
84
# File 'lib/twitter/rest/tweets.rb', line 79

def statuses(*args)
  arguments = Twitter::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(',')), Twitter::Tweet)
  end
end

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

Untweets a retweeted status as the authenticating user

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:

Rate Limited?:

  • Yes

Authentication:

  • Requires user context


315
316
317
318
319
320
321
322
323
324
# File 'lib/twitter/rest/tweets.rb', line 315

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

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

Note:

A status update with text identical to the authenticating user's current status will be ignored to prevent duplicates.

Updates the authenticating user's status

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)

    Set to true for content which may not be suitable for every audience.

  • :in_reply_to_status (Twitter::Tweet)

    An existing status that the update is in reply to. If the status being replied to was not originally posted by the authenticated user, the text of the status must begin with an @-mention, or twitter will reject the update.

  • :in_reply_to_status_id (Integer)

    The ID of an existing status that the update is in reply to.

  • :lat (Float)

    The latitude of the location this tweet refers to. This option will be ignored unless it is inside the range -90.0 to +90.0 (North is positive) inclusive. It will also be ignored if there isn't a corresponding :long option.

  • :long (Float)

    The longitude of the location this tweet refers to. The valid ranges for longitude is -180.0 to +180.0 (East is positive) inclusive. This option will be ignored if outside that range, if it is not a number, if geo_enabled is disabled, or if there not a corresponding :lat option.

  • :place (Twitter::Place)

    A place in the world. These can be retrieved from PlacesAndGeo#reverse_geocode.

  • :place_id (String)

    A place in the world. These IDs can be retrieved from PlacesAndGeo#reverse_geocode.

  • :display_coordinates (String)

    Whether or not to put a pin on the exact coordinates a tweet has been sent from.

  • :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:

  • (Twitter::Tweet)

    The created Tweet. When the tweet is deemed a duplicate by Twitter, returns the last Tweet from the user's timeline.

Raises:

See Also:

Rate Limited?:

  • No

Authentication:

  • Requires user context


127
128
129
130
131
# File 'lib/twitter/rest/tweets.rb', line 127

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

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

Note:

A status update with text identical to the authenticating user's current status will be ignored to prevent duplicates.

Updates the authenticating user's status

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)

    Set to true for content which may not be suitable for every audience.

  • :in_reply_to_status (Twitter::Tweet)

    An existing status that the update is in reply to. If the status being replied to was not originally posted by the authenticated user, the text of the status must begin with an @-mention, or twitter will reject the update.

  • :in_reply_to_status_id (Integer)

    The ID of an existing status that the update is in reply to.

  • :lat (Float)

    The latitude of the location this tweet refers to. This option will be ignored unless it is inside the range -90.0 to +90.0 (North is positive) inclusive. It will also be ignored if there isn't a corresponding :long option.

  • :long (Float)

    The longitude of the location this tweet refers to. The valid ranges for longitude is -180.0 to +180.0 (East is positive) inclusive. This option will be ignored if outside that range, if it is not a number, if geo_enabled is disabled, or if there not a corresponding :lat option.

  • :place (Twitter::Place)

    A place in the world. These can be retrieved from PlacesAndGeo#reverse_geocode.

  • :place_id (String)

    A place in the world. These IDs can be retrieved from PlacesAndGeo#reverse_geocode.

  • :display_coordinates (String)

    Whether or not to put a pin on the exact coordinates a tweet has been sent from.

  • :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:

Rate Limited?:

  • No

Authentication:

  • Requires user context


153
154
155
156
157
158
# File 'lib/twitter/rest/tweets.rb', line 153

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: status), Twitter::Tweet)
end

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

Note:

A status update with text/media identical to the authenticating user's current status will NOT be ignored

Updates the authenticating user's status with media

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 (PNG, JPEG or GIF).

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

    A customizable set of options.

Options Hash (options):

  • :possibly_sensitive (Boolean, String, Integer)

    Set to true for content which may not be suitable for every audience.

  • :in_reply_to_status (Twitter::Tweet)

    An existing status that the update is in reply to.

  • :in_reply_to_status_id (Integer)

    The ID of an existing Tweet that the update is in reply to.

  • :lat (Float)

    The latitude of the location this tweet refers to. This option will be ignored unless it is inside the range -90.0 to +90.0 (North is positive) inclusive. It will also be ignored if there isn't a corresponding :long option.

  • :long (Float)

    The longitude of the location this tweet refers to. The valid ranges for longitude is -180.0 to +180.0 (East is positive) inclusive. This option will be ignored if outside that range, if it is not a number, if geo_enabled is disabled, or if there not a corresponding :lat option.

  • :place (Twitter::Place)

    A place in the world. These can be retrieved from PlacesAndGeo#reverse_geocode.

  • :place_id (String)

    A place in the world. These IDs can be retrieved from PlacesAndGeo#reverse_geocode.

  • :display_coordinates (String)

    Whether or not to put a pin on the exact coordinates a tweet has been sent from.

  • :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:

Rate Limited?:

  • No

Authentication:

  • Requires user context


226
227
228
229
230
231
232
# File 'lib/twitter/rest/tweets.rb', line 226

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