Module: Twitter::API::Users

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

Constant Summary collapse

MAX_USERS_PER_REQUEST =
100

Constants included from Utils

Twitter::API::Utils::DEFAULT_CURSOR

Instance Method Summary collapse

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>

    Examples:

    Block and unfriend @sferik as the authenticating user

    Twitter.block('sferik')
    Twitter.block(7505382)  # Same as above
    

    Parameters:

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

      An array of Twitter user IDs, screen names, or objects.

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

    Parameters:

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

      An array 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



213
214
215
# File 'lib/twitter/api/users.rb', line 213

def block(*args)
  threaded_user_objects_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:

Check whether the authenticating user is blocking @sferik

Twitter.block?('sferik')
Twitter.block?(7505382)  # Same as above

Parameters:

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

    A Twitter user ID, screen name, 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



184
185
186
187
188
189
190
191
192
193
194
195
# File 'lib/twitter/api/users.rb', line 184

def block?(user, options={})
  merge_default_cursor!(options)
  user_id = case user
  when Integer
    user
  when String
    user(user).id
  when Twitter::User
    user.id
  end
  blocked_ids(options).all.map(&:to_i).include?(user_id)
end

#block(options = {}) ⇒ Array

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

Examples:

Return an array of numeric user ids the authenticating user is blocking

Twitter.blocking_ids

Parameters:

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

    A customizable set of options.

Returns:

  • (Array)

    Numeric user ids the authenticating user is blocking.

Raises:

See Also:

Rate Limited?:

  • Yes

Authentication:

  • Requires user context



166
167
168
169
170
# File 'lib/twitter/api/users.rb', line 166

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

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

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

Examples:

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

Twitter.blocking

Parameters:

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

    A customizable set of options.

Options Hash (options):

  • :page (Integer)

    Specifies the page of results to retrieve.

Returns:

  • (Array<Twitter::User>)

    User objects that the authenticating user is blocking.

Raises:

See Also:

Rate Limited?:

  • Yes

Authentication:

  • Requires user context



151
152
153
# File 'lib/twitter/api/users.rb', line 151

def blocking(options={})
  cursor_from_response(:users, Twitter::User, :get, "/1.1/blocks/list.json", options, :blocking)
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>

    Examples:

    Return the authenticated user's contributees

    Twitter.contributees
    

    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>

    Examples:

    Return users @sferik can contribute to

    Twitter.contributees('sferik')
    Twitter.contributees(7505382)  # Same as above
    

    Parameters:

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

      A Twitter user ID, screen name, 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



348
349
350
# File 'lib/twitter/api/users.rb', line 348

def contributees(*args)
  user_objects_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>

    Examples:

    Return the authenticated user's contributors

    Twitter.contributors
    

    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>

    Examples:

    Return users who can contribute to @sferik's account

    Twitter.contributors('sferik')
    Twitter.contributors(7505382)  # Same as above
    

    Parameters:

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

      A Twitter user ID, screen name, 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



371
372
373
# File 'lib/twitter/api/users.rb', line 371

def contributors(*args)
  user_objects_from_response(:get, "/1.1/users/contributors.json", args)
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:

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

    Examples:

    Return the authenticated user's profile banner

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

    Examples:

    Return the specified user's profile banner

    Twitter.profile_banner('sferik')
    Twitter.profile_banner(7505382)  # Same as above
    

    Parameters:

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

      A Twitter user ID, screen name, or object.

Returns:

Raises:

See Also:

Rate Limited?:

  • Yes

Authentication:

  • Requires user context



428
429
430
431
432
# File 'lib/twitter/api/users.rb', line 428

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

#remove_profile_banner(options = {}) ⇒ nil

Removes the authenticating user's profile banner image

Examples:

Remove the authenticating user's profile banner image

Twitter.remove_profile_banner

Parameters:

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

    A customizable set of options.

Returns:

  • (nil)

Raises:

See Also:

Rate Limited?:

  • No

Authentication:

  • Requires user context



385
386
387
# File 'lib/twitter/api/users.rb', line 385

def remove_profile_banner(options={})
  post("/1.1/account/remove_profile_banner.json", options)[:body]
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.

Examples:

Return the settings for the authenticating user.

Twitter.settings

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.

Returns:

Raises:

See Also:

Rate Limited?:

  • Yes

Authentication:

  • Requires user context



33
34
35
36
# File 'lib/twitter/api/users.rb', line 33

def settings(options={})
  request_method = options.size.zero? ? :get : :post
  object_from_response(Twitter::Settings, request_method, "/1.1/account/settings.json", options)
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>

    Examples:

    Un-block @sferik as the authenticating user

    Twitter.unblock('sferik')
    Twitter.unblock(7505382)  # Same as above
    

    Parameters:

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

      An array of Twitter user IDs, screen names, or objects.

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

    Parameters:

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

      An array 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



232
233
234
# File 'lib/twitter/api/users.rb', line 232

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

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

Sets which device Twitter delivers updates to for the authenticating user

Examples:

Turn SMS updates on for the authenticating user

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

Rate Limited?:

  • No

Authentication:

  • Requires user context



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

def update_delivery_device(device, options={})
  object_from_response(Twitter::User, :post, "/1.1/account/update_delivery_device.json", options.merge(:device => device))
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

Examples:

Set authenticating user's name to Erik Michaels-Ober

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

Returns:

Raises:

See Also:

Rate Limited?:

  • No

