Module: Twitter::REST::Users

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

Constant Summary collapse

MAX_USERS_PER_REQUEST =
100

Constants included from Utils

Twitter::REST::Utils::DEFAULT_CURSOR

Instance Method Summary collapse

Methods included from Utils

flat_pmap, pmap

Instance Method Details

#block(*users) ⇒ Array<Twitter::User> #block(*users, options) ⇒ Array<Twitter::User>

Note:

Destroys a friendship to the blocked user if it exists.

Blocks the users specified by the authenticating user

Overloads:

  • #block(*users) ⇒ Array<Twitter::User>

    Parameters:

    • users (Enumerable<Integer, String, Twitter::User>)

      A collection of Twitter user IDs, screen names, or objects.

  • #block(*users, options) ⇒ Array<Twitter::User>

    Parameters:

    • users (Enumerable<Integer, String, Twitter::User>)

      A collection of Twitter user IDs, screen names, or objects.

    • options (Hash)

      A customizable set of options.

Returns:

Raises:

See Also:

Rate Limited?:

  • Yes

Authentication:

  • Requires user context



176
177
178
# File 'lib/twitter/rest/users.rb', line 176

def block(*args)
  parallel_users_from_response(:post, "/1.1/blocks/create.json", args)
end

#block?(user, options = {}) ⇒ Boolean

Returns true if the authenticating user is blocking a target user

Parameters:

  • user (Integer, String, URI, Twitter::User)

    A Twitter user ID, screen name, URI, or object.

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

    A customizable set of options.

Returns:

  • (Boolean)

    true if the authenticating user is blocking the target user, otherwise false.

Raises:

See Also:

Rate Limited?:

  • Yes

Authentication:

  • Requires user context



153
154
155
156
157
158
159
160
161
# File 'lib/twitter/rest/users.rb', line 153

def block?(user, options = {})
  user_id =
    case user
    when Integer                       then user
    when String, URI, Addressable::URI then user(user).id
    when Twitter::User                 then user.id
    end
  blocked_ids(options).collect(&:to_i).include?(user_id)
end

#blocked(options = {}) ⇒ Array<Twitter::User>

Returns an array of user objects that the authenticating user is blocking

Parameters:

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

    A customizable set of options.

Options Hash (options):

  • :skip_status (Boolean, String, Integer)

    Do not include user's Tweets when set to true, 't' or 1.

Returns:

  • (Array<Twitter::User>)

    User objects that the authenticating user is blocking.

Raises:

See Also:

Rate Limited?:

  • Yes

Authentication:

  • Requires user context



125
126
127
# File 'lib/twitter/rest/users.rb', line 125

def blocked(options = {})
  perform_get_with_cursor("/1.1/blocks/list.json", options, :users, Twitter::User)
end

#blocked_ids(options = {}) ⇒ Twitter::Cursor

Returns an array of numeric user IDs the authenticating user is blocking

Parameters:

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

    A customizable set of options.

Returns:

  • (Twitter::Cursor)

    Numeric user IDs the authenticating user is blocking.

Raises:

See Also:

Rate Limited?:

  • Yes

Authentication:

  • Requires user context



138
139
140
141
142
# File 'lib/twitter/rest/users.rb', line 138

def blocked_ids(*args)
  arguments = Twitter::Arguments.new(args)
  merge_user!(arguments.options, arguments.pop)
  perform_get_with_cursor("/1.1/blocks/ids.json", arguments.options, :ids)
end

#contributees(options = {}) ⇒ Array<Twitter::User> #contributees(user, options = {}) ⇒ Array<Twitter::User>

Returns an array of users that the specified user can contribute to

Overloads:

  • #contributees(options = {}) ⇒ Array<Twitter::User>

    Parameters:

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

      A customizable set of options.

    Options Hash (options):

    • :skip_status (Boolean, String, Integer)

      Do not include contributee's Tweets when set to true, 't' or 1.

  • #contributees(user, options = {}) ⇒ Array<Twitter::User>

    Parameters:

    • user (Integer, String, Twitter::User)

      A Twitter user ID, screen name, URI, or object.

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

      A customizable set of options.

    Options Hash (options):

    • :skip_status (Boolean, String, Integer)

      Do not include contributee's Tweets when set to true, 't' or 1.

Returns:

Raises:

See Also:

Rate Limited?:

  • Yes

Authentication:

  • Requires user context



287
288
289
# File 'lib/twitter/rest/users.rb', line 287

def contributees(*args)
  users_from_response(:get, "/1.1/users/contributees.json", args)
end

