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

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

Note:

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

Destroys the specified Tweets

Overloads:

  • - (Array<Twitter::Tweet>) destroy_status(*tweets, 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.

Raises:

See Also:

Rate Limited?:

  • No

Authentication:

  • Requires user context



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

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

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

Returns oEmbed for a Tweet

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.

Raises:

See Also:

Rate Limited?:

  • Yes

Authentication:

  • Requires user context



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

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

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

Returns oEmbeds for Tweets

Overloads:

  • - (Array<Twitter::OEmbed>) oembed(*tweets, 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.

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

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

Retweets the specified Tweets as the authenticating user

Overloads:

  • - (Array<Twitter::Tweet>) retweet(*tweets, 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.

Raises:

See Also:

Rate Limited?:

  • Yes

Authentication:

  • Requires user context



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

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

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

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

Overloads:

  • - (Array<Twitter::Tweet>) retweet!(*tweets, 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.

Raises:

See Also:

Rate Limited?:

  • Yes

Authentication:

  • Requires user context



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

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

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

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

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_with_cursor(:get, '/1.1/statuses/retweeters/ids.json', arguments.options, :ids)
end

- (Array) retweeters_of(tweet, options = {})

Show up to 100 users who retweeted the Tweet

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.

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

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

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

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.

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_with_objects(:get, "/1.1/statuses/retweets/#{extract_id(tweet)}.json", options, Twitter::Tweet)
end

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

Returns a Tweet

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.

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_with_object(:get, "/1.1/statuses/show/#{extract_id(tweet)}.json", options, Twitter::Tweet)
end

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

Returns Tweets

Overloads:

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

    Options Hash (options):

    • :method (Symbol, String)

      Requests users via a GET request instead of the standard POST request if set to ':get'.

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

See Also:

Rate Limited?:

  • Yes

Authentication:

  • Required



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

def statuses(*args)
  arguments = Twitter::Arguments.new(args)
  request_method = arguments.options.delete(:method) || :post
  flat_pmap(arguments.each_slice(MAX_TWEETS_PER_REQUEST)) do |tweets|
    perform_with_objects(request_method, '/1.1/statuses/lookup.json', arguments.options.merge(:id => tweets.collect { |u| extract_id(u) }.join(',')), Twitter::Tweet)
  end
end

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

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

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.

Raises:

See Also:

Rate Limited?:

  • No

Authentication:

  • Requires user context



129
130
131
132
133
# File 'lib/twitter/rest/tweets.rb', line 129

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

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

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

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.

Raises:

See Also:

Rate Limited?:

  • No

Authentication:

  • Requires user context



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

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_with_object(:post, '/1.1/statuses/update.json', hash.merge(:status => status), Twitter::Tweet)
end

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

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

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.

Raises:

See Also:

Rate Limited?:

  • No

Authentication:

  • Requires user context



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

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_with_object(:post, '/1.1/statuses/update_with_media.json', hash.merge('media[]' => media, 'status' => status), Twitter::Tweet)
end