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
  end
  follower_ids
end

# 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)
  end
  friends
end

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.

Advertisements