Skip to content

Commit

Permalink
Add convenience method to determine whether a user exists
Browse files Browse the repository at this point in the history
  • Loading branch information
sferik committed Jun 4, 2011
1 parent b139e91 commit a7fc861
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 0 deletions.
17 changes: 17 additions & 0 deletions lib/twitter/client/user.rb
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,23 @@ def user(*args)
format.to_s.downcase == 'xml' ? response['user'] : response
end

# Returns true if the specified user exists
#
# @param user [Integer, String] A Twitter user ID or screen name.
# @return [Boolean] true if the user exists, otherwise false.
# @example Return true if @sferik exists
# Twitter.user?("sferik")
# Twitter.user?(7505382) # Same as above
# @authenticated false
# @rate_limited true
def user?(user, options={})
merge_user_into_options!(user, options)
get('users/show', options, true)
true
rescue Twitter::NotFound
false
end

# Returns extended information for up to 100 users
#
# @format :json, :xml
Expand Down
30 changes: 30 additions & 0 deletions spec/twitter/client/user_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,36 @@

end

describe ".user?" do

before do
stub_get("users/show.#{format}").
with(:query => {:screen_name => "sferik"}).
to_return(:body => fixture("sferik.#{format}"), :headers => {:content_type => "application/#{format}; charset=utf-8"})
stub_get("users/show.#{format}").
with(:query => {:screen_name => "pengwynn"}).
to_return(:body => fixture("not_found.#{format}"), :status => 404, :headers => {:content_type => "application/#{format}; charset=utf-8"})
end

it "should get the correct resource" do
@client.user?("sferik")
a_get("users/show.#{format}").
with(:query => {:screen_name => "sferik"}).
should have_been_made
end

it "should return true if user exists" do
user = @client.user?("sferik")
user.should be_true
end

it "should return false if user does not exists" do
user = @client.user?("pengwynn")
user.should be_false
end

end

describe ".users" do

context "with screen names passed" do
Expand Down

0 comments on commit a7fc861

Please sign in to comment.