class Twitter::Client @@USER_URIS = { :info => '/users/show', :friends => '/statuses/friends.json', :followers => '/statuses/followers.json', } # Provides access to Twitter's User APIs # # Returns user instance for the id given. The id # can either refer to the numeric user ID or the user's screen name. # # For example, # @twitter.user(234943) #=> Twitter::User object instance for user with numeric id of 234943 # @twitter.user('mylogin') #=> Twitter::User object instance for user with screen name 'mylogin' # # Where options is a +Hash+ of options that can include: # * :page - optional. Retrieves the next set of friends. There are 100 friends per page. Default: 1. # * :lite - optional. Prevents the inline inclusion of current status. Default: false. # * :since - optional. Only relevant for :friends action. Narrows the results to just those friends added after the date given as value of this option. Must be HTTP-formatted date. # # An ArgumentError will be raised if an invalid action # is given. Valid actions are: # * +:info+ # * +:friends+ # # +Note:+ You should not use this method to attempt to retrieve the # authenticated user's followers. Please use any of the following # ways of accessing this list: # followers = client.my(:followers) # OR # followers = client.my(:info).followers def user(id, action = :info, options = {}) raise ArgumentError, "Invalid user action: #{action}" unless @@USER_URIS.keys.member?(action) id = id.to_i if id.is_a?(Twitter::User) params = options.merge(:id => id) response = http_connect {|conn| create_http_get_request(@@USER_URIS[action], params) } bless_models(Twitter::User.unmarshal(response.body)) end # Syntactic sugar for queries relating to authenticated user in Twitter's User API # # Where action is one of the following: # * :info - Returns user instance for the authenticated user. # * :friends - Returns Array of users that are authenticated user's friends # * :followers - Returns Array of users that are authenticated user's followers # # Where options is a +Hash+ of options that can include: # * :page - optional. Retrieves the next set of friends. There are 100 friends per page. Default: 1. # * :lite - optional. Prevents the inline inclusion of current status. Default: false. # * :since - optional. Only relevant for :friends action. Narrows the results to just those friends added after the date given as value of this option. Must be HTTP-formatted date. # # An ArgumentError will be raised if an invalid action # is given. Valid actions are: # * +:info+ # * +:friends+ # * +:followers+ def my(action, options = {}) raise ArgumentError, "Invalid user action: #{action}" unless @@USER_URIS.keys.member?(action) params = options.merge(:id => @login) response = http_connect {|conn| create_http_get_request(@@USER_URIS[action], params) } users = Twitter::User.unmarshal(response.body) bless_models(users) end # Returns all followers for an authenticated user. # # This method makes multiple call to the Twitter API to get all users. # By default, the Twitter API only returns 100 followers at a time. # This method was written for the case where you need all followers,no matter how many there are. # def all_followers has_followers_remaining = true followers_array = [] page = 1 while has_followers_remaining tmp_followers = self.my(:followers, :page => page) followers_array << tmp_followers #puts "Page: #{page} , followers: #{tmp_followers.size}" page += page has_followers_remaining = false unless tmp_followers.size > 98 sleep 1 end followers_array.flatten! end end