Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

swithced to new cursor API for friends/followers social graph methods…

…; github issue #1
  • Loading branch information...
commit 40419d2778c24f15d14ce54a1948c4e2ee2fb8cb 1 parent aae605e
@tardate authored
View
20 MIT-LICENSE
@@ -0,0 +1,20 @@
+Copyright (c) 2009 Paul Gallagher (tardate.com)
+
+Permission is hereby granted, free of charge, to any person obtaining
+a copy of this software and associated documentation files (the
+"Software"), to deal in the Software without restriction, including
+without limitation the rights to use, copy, modify, merge, publish,
+distribute, sublicense, and/or sell copies of the Software, and to
+permit persons to whom the Software is furnished to do so, subject to
+the following conditions:
+
+The above copyright notice and this permission notice shall be
+included in all copies or substantial portions of the Software.
+
+THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
+EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
+MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
+NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
+LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
+OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
+WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
View
24 lib/oauth_system.rb
@@ -143,24 +143,20 @@ def update_status!( status , in_reply_to_status_id = nil )
# Twitter REST API Method: statuses friends
def friends(user=nil)
- begin
- self.twitagent.friends(user)
- rescue => err
- RAILS_DEFAULT_LOGGER.error "Failed to get friends via OAuth for #{current_user.inspect}"
- flash[:error] = "Twitter API failure (getting friends)"
- return
- end
+ self.twitagent.friends(user)
+ rescue => err
+ RAILS_DEFAULT_LOGGER.error "Failed to get friends via OAuth for #{current_user.inspect}"
+ flash[:error] = "Twitter API failure (getting friends)"
+ return
end
# Twitter REST API Method: statuses followers
def followers(user=nil)
- begin
- self.twitagent.followers(user)
- rescue => err
- RAILS_DEFAULT_LOGGER.error "Failed to get followers via OAuth for #{current_user.inspect}"
- flash[:error] = "Twitter API failure (getting followers)"
- return
- end
+ self.twitagent.followers(user)
+ rescue => err
+ RAILS_DEFAULT_LOGGER.error "Failed to get followers via OAuth for #{current_user.inspect}"
+ flash[:error] = "Twitter API failure (getting followers)"
+ return
end
# Twitter REST API Method: statuses mentions
View
8 lib/tasks/test.rake
@@ -7,10 +7,10 @@ task :demo_proxy_login => :environment do
twoauth = TwitterOauth.new(member.token, member.secret)
puts ".. .. getting friends:"
- friends = twoauth.friends
- friends.each do |friend|
- puts ".. .. .. #{friend['screen_name']}"
- end
+ twoauth.dump_friends
+
+ puts ".. .. getting followers:"
+ twoauth.dump_followers
puts "demo_proxy_login: Done."
end
View
180 lib/twitter_oauth.rb
@@ -92,85 +92,135 @@ def rate_limit_status
end
end
- # Twitter REST API Method: statuses friends
+ # Twitter REST API Method: statuses friends (page-based implementation)
+ # Returns: friends array
def friends_by_page( user=nil, page = 1 )
- begin
- params = (
- { :screen_name => user, :page => page }.collect { |n| "#{n[0]}=#{n[1]}" if n[1] }
- ).compact.join('&')
- #params = "?#{params}" unless params.empty?
- url = "/statuses/friends.json?#{params}"
- response = access_token.get( url )
- case response
- when Net::HTTPSuccess
- friends=JSON.parse(response.body)
- raise TwitterOauth::UnexpectedResponse unless friends.is_a? Array
- friends
- else
- raise TwitterOauth::APIError
- end
- rescue => err
- puts "Exception in friends_by_page: #{err}"
- raise err
+ warn "[DEPRECATION] the page parameter ot the twitter API has been deprecated as of 26-Oct-2009. use 'friends_by_cursor' method instead."
+ params = (
+ { :screen_name => user, :page => page }.collect { |n| "#{n[0]}=#{n[1]}" if n[1] }
+ ).compact.join('&')
+ url = "/statuses/friends.json?#{params}"
+ response = access_token.get( url )
+ case response
+ when Net::HTTPSuccess
+ friends=JSON.parse(response.body)
+ raise TwitterOauth::UnexpectedResponse unless friends.is_a? Array
+ friends
+ else
+ raise TwitterOauth::APIError
+ end
+ rescue => err
+ puts "Exception in friends_by_page: #{err}"
+ raise err
+ end
+
+ # Twitter REST API Method: statuses friends (cursor-based implementation)
+ # Returns: friends hash
+ def friends_by_cursor( user=nil, cursor = -1 )
+ params = (
+ { :screen_name => user, :cursor => cursor }.collect { |n| "#{n[0]}=#{n[1]}" if n[1] }
+ ).compact.join('&')
+ url = "/statuses/friends.json?#{params}"
+ response = access_token.get( url )
+ case response
+ when Net::HTTPSuccess
+ friends=JSON.parse(response.body)
+ raise TwitterOauth::UnexpectedResponse unless friends.is_a? Hash
+ friends
+ else
+ raise TwitterOauth::APIError
end
+ rescue => err
+ puts "Exception in friends_by_cursor: #{err}"
+ raise err
end
+
+ # Twitter REST API Method: statuses friends
+ # Returns: array of all friends for the given user
+ # Parameters:
+ # <t>user</t> - screen_name of the user to retrieve friends for. if nil, returns friends for the current twitter-authenticated user
def friends(user=nil)
+ cursor = -1
+ page = 0
+ friends = []
begin
- page = 1
- friends = []
- begin
- friendspage = friends_by_page( user, page )
- puts "page #{page} - found #{friendspage.size} friends"
- friends += friendspage if friendspage
- page += 1
- end until friendspage.size == 0
+ friendspage = friends_by_cursor( user, cursor )
+ page += 1
+ puts "page #{page}/cursor #{cursor} - found #{friendspage["users"].size} friends. Next cursor: #{friendspage["next_cursor"]}"
+ friends += friendspage["users"] if friendspage
+ cursor = friendspage["next_cursor"]
+ end until cursor == 0
+ friends
+ rescue => err
+ puts "Exception in friends: #{err}"
+ raise err
+ end
+
+
+ # Twitter REST API Method: statuses followers (page-based implementation)
+ # Returns: followers array
+ def followers_by_page( user=nil, page = 1 )
+ warn "[DEPRECATION] the page parameter ot the twitter API has been deprecated as of 26-Oct-2009. use 'followers_by_cursor' method instead."
+ params = (
+ { :screen_name => user, :page => page }.collect { |n| "#{n[0]}=#{n[1]}" if n[1] }
+ ).compact.join('&')
+ url = "/statuses/followers.json?#{params}"
+ response = access_token.get( url )
+ case response
+ when Net::HTTPSuccess
+ friends=JSON.parse(response.body)
+ raise TwitterOauth::UnexpectedResponse unless friends.is_a? Array
friends
- rescue => err
- puts "Exception in friends: #{err}"
- raise err
+ else
+ raise TwitterOauth::APIError
end
+ rescue => err
+ puts "Exception in followers_by_page: #{err}"
+ raise err
end
-
- # Twitter REST API Method: statuses followers
- def followers_by_page( user=nil, page = 1 )
- begin
- params = (
- { :screen_name => user, :page => page }.collect { |n| "#{n[0]}=#{n[1]}" if n[1] }
- ).compact.join('&')
- #params = "?#{params}" unless params.empty?
- url = "/statuses/followers.json?#{params}"
- response = access_token.get( url )
- case response
- when Net::HTTPSuccess
- friends=JSON.parse(response.body)
- raise TwitterOauth::UnexpectedResponse unless friends.is_a? Array
- friends
- else
- raise TwitterOauth::APIError
- end
- rescue => err
- puts "Exception in followers_by_page: #{err}"
- raise err
+ # Twitter REST API Method: statuses followers (cursor-based implementation)
+ # Returns: followers hash
+ def followers_by_cursor( user=nil, cursor = -1 )
+ params = (
+ { :screen_name => user, :cursor => cursor }.collect { |n| "#{n[0]}=#{n[1]}" if n[1] }
+ ).compact.join('&')
+ url = "/statuses/followers.json?#{params}"
+ response = access_token.get( url )
+ case response
+ when Net::HTTPSuccess
+ friends=JSON.parse(response.body)
+ raise TwitterOauth::UnexpectedResponse unless friends.is_a? Hash
+ friends
+ else
+ raise TwitterOauth::APIError
end
+ rescue => err
+ puts "Exception in followers_by_cursor: #{err}"
+ raise err
end
+
+ # Twitter REST API Method: statuses friends
+ # Returns: array of all friends for the given user
+ # Parameters:
+ # <t>user</t> - screen_name of the user to retrieve friends for. if nil, returns friends for the current twitter-authenticated user
def followers(user=nil)
+ cursor = -1
+ page = 0
+ followers = []
begin
- page = 1
- followers = []
- begin
- followerspage = followers_by_page( user, page )
- puts "page #{page} - found #{followerspage.size} followers"
- followers += followerspage if followerspage
- page += 1
- end until followerspage.size == 0
- followers
- rescue => err
- puts "Exception in followers: #{err}"
- raise err
- end
+ followerspage = followers_by_cursor( user, cursor )
+ page += 1
+ puts "page #{page}/cursor #{cursor} - found #{followerspage["users"].size} followers. Next cursor: #{followerspage["next_cursor"]}"
+ followers += followerspage["users"] if followerspage["users"]
+ cursor = followerspage["next_cursor"]
+ end until cursor == 0
+ followers
+ rescue => err
+ puts "Exception in followers: #{err}"
+ raise err
end
-
+
# Twitter REST API Method: friendships exists
# Will return true if user_a follows user_b, otherwise will return false.
def friendship_exists?(user_a, user_b)
Please sign in to comment.
Something went wrong with that request. Please try again.