#contributors(options = {}) ⇒ Array<Twitter::User> #contributors(user, options = {}) ⇒ Array<Twitter::User>

Returns an array of users who can contribute to the specified account

Overloads:

  • #contributors(options = {}) ⇒ Array<Twitter::User>

    Parameters:

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

      A customizable set of options.

    Options Hash (options):

    • :skip_status (Boolean, String, Integer)

      Do not include contributee's Tweets when set to true, 't' or 1.

  • #contributors(user, options = {}) ⇒ Array<Twitter::User>

    Parameters:

    • user (Integer, String, Twitter::User)

      A Twitter user ID, screen name, URI, or object.

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

      A customizable set of options.

    Options Hash (options):

    • :skip_status (Boolean, String, Integer)

      Do not include contributee's Tweets when set to true, 't' or 1.

Returns:

Raises:

See Also:

Rate Limited?:

  • Yes

Authentication:

  • Requires user context



305
306
307
# File 'lib/twitter/rest/users.rb', line 305

def contributors(*args)
  users_from_response(:get, "/1.1/users/contributors.json", args)
end

#mute(*users) ⇒ Array<Twitter::User> #mute(*users, options) ⇒ Array<Twitter::User>

Mutes the users specified by the authenticating user

Overloads:

  • #mute(*users) ⇒ Array<Twitter::User>

    Parameters:

    • users (Enumerable<Integer, String, Twitter::User>)

      A collection of Twitter user IDs, screen names, or objects.

  • #mute(*users, options) ⇒ Array<Twitter::User>

    Parameters:

    • users (Enumerable<Integer, String, Twitter::User>)

      A collection of Twitter user IDs, screen names, or objects.

    • options (Hash)

      A customizable set of options.

Returns:

Raises:

See Also:

Rate Limited?:

  • Yes

Authentication:

  • Requires user context



373
374
375
# File 'lib/twitter/rest/users.rb', line 373

def mute(*args)
  parallel_users_from_response(:post, "/1.1/mutes/users/create.json", args)
end

#muted(options = {}) ⇒ Array<Twitter::User>

Returns an array of user objects that the authenticating user is muting

Parameters:

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

    A customizable set of options.

Options Hash (options):

  • :skip_status (Boolean, String, Integer)

    Do not include user's Tweets when set to true, 't' or 1.

Returns:

  • (Array<Twitter::User>)

    User objects that the authenticating user is muting.

Raises:

See Also:

Rate Limited?:

  • Yes

Authentication:

  • Requires user context



402
403
404
# File 'lib/twitter/rest/users.rb', line 402

def muted(options = {})
  perform_get_with_cursor("/1.1/mutes/users/list.json", options, :users, Twitter::User)
end

#muted_ids(options = {}) ⇒ Twitter::Cursor

Returns an array of numeric user IDs the authenticating user is muting

Parameters:

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

    A customizable set of options.

Returns:

Raises:

See Also:

Rate Limited?:

  • Yes

Authentication:

  • Requires user context



415
416
417
418
419
# File 'lib/twitter/rest/users.rb', line 415

def muted_ids(*args)
  arguments = Twitter::Arguments.new(args)
  merge_user!(arguments.options, arguments.pop)
  perform_get_with_cursor("/1.1/mutes/users/ids.json", arguments.options, :ids)
end

#profile_banner(options = {}) ⇒ Twitter::ProfileBanner #profile_banner(user, options = {}) ⇒ Twitter::ProfileBanner

Note:

If the user has not uploaded a profile banner, a HTTP 404 will be served instead.

Returns the available size variations of the specified user's profile banner.

Overloads:

Returns:

Raises:

See Also:

Rate Limited?:

  • Yes

Authentication:

  • Requires user context



355
356
357
358
359
# File 'lib/twitter/rest/users.rb', line 355

def profile_banner(*args)
  arguments = Twitter::Arguments.new(args)
  merge_user!(arguments.options, arguments.pop || user_id) unless arguments.options[:user_id] || arguments.options[:screen_name]
  perform_get_with_object("/1.1/users/profile_banner.json", arguments.options, Twitter::ProfileBanner)
end

#remove_profile_banner(options = {}) ⇒ nil

Removes the authenticating user's profile banner image

Parameters:

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

    A customizable set of options.

Returns:

  • (nil)

Raises:

See Also:

Rate Limited?:

  • No

Authentication:

  • Requires user context



317
318
319
320
# File 'lib/twitter/rest/users.rb', line 317

def remove_profile_banner(options = {})
  perform_post("/1.1/account/remove_profile_banner.json", options)
  true
end