Authentication:

  • Requires user context



84
85
86
# File 'lib/twitter/api/users.rb', line 84

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

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

Updates the authenticating user's profile background image

Examples:

Update the authenticating user's profile background image

Twitter.update_profile_background_image(File.new("we_concept_bg2.png"))

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



100
101
102
# File 'lib/twitter/api/users.rb', line 100

def update_profile_background_image(image, options={})
  object_from_response(Twitter::User, :post, "/1.1/account/update_profile_background_image.json", options.merge(:image => image))
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

Examples:

Update the authenticating user's profile banner

Twitter.update_profile_banner(File.new("me.jpeg"))

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



408
409
410
# File 'lib/twitter/api/users.rb', line 408

def update_profile_banner(banner, options={})
  post("/1.1/account/update_profile_banner.json", options.merge(:banner => banner))[:body]
end

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

Sets one or more hex values that control the color scheme of the authenticating user's profile

Examples:

Set authenticating user's profile background to black

Twitter.update_profile_colors(:profile_background_color => '000000')

Parameters:

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

    A customizable set of options.

Options Hash (options):

  • :profile_background_color (String)

    Profile background color.

  • :profile_text_color (String)

    Profile text color.

  • :profile_link_color (String)

    Profile link color.

  • :profile_sidebar_fill_color (String)

    Profile sidebar's background color.

  • :profile_sidebar_border_color (String)

    Profile sidebar's border color.

Returns:

Raises:

See Also:

Rate Limited?:

  • No

Authentication:

  • Requires user context



119
120
121
# File 'lib/twitter/api/users.rb', line 119

def update_profile_colors(options={})
  object_from_response(Twitter::User, :post, "/1.1/account/update_profile_colors.json", options)
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

Examples:

Update the authenticating user's profile image

Twitter.update_profile_image(File.new("me.jpeg"))

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



136
137
138
# File 'lib/twitter/api/users.rb', line 136

def update_profile_image(image, options={})
  object_from_response(Twitter::User, :post, "/1.1/account/update_profile_image.json", options.merge(:image => image))
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

    Examples:

    Return extended information for the authenticated user

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

    Examples:

    Return extended information for @sferik

    Twitter.user('sferik')
    Twitter.user(7505382)  # Same as above
    

    Parameters:

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

      A Twitter user ID, screen name, or object.

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

      A customizable set of options.

Returns:

Raises:

See Also:

Rate Limited?:

  • Yes

Authentication:

  • Requires user context



284
285
286
287
288
289
290
291
292
# File 'lib/twitter/api/users.rb', line 284

def user(*args)
  arguments = Twitter::API::Arguments.new(args)
  if user = arguments.pop
    merge_user!(arguments.options, user)
    object_from_response(Twitter::User, :get, "/1.1/users/show.json", arguments.options)
  else
    verify_credentials(arguments.options)
  end
end

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

Returns true if the specified user exists

Examples:

Return true if @sferik exists

Twitter.user?('sferik')
Twitter.user?(7505382)  # Same as above

Parameters:

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

    A Twitter user ID, screen name, or object.

Returns:

  • (Boolean)

    true if the user exists, otherwise false.

Raises:

Rate Limited?:

  • Yes

Authentication:

  • Requires user context



304
305
306
307
308
309
310
# File 'lib/twitter/api/users.rb', line 304

def user?(user, options={})
  merge_user!(options, user)
  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:

Return users that match "Erik Michaels-Ober"

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



325
326
327
# File 'lib/twitter/api/users.rb', line 325

def user_search(query, options={})
  objects_from_response(Twitter::User, :get, "/1.1/users/search.json", options.merge(:q => query))
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>

    Examples:

    Return extended information for @sferik and @pengwynn

    Twitter.users('sferik', 'pengwynn')  # Retrieve users with a POST request using screen_names
    Twitter.users(7505382, 14100886)     # Same as above using twitter_ids
    

    Parameters:

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

      An array of Twitter user IDs, screen names, or objects.

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

    Examples:

    Return extended information for @sferik and @pengwynn

    Twitter.users('sferik', 'pengwynn', :method => :get) # Retrieve users with a GET request
    Twitter.users(7505382, 14100886, :method => :get)    # Same as above
    

    Parameters:

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

      An array of Twitter user IDs, screen names, or objects.

    • options (Hash)

      A customizable set of options.

    Options Hash (options):

    • :method (Symbol, String)

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

    • :include_entities (Boolean)

      The tweet entities node will be disincluded when set to false.

Returns:

Raises:

See Also:

Rate Limited?:

  • Yes

Authentication:

  • Requires user context



256
257
258
259
260
261
262
# File 'lib/twitter/api/users.rb', line 256

def users(*args)
  arguments = Twitter::API::Arguments.new(args)
  method = arguments.options.delete(:method) || :post
  arguments.flatten.each_slice(MAX_USERS_PER_REQUEST).threaded_map do |users|
    objects_from_response(Twitter::User, method, "/1.1/users/lookup.json", merge_users(arguments.options, users))
  end.flatten
end

#verify_credentials(options = {}) ⇒ Twitter::User Also known as: current_user

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

Examples:

Return the requesting user if authentication was successful

Twitter.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 when set to true, 't' or 1.

Returns:

Raises:

See Also:

Rate Limited?:

  • Yes

Authentication:

  • Requires user context



49
50
51
# File 'lib/twitter/api/users.rb', line 49

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