Permalink
Browse files

Cache the user's screen name in an instance variable to avoid API calls

  • Loading branch information...
1 parent 154b00f commit dfc5641511bd99da857bf524af8449afb1843f8e @sferik committed Sep 6, 2012
Showing with 13 additions and 9 deletions.
  1. +13 −9 lib/twitter/api.rb
View
22 lib/twitter/api.rb
@@ -991,7 +991,7 @@ def lists_subscribed_to(*args)
def list_timeline(*args)
options = args.extract_options!
options.merge_list!(args.pop)
- options.merge_owner!(args.pop || verify_credentials.screen_name) unless options[:owner_id] || options[:owner_screen_name]
+ options.merge_owner!(args.pop || screen_name) unless options[:owner_id] || options[:owner_screen_name]
collection_from_response(Twitter::Tweet, :get, "/1.1/lists/statuses.json", options)
end
@@ -2527,7 +2527,7 @@ def contributors(*args)
# Twitter.recommendations("sferik")
def recommendations(*args)
options = args.extract_options!
- options.merge_user!(args.pop || verify_credentials.screen_name)
+ options.merge_user!(args.pop || screen_name)
options[:excluded] = options[:excluded].join(',') if options[:excluded].is_a?(Array)
response = get("/1.1/users/recommendations.json", options)
response[:body].map do |recommendation|
@@ -2562,7 +2562,7 @@ def recommendations(*args)
def following_followers_of(*args)
options = args.extract_options!
merge_default_cursor!(options)
- options.merge_user!(args.pop || verify_credentials.screen_name)
+ options.merge_user!(args.pop || screen_name)
cursor_from_response(:users, Twitter::User, :get, "/users/following_followers_of.json", options)
end
@@ -2659,7 +2659,7 @@ def cursor_from_response(collection_name, klass, request_method, url, params={},
def list_from_response(request_method, url, args)
options = args.extract_options!
options.merge_list!(args.pop)
- options.merge_owner!(args.pop || verify_credentials.screen_name) unless options[:owner_id] || options[:owner_screen_name]
+ options.merge_owner!(args.pop || screen_name) unless options[:owner_id] || options[:owner_screen_name]
object_from_response(Twitter::List, request_method, url, options)
end
@@ -2680,7 +2680,7 @@ def lists_from_response(request_method, url, args)
# @return [Array<Twitter::User>]
def users_from_response(request_method, url, args)
options = args.extract_options!
- options.merge_user!(args.pop || verify_credentials.screen_name)
+ options.merge_user!(args.pop || screen_name)
collection_from_response(Twitter::User, request_method, url, options)
end
@@ -2710,7 +2710,7 @@ def list_users(request_method, url, args)
options = args.extract_options!
merge_default_cursor!(options)
options.merge_list!(args.pop)
- options.merge_owner!(args.pop || verify_credentials.screen_name) unless options[:owner_id] || options[:owner_screen_name]
+ options.merge_owner!(args.pop || screen_name) unless options[:owner_id] || options[:owner_screen_name]
cursor_from_response(:users, Twitter::User, request_method, url, options, {}, calling_method)
end
@@ -2743,7 +2743,7 @@ def list_user?(request_method, url, args)
options = args.extract_options!
options.merge_user!(args.pop)
options.merge_list!(args.pop)
- options.merge_owner!(args.pop || verify_credentials.screen_name) unless options[:owner_id] || options[:owner_screen_name]
+ options.merge_owner!(args.pop || screen_name) unless options[:owner_id] || options[:owner_screen_name]
send(request_method.to_sym, url, options)
true
rescue Twitter::Error::NotFound, Twitter::Error::Forbidden
@@ -2754,15 +2754,15 @@ def list_modify_member(request_method, url, args)
options = args.extract_options!
options.merge_user!(args.pop)
options.merge_list!(args.pop)
- options.merge_owner!(args.pop || verify_credentials.screen_name) unless options[:owner_id] || options[:owner_screen_name]
+ options.merge_owner!(args.pop || screen_name) unless options[:owner_id] || options[:owner_screen_name]
object_from_response(Twitter::List, request_method, url, options)
end
def list_modify_members(request_method, url, args)
options = args.extract_options!
members = args.pop
options.merge_list!(args.pop)
- options.merge_owner!(args.pop || verify_credentials.screen_name) unless options[:owner_id] || options[:owner_screen_name]
+ options.merge_owner!(args.pop || screen_name) unless options[:owner_id] || options[:owner_screen_name]
members.flatten.each_slice(MAX_USERS_PER_REQUEST).threaded_map do |users|
object_from_response(Twitter::List, request_method, url, options.merge_users(users))
end.last
@@ -2784,5 +2784,9 @@ def merge_default_cursor!(options)
options.merge!(:cursor => DEFAULT_CURSOR) unless options[:cursor]
end
+ def screen_name
+ @screen_name ||= verify_credentials.screen_name
+ end
+
end
end

0 comments on commit dfc5641

Please sign in to comment.