#settings(options = {}) ⇒ Twitter::Settings

Updates the authenticating user's settings. Or, if no options supplied, returns settings (including current trend, geo and sleep time information) for the authenticating user.

Parameters:

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

    A customizable set of options.

Options Hash (options):

  • :trend_location_woeid (Integer)

    The Yahoo! Where On Earth ID to use as the user's default trend location. Global information is available by using 1 as the WOEID. The woeid must be one of the locations returned by GET trends/available.

  • :sleep_time_enabled (Boolean, String, Integer)

    When set to true, 't' or 1, will enable sleep time for the user. Sleep time is the time when push or SMS notifications should not be sent to the user.

  • :start_sleep_time (Integer)

    The hour that sleep time should begin if it is enabled. The value for this parameter should be provided in ISO8601 format (i.e. 00-23). The time is considered to be in the same timezone as the user's time_zone setting.

  • :end_sleep_time (Integer)

    The hour that sleep time should end if it is enabled. The value for this parameter should be provided in ISO8601 format (i.e. 00-23). The time is considered to be in the same timezone as the user's time_zone setting.

  • :time_zone (String)

    The timezone dates and times should be displayed in for the user. The timezone must be one of the Rails TimeZone names.

  • :lang (String)

    The language which Twitter should render in for this user. The language must be specified by the appropriate two letter ISO 639-1 representation. Currently supported languages are provided by GET help/languages.

  • :allow_contributor_request (String)

    Whether to allow others to include user as contributor. Possible values include 'all' (anyone can include user), 'following' (only followers can include user) or 'none'. Also note that changes to this field require the request also include a current_password value with the user's password to successfully modify this field.

  • :current_password (String)

    The user's password. This is only required when modifying the allow_contributor_request field.

Returns:

Raises:

See Also:

Rate Limited?:

  • Yes

Authentication:

  • Requires user context



35
36
37
38
39
40
41
# File 'lib/twitter/rest/users.rb', line 35

def settings(options = {})
  request_method = options.empty? ? :get : :post
  response = perform_request(request_method.to_sym, "/1.1/account/settings.json", options)
  # https://dev.twitter.com/issues/59
  response[:trend_location] = response.fetch(:trend_location, []).first
  Twitter::Settings.new(response)
end

#unblock(*users) ⇒ Array<Twitter::User> #unblock(*users, options) ⇒ Array<Twitter::User>

Un-blocks the users specified by the authenticating user

Overloads:

  • #unblock(*users) ⇒ Array<Twitter::User>

    Parameters:

    • users (Enumerable<Integer, String, Twitter::User>)

      A collection of Twitter user IDs, screen names, or objects.

  • #unblock(*users, options) ⇒ Array<Twitter::User>

    Parameters:

    • users (Enumerable<Integer, String, Twitter::User>)

      A collection of Twitter user IDs, screen names, or objects.

    • options (Hash)

      A customizable set of options.

Returns:

Raises:

See Also:

Rate Limited?:

  • No

Authentication:

  • Requires user context



192
193
194
# File 'lib/twitter/rest/users.rb', line 192

def unblock(*args)
  parallel_users_from_response(:post, "/1.1/blocks/destroy.json", args)
end

#unmute(*users) ⇒ Array<Twitter::User> #unmute(*users, options) ⇒ Array<Twitter::User>

Un-mutes the user specified by the authenticating user.

Overloads:

  • #unmute(*users) ⇒ Array<Twitter::User>

    Parameters:

    • users (Enumerable<Integer, String, Twitter::User>)

      A collection of Twitter user IDs, screen names, or objects.

  • #unmute(*users, options) ⇒ Array<Twitter::User>

    Parameters:

    • users (Enumerable<Integer, String, Twitter::User>)

      A collection of Twitter user IDs, screen names, or objects.

    • options (Hash)

      A customizable set of options.

Returns:

Raises:

See Also:

Rate Limited?:

  • Yes

Authentication:

  • Requires user context



389
390
391
# File 'lib/twitter/rest/users.rb', line 389

def unmute(*args)
  parallel_users_from_response(:post, "/1.1/mutes/users/destroy.json", args)
end

#update_delivery_device(device, options = {}) ⇒ Twitter::User

Sets which device Twitter delivers updates to for the authenticating user

Parameters:

  • device (String)

    Must be one of: 'sms', 'none'.

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

    A customizable set of options.

Returns:

Raises:

See Also:

Rate Limited?:

  • No

Authentication:

  • Requires user context



65
66
67
# File 'lib/twitter/rest/users.rb', line 65

def update_delivery_device(device, options = {})
  perform_post_with_object("/1.1/account/update_delivery_device.json", options.merge(device:), Twitter::User)
end

#update_profile(options = {}) ⇒ Twitter::User

Note:

Only the options specified will be updated.

Sets values that users are able to set under the "Account" tab of their settings page

Parameters:

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

    A customizable set of options.

Options Hash (options):

  • :name (String)

    Full name associated with the profile. Maximum of 20 characters.

  • :url (String)

    URL associated with the profile. Will be prepended with "http://" if not present. Maximum of 100 characters.

  • :location (String)

    The city or country describing where the user of the account is located. The contents are not normalized or geocoded in any way. Maximum of 30 characters.

  • :description (String)

    A description of the user owning the account. Maximum of 160 characters.

  • :profile_link_color (String)

    A hex value of the color scheme used for links on user's profile page. Must be a valid hexadecimal value, and may be either three or six characters

Returns:

Raises:

See Also:

Rate Limited?:

  • No

Authentication:

  • Requires user context



83
84
85
# File 'lib/twitter/rest/users.rb', line 83

def update_profile(options = {})
  perform_post_with_object("/1.1/account/update_profile.json", options, Twitter::User)
end

#update_profile_background_image(image, options = {}) ⇒ Twitter::User

Updates the authenticating user's profile background image

Parameters:

  • image (File)

    The background image for the profile, base64-encoded. Must be a valid GIF, JPG, or PNG image of less than 800 kilobytes in size. Images with width larger than 2048 pixels will be forcibly scaled down. The image must be provided as raw multipart data, not a URL.

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

    A customizable set of options.

Options Hash (options):

  • :tile (Boolean)

    Whether or not to tile the background image. If set to true the background image will be displayed tiled. The image will not be tiled otherwise.

Returns:

Raises:

See Also:

Rate Limited?:

  • No

Authentication:

  • Requires user context



97
98
99
# File 'lib/twitter/rest/users.rb', line 97

def update_profile_background_image(image, options = {})
  post_profile_image("/1.1/account/update_profile_background_image.json", image, options)
end

#update_profile_banner(banner, options = {}) ⇒ nil

Note:

Uploads a profile banner on behalf of the authenticating user. For best results, upload an <5MB image that is exactly 1252px by 626px. Images will be resized for a number of display options. Users with an uploaded profile banner will have a profile_banner_url node in their Users objects. More information about sizing variations can be found in User Profile Images and Banners.

Note:

Profile banner images are processed asynchronously. The profile_banner_url and its variant sizes will not necessary be available directly after upload.

Updates the authenticating user's profile banner image

Parameters:

  • banner (File)

    The Base64-encoded or raw image data being uploaded as the user's new profile banner.

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

    A customizable set of options.

Options Hash (options):

  • :width (Integer)

    The width of the preferred section of the image being uploaded in pixels. Use with height, offset_left, and offset_top to select the desired region of the image to use.

  • :height (Integer)

    The height of the preferred section of the image being uploaded in pixels. Use with width, offset_left, and offset_top to select the desired region of the image to use.

  • :offset_left (Integer)

    The number of pixels by which to offset the uploaded image from the left. Use with height, width, and offset_top to select the desired region of the image to use.

  • :offset_top (Integer)

    The number of pixels by which to offset the uploaded image from the top. Use with height, width, and offset_left to select the desired region of the image to use.

Returns:

  • (nil)

Raises:

See Also:

Rate Limited?:

  • No

Authentication:

  • Requires user context



339
340
341
342
# File 'lib/twitter/rest/users.rb', line 339

def update_profile_banner(banner, options = {})
  perform_post("/1.1/account/update_profile_banner.json", options.merge(banner:))
  true
end

#update_profile_image(image, options = {}) ⇒ Twitter::User

Note:

Updates the authenticating user's profile image. Note that this method expects raw multipart data, not a URL to an image.

Note:

This method asynchronously processes the uploaded file before updating the user's profile image URL. You can either update your local cache the next time you request the user's information, or, at least 5 seconds after uploading the image, ask for the updated URL using GET users/show.

Updates the authenticating user's profile image

Parameters:

  • image (File)

    The avatar image for the profile, base64-encoded. Must be a valid GIF, JPG, or PNG image of less than 700 kilobytes in size. Images with width larger than 500 pixels will be scaled down. Animated GIFs will be converted to a static GIF of the first frame, removing the animation.

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

    A customizable set of options.

Returns:

Raises:

See Also:

Rate Limited?:

  • No

Authentication:

  • Requires user context



