Permalink
Browse files

Add convenience method to determine whether a user exists

  • Loading branch information...
1 parent b139e91 commit a7fc8616e5733e4c64f98f2ff5562e74e025a1f3 @sferik committed Jun 4, 2011
Showing with 47 additions and 0 deletions.
  1. +17 −0 lib/twitter/client/user.rb
  2. +30 −0 spec/twitter/client/user_spec.rb
View
@@ -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
@@ -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

0 comments on commit a7fc861

Please sign in to comment.