Module: Twitter::REST::Tweets

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

Constant Summary

MAX_TWEETS_PER_REQUEST =
100

Constants included from Utils

Utils::DEFAULT_CURSOR, Utils::URI_SUBSTRING

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



97
98
99
100
101
102
# File 'lib/twitter/rest/tweets.rb', line 97

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.

Returns:

Raises:

See Also:

Rate Limited?:

  • Yes

Authentication:

  • Requires user context



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

def oembed(tweet, options = {})
  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



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

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



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

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



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

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



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

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



43
44
45
46
47
# File 'lib/twitter/rest/tweets.rb', line 43

def retweeters_of(tweet, options = {})
  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



27
28
29
# File 'lib/twitter/rest/tweets.rb', line 27

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



60
61
62
# File 'lib/twitter/rest/tweets.rb', line 60

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



76
77
78
79
80
81
# File 'lib/twitter/rest/tweets.rb', line 76

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

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

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

  • :media_ids (String)

    A comma separated list of uploaded media IDs to attach to the Tweet.

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



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

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

  • :media_ids (String)

    A comma separated list of uploaded media IDs to attach to the Tweet.

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



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

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

  • media (File, Hash)

    A File object with your picture (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



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

def update_with_media(status, media, options = {})
  fail(Twitter::Error::UnacceptableIO.new) unless media.respond_to?(:to_io)
  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_with_media.json', hash.merge('media[]' => media, 'status' => status), Twitter::Tweet)
end