Retrieves all friends information with Twitter gem

This might be trivial but since I do not find an example on the Github page, I would like to give a sample implementation.

# Public: Retrieves Twitter User IDs for the followers.
# Returns Array of Twitter User IDs.
def get_follower_ids
  follower_ids = []
  next_cursor = -1
  while next_cursor != 0
    cursor = client.follower_ids(:cursor => next_cursor)
    follower_ids.concat cursor.collection
    next_cursor = cursor.next_cursor

# Public: Retrieves Twitter Users the user is following.
# Returns Array of Twitter::User.
def get_friends
  friends = []
  get_friend_ids.each_slice(100) do |ids|
    friends.concat client.users(ids)

This example shows you how to use Twitter::Cursor and with Twitter API, you can only get 100 user information at a time (that’s why I have each_slice(100) ).

Click here to find out more about Twitter gem.

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s