Module: Twitter::API::Tweets

Includes:
Utils
Included in:
Client
Defined in:
lib/twitter/api/tweets.rb

Constant Summary

Constants included from Utils

Utils::DEFAULT_CURSOR

Instance Method Summary collapse

Instance Method Details

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

Returns oEmbed for a Tweet

Examples:

Return oEmbeds for Tweet with the ID 25938088801

Twitter.status_with_activity(25938088801)

Parameters:

  • id (Integer, String)

    A Tweet ID.

  • 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



226
227
228
# File 'lib/twitter/api/tweets.rb', line 226

def oembed(id, options={})
  object_from_response(Twitter::OEmbed, :get, "/1.1/statuses/oembed.json?id=#{id}", options)
end

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

Returns oEmbeds for Tweets

Overloads:

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

    Examples:

    Return oEmbeds for Tweets with the ID 25938088801

    Twitter.status_with_activity(25938088801)

    Parameters:

    • ids_or_urls (Array<Integer, String>, Set<Integer, String>)

      An array of Tweet IDs or URLs.

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

    Parameters:

    • ids_or_urls (Array<Integer, String>, Set<Integer, String>)

      An array of Tweet IDs or URLs.

    • 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



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

def oembeds(*args)
  arguments = Twitter::API::Arguments.new(args)
  arguments.flatten.threaded_map do |id|
    object_from_response(Twitter::OEmbed, :get, "/1.1/statuses/oembed.json?id=#{id}", arguments.options)
  end
end

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

Retweets the specified Tweets as the authenticating user

Overloads:

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

    Examples:

    Retweet the Tweet with the ID 28561922516

    Twitter.retweet(28561922516)

    Parameters:

    • ids (Array<Integer>, Set<Integer>)

      An array of Tweet IDs.

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

    Parameters:

    • ids (Array<Integer>, Set<Integer>)

      An array of Tweet IDs.

    • 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



147
148
149
150
151
152
153
154
155
156
# File 'lib/twitter/api/tweets.rb', line 147

def retweet(*args)
  arguments = Twitter::API::Arguments.new(args)
  arguments.flatten.threaded_map do |id|
    begin
      post_retweet(id, arguments.options)
    rescue Twitter::Error::Forbidden => error
      raise unless error.message == Twitter::Error::AlreadyRetweeted::MESSAGE
    end
  end.compact
end

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

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

Overloads:

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

    Examples:

    Retweet the Tweet with the ID 28561922516

    Twitter.retweet!(28561922516)

    Parameters:

    • ids (Array<Integer>, Set<Integer>)

      An array of Tweet IDs.

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

    Parameters:

    • ids (Array<Integer>, Set<Integer>)

      An array of Tweet IDs.

    • 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/api/tweets.rb', line 174

def retweet!(*args)
  arguments = Twitter::API::Arguments.new(args)
  arguments.flatten.threaded_map do |id|
    begin
      post_retweet(id, arguments.options)
    rescue Twitter::Error::Forbidden => error
      handle_forbidden_error(Twitter::Error::AlreadyRetweeted, error)
    end
  end.compact
end

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

Show up to 100 users who retweeted the Tweet

Examples:

Show up to 100 users who retweeted the Tweet with the ID 28561922516

Twitter.retweeters_of(28561922516)

Parameters:

  • id (Integer)

    The numerical ID of the desired Tweet.

  • 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



44
45
46
47
48
49
50
51
52
# File 'lib/twitter/api/tweets.rb', line 44

def retweeters_of(id, options={})
  ids_only = !!options.delete(:ids_only)
  retweeters = retweets(id, options).map(&:user)
  if ids_only
    retweeters.map(&:id)
  else
    retweeters
  end
end

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

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

Examples:

Return up to 100 of the first retweets of the Tweet with the ID 28561922516

Twitter.retweets(28561922516)

Parameters:

  • id (Integer)

    The numerical ID of the desired Tweet.

  • 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



26
27
28
# File 'lib/twitter/api/tweets.rb', line 26

def retweets(id, options={})
  objects_from_response(Twitter::Tweet, :get, "/1.1/statuses/retweets/#{id}.json", options)
end

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

Returns a Tweet

Examples:

Return the Tweet with the ID 25938088801

Twitter.status(25938088801)

Parameters:

  • id (Integer)

    A Tweet ID.

  • 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



66
67
68
# File 'lib/twitter/api/tweets.rb', line 66

def status(id, options={})
  object_from_response(Twitter::Tweet, :get, "/1.1/statuses/show/#{id}.json", options)
end

#status_destroy(*ids) ⇒ Array<Twitter::Tweet> #status_destroy(*ids, options) ⇒ Array<Twitter::Tweet> Also known as: tweet_destroy

Note:

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

Destroys the specified Tweets

Overloads:

  • #status_destroy(*ids) ⇒ Array<Twitter::Tweet>

    Examples:

    Destroy the Tweet with the ID 25938088801

    Twitter.status_destroy(25938088801)

    Parameters:

    • ids (Array<Integer>, Set<Integer>)

      An array of Tweet IDs.

  • #status_destroy(*ids, options) ⇒ Array<Twitter::Tweet>

    Parameters:

    • ids (Array<Integer>, Set<Integer>)

      An array of Tweet IDs.

    • 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



105
106
107
# File 'lib/twitter/api/tweets.rb', line 105

def status_destroy(*args)
  threaded_tweets_from_response(:post, "/1.1/statuses/destroy", args)
end

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

Returns Tweets

Overloads:

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

    Examples:

    Return the Tweet with the ID 25938088801

    Twitter.statuses(25938088801)

    Parameters:

    • ids (Array<Integer>, Set<Integer>)

      An array of Tweet IDs.

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

    Parameters:

    • ids (Array<Integer>, Set<Integer>)

      An array of Tweet IDs.

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

  • Yes

Authentication:

  • Requires user context



85
86
87
# File 'lib/twitter/api/tweets.rb', line 85

def statuses(*args)
  threaded_tweets_from_response(:get, "/1.1/statuses/show", args)
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

Examples:

Update the authenticating user's status

Twitter.update("I'm tweeting with @gem!")

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

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



128
129
130
# File 'lib/twitter/api/tweets.rb', line 128

def update(status, options={})
  object_from_response(Twitter::Tweet, :post, "/1.1/statuses/update.json", options.merge(:status => status))
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

Examples:

Update the authenticating user's status

Twitter.update_with_media("I'm tweeting with @gem!", File.new('my_awesome_pic.jpeg'))

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

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



204
205
206
# File 'lib/twitter/api/tweets.rb', line 204

def update_with_media(status, media, options={})
  object_from_response(Twitter::Tweet, :post, "/1.1/statuses/update_with_media.json", options.merge('media[]' => media, 'status' => status))
end