112
113
114
# File 'lib/twitter/rest/users.rb', line 112

def update_profile_image(image, options = {})
  post_profile_image("/1.1/account/update_profile_image.json", image, options)
end

#user(options = {}) ⇒ Twitter::User #user(user, options = {}) ⇒ Twitter::User

Returns The requested user.

Overloads:

  • #user(options = {}) ⇒ Twitter::User

    Returns extended information for the authenticated user

    Parameters:

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

      A customizable set of options.

    Options Hash (options):

    • :skip_status (Boolean, String, Integer)

      Do not include user's Tweets when set to true, 't' or 1.

  • #user(user, options = {}) ⇒ Twitter::User

    Returns extended information for a given user

    Parameters:

    • user (Integer, String, Twitter::User)

      A Twitter user ID, screen name, URI, or object.

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

      A customizable set of options.

    Options Hash (options):

    • :skip_status (Boolean, String, Integer)

      Do not include user's Tweets when set to true, 't' or 1.

Returns:

Raises:

See Also:

Rate Limited?:

  • Yes

Authentication:

  • Requires user context



231
232
233
234
235
236
237
238
239
# File 'lib/twitter/rest/users.rb', line 231

def user(*args)
  arguments = Twitter::Arguments.new(args)
  if arguments.last || user_id?
    merge_user!(arguments.options, arguments.pop || user_id)
    perform_get_with_object("/1.1/users/show.json", arguments.options, Twitter::User)
  else
    verify_credentials(arguments.options)
  end
end

#user?(user, options = {}) ⇒ Boolean

Returns true if the specified user exists

Parameters:

  • user (Integer, String, Twitter::User)

    A Twitter user ID, screen name, URI, or object.

Returns:

  • (Boolean)

    true if the user exists, otherwise false.

Raises:

Rate Limited?:

  • Yes

Authentication:

  • Requires user context



248
249
250
251
252
253
254
255
# File 'lib/twitter/rest/users.rb', line 248

def user?(user, options = {})
  options = options.dup
  merge_user!(options, user)
  perform_get("/1.1/users/show.json", options)
  true
rescue Twitter::Error::NotFound
  false
end

#user_search(query, options = {}) ⇒ Array<Twitter::User>

Returns users that match the given query

Parameters:

  • query (String)

    The search query to run against people search.

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

    A customizable set of options.

Options Hash (options):

  • :count (Integer)

    The number of people to retrieve. Maxiumum of 20 allowed per page.

  • :page (Integer)

    Specifies the page of results to retrieve.

Returns:

Raises:

See Also:

Rate Limited?:

  • Yes

Authentication:

  • Requires user context



268
269
270
271
# File 'lib/twitter/rest/users.rb', line 268

def user_search(query, options = {})
  options = options.dup
  perform_get_with_objects("/1.1/users/search.json", options.merge(q: query), Twitter::User)
end

#users(*users) ⇒ Array<Twitter::User> #users(*users, options) ⇒ Array<Twitter::User>

Returns extended information for up to 100 users

Overloads:

  • #users(*users) ⇒ Array<Twitter::User>

    Parameters:

    • users (Enumerable<Integer, String, Twitter::User>)

      A collection of Twitter user IDs, screen names, or objects.

  • #users(*users, options) ⇒ Array<Twitter::User>

    Parameters:

    • users (Enumerable<Integer, String, Twitter::User>)

      A collection of Twitter user IDs, screen names, or objects.

    • options (Hash)

      A customizable set of options.

Returns:

Raises:

See Also:

Rate Limited?:

  • Yes

Authentication:

  • Required



208
209
210
211
212
213
# File 'lib/twitter/rest/users.rb', line 208

def users(*args)
  arguments = Twitter::Arguments.new(args)
  flat_pmap(arguments.each_slice(MAX_USERS_PER_REQUEST)) do |users|
    perform_get_with_objects("/1.1/users/lookup.json", merge_users(arguments.options, users), Twitter::User)
  end
end

#verify_credentials(options = {}) ⇒ Twitter::User

Returns the requesting user if authentication was successful, otherwise raises Error::Unauthorized

Parameters:

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

    A customizable set of options.

Options Hash (options):

  • :skip_status (Boolean, String, Integer)

    Do not include user's Tweets when set to true, 't' or 1.

Returns:

Raises:

See Also:

Rate Limited?:

  • Yes

Authentication:

  • Requires user context



52
53
54
# File 'lib/twitter/rest/users.rb', line 52

def verify_credentials(options = {})
  perform_get_with_object("/1.1/account/verify_credentials.json", options, Twitter::User)
end