Module: Twitter::REST::Users

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

Overview

Methods for working with Twitter users

Constant Summary collapse

MAX_USERS_PER_REQUEST =

Maximum 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

Examples:

client.block('sferik')

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:



207
208
209
# File 'lib/twitter/rest/users.rb', line 207

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

Examples:

client.block?('sferik')

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.

Raises:

See Also:



181
182
183
184
185
186
187
188
189
# File 'lib/twitter/rest/users.rb', line 181

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

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

Returns users that the authenticating user is blocking

Examples:

client.blocked

Parameters:

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

    A customizable set of options.

Options Hash (options):

  • :skip_status (Boolean, String, Integer)

    Do not include user’s Tweets.

Returns:

  • (Array<Twitter::User>)

    User objects that the authenticating user is blocking.

Raises:

See Also:



147
148
149
# File 'lib/twitter/rest/users.rb', line 147

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

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

Returns user IDs the authenticating user is blocking

Examples:

client.blocked_ids

Parameters:

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

    A customizable set of options.

Returns:

  • (Twitter::Cursor)

    Numeric user IDs the authenticating user is blocking.

Raises:

See Also:



163
164
165
166
167
# File 'lib/twitter/rest/users.rb', line 163

def blocked_ids(*args)
  arguments = 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 users that the specified user can contribute to

Examples:

client.contributees

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:



337
338
339
# File 'lib/twitter/rest/users.rb', line 337

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 users who can contribute to the specified account

Examples:

client.contributors

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:



358
359
360
# File 'lib/twitter/rest/users.rb', line 358

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

Examples:

client.mute('sferik')

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:



439
440
441
# File 'lib/twitter/rest/users.rb', line 439

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

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

Returns users that the authenticating user is muting

Examples:

client.muted

Parameters:

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

    A customizable set of options.

Options Hash (options):

  • :skip_status (Boolean, String, Integer)

    Do not include user’s Tweets.

Returns:

  • (Array<Twitter::User>)

    User objects that the authenticating user is muting.

Raises:

See Also:



474
475
476
# File 'lib/twitter/rest/users.rb', line 474

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

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

Returns user IDs the authenticating user is muting

Examples:

client.muted_ids

Parameters:

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

    A customizable set of options.

Returns:

Raises:

See Also:



490
491
492
493
494
# File 'lib/twitter/rest/users.rb', line 490

def muted_ids(*args)
  arguments = 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 is served.

Returns the profile banner size variations for a user

Examples:

client.profile_banner('sferik')

Overloads:

Returns:

Raises:

See Also:



418
419
420
421
422
# File 'lib/twitter/rest/users.rb', line 418

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

#remove_profile_banner(options = {}) ⇒ nil

Removes the authenticating user’s profile banner image

rubocop:disable Naming/PredicateMethod

Examples:

client.remove_profile_banner

Parameters:

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

    A customizable set of options.

Returns:

  • (nil)

Raises:

See Also:



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

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

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

Updates or returns the authenticating user’s settings

Examples:

client.settings

Parameters:

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

    A customizable set of options.

Options Hash (options):

  • :trend_location_woeid (Integer)

    The Yahoo! Where On Earth ID.

  • :sleep_time_enabled (Boolean, String, Integer)

    Enable sleep time.

  • :start_sleep_time (Integer)

    The hour sleep time should begin.

  • :end_sleep_time (Integer)

    The hour sleep time should end.

  • :time_zone (String)

    The timezone for the user.

  • :lang (String)

    The language which Twitter should render in.

  • :allow_contributor_request (String)

    Allow others to include user.

  • :current_password (String)

    The user’s password.

Returns:

Raises:

See Also:



39
40
41
42
43
44
45
46
# File 'lib/twitter/rest/users.rb', line 39

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

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

Un-blocks the users specified by the authenticating user

Examples:

client.unblock('sferik')

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:



226
227
228
# File 'lib/twitter/rest/users.rb', line 226

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

Examples:

client.unmute('sferik')

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:



458
459
460
# File 'lib/twitter/rest/users.rb', line 458

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

Examples:

client.update_delivery_device('sms')

Parameters:

  • device (String)

    Must be one of: ‘sms’, ‘none’.

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

    A customizable set of options.

Returns:

Raises:

See Also:



76
77
78
# File 'lib/twitter/rest/users.rb', line 76

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

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

Note:

Only the options specified will be updated.

Sets values that users can set in their profile settings

Examples:

client.update_profile(name: 'Erik Michaels-Ober')

Parameters:

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

    A customizable set of options.

Options Hash (options):

  • :name (String)

    Full name associated with the profile.

  • :url (String)

    URL associated with the profile.

  • :location (String)

    The city or country describing the user’s location.

  • :description (String)

    A description of the user.

  • :profile_link_color (String)

    A hex value for link color.

Returns:

Raises:

See Also:



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

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

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

Updates the authenticating user’s profile background image

Examples:

client.update_profile_background_image(File.new('/path/to/image.png'))

Parameters:

  • image (File)

    The background image for the profile.

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

    A customizable set of options.

Options Hash (options):

  • :tile (Boolean)

    Whether or not to tile the background image.

Returns:

Raises:

See Also:



114
115
116
# File 'lib/twitter/rest/users.rb', line 114

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:

For best results, upload an image that is exactly 1252px by 626px.

Updates the authenticating user’s profile banner image

Examples:

client.update_profile_banner(File.new('/path/to/banner.png'))

Parameters:

  • banner (File)

    The image data being uploaded as the profile banner.

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

    A customizable set of options.

Options Hash (options):

  • :width (Integer)

    The width of the preferred section.

  • :height (Integer)

    The height of the preferred section.

  • :offset_left (Integer)

    The offset from the left.

  • :offset_top (Integer)

    The offset from the top.

Returns:

  • (nil)

Raises:

See Also:



398
399
400
401
# File 'lib/twitter/rest/users.rb', line 398

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:

This method expects raw multipart data, not a URL to an image.

Updates the authenticating user’s profile image

Examples:

client.update_profile_image(File.new('/path/to/avatar.png'))

Parameters:

  • image (File)

    The avatar image for the profile.

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

    A customizable set of options.

Returns:

Raises:

See Also:



131
132
133
# File 'lib/twitter/rest/users.rb', line 131

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 extended information for a given user

Examples:

client.user('sferik')

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:



273
274
275
276
277
278
279
280
281
# File 'lib/twitter/rest/users.rb', line 273

def user(*args)
  arguments = 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, User)
  else
    verify_credentials(arguments.options)
  end
end

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

Returns true if the specified user exists

Examples:

client.user?('sferik')

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:



293
294
295
296
297
298
299
300
# File 'lib/twitter/rest/users.rb', line 293

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

Examples:

client.user_search('Erik Michaels-Ober')

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.

  • :page (Integer)

    Specifies the page of results to retrieve.

Returns:

Raises:

See Also:



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

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

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

Returns extended information for up to 100 users

Examples:

client.users('sferik', 'pengwynn')

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:



245
246
247
248
249
250
# File 'lib/twitter/rest/users.rb', line 245

def users(*args)
  arguments = 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), User)
  end
end

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

Returns the requesting user if authentication was successful

Examples:

client.verify_credentials

Parameters:

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

    A customizable set of options.

Options Hash (options):

  • :skip_status (Boolean, String, Integer)

    Do not include user’s Tweets.

Returns:

Raises:

See Also:



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

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