Skip to content

Commit

Permalink
Cache the user's screen name in an instance variable to avoid API calls
Browse files Browse the repository at this point in the history
  • Loading branch information
sferik committed Sep 7, 2012
1 parent 154b00f commit dfc5641
Showing 1 changed file with 13 additions and 9 deletions.
22 changes: 13 additions & 9 deletions lib/twitter/api.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -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|
Expand Down Expand Up @@ -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

Expand Down Expand Up @@ -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

Expand All @@ -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

Expand Down Expand Up @@ -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

Expand Down Expand Up @@ -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
Expand All @@ -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
Expand All @@ -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.