Skip to content

Commit

Permalink
Performance improvement in #user
Browse files Browse the repository at this point in the history
Only make one request when user ID or screen name isn't provided.
  • Loading branch information
sferik committed Dec 7, 2011
1 parent d776fa2 commit 9cfaa15
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 8 deletions.
9 changes: 6 additions & 3 deletions lib/twitter/client/users.rb
Original file line number Diff line number Diff line change
Expand Up @@ -85,9 +85,12 @@ def user_search(query, options={})
# Twitter.user(7505382) # Same as above
def user(*args)
options = args.last.is_a?(Hash) ? args.pop : {}
user = args.pop || self.current_user.screen_name
options.merge_user!(user)
user = get("/1/users/show.json", options)
if user = args.pop
options.merge_user!(user)
user = get("/1/users/show.json", options)
else
user = get("/1/account/verify_credentials.json", options)
end
Twitter::User.new(user)
end

Expand Down
6 changes: 1 addition & 5 deletions spec/twitter/client/users_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -180,14 +180,10 @@
before do
stub_get("/1/account/verify_credentials.json").
to_return(:body => fixture("sferik.json"), :headers => {:content_type => "application/json; charset=utf-8"})
stub_get("/1/users/show.json").
with(:query => {:screen_name => "sferik"}).
to_return(:body => fixture("sferik.json"), :headers => {:content_type => "application/json; charset=utf-8"})
end
it "should request the correct resource" do
@client.user
a_get("/1/users/show.json").
with(:query => {:screen_name => "sferik"}).
a_get("/1/account/verify_credentials.json").
should have_been_made
end
end
Expand Down

0 comments on commit 9cfaa15

Please sign in to comment.