Browse files

Allow users lookup to optionally be handled via a GET request

  • Loading branch information...
1 parent ce0c97d commit 4885c8df5f36fcbe39bd435ef12b6e0bed06dcb5 @thomasklemm thomasklemm committed Nov 29, 2012
Showing with 124 additions and 54 deletions.
  1. +2 −1 lib/twitter/api/users.rb
  2. +107 −49 spec/twitter/api/users_spec.rb
  3. +15 −4 spec/twitter/error/client_error_spec.rb
View
3 lib/twitter/api/users.rb
@@ -246,8 +246,9 @@ def unblock(*args)
# @param options [Hash] A customizable set of options.
def users(*args)
options = extract_options!(args)
+ method = options.delete(:method) || :post
args.flatten.each_slice(MAX_USERS_PER_REQUEST).threaded_map do |users|
- collection_from_response(Twitter::User, :post, "/1.1/users/lookup.json", merge_users(options, users))
+ collection_from_response(Twitter::User, method, "/1.1/users/lookup.json", merge_users(options, users))
end.flatten
end
View
156 spec/twitter/api/users_spec.rb
@@ -285,57 +285,115 @@
end
describe "#users" do
- context "with screen names passed" do
- before do
- stub_post("/1.1/users/lookup.json").with(:body => {:screen_name => "sferik,pengwynn"}).to_return(:body => fixture("users.json"), :headers => {:content_type => "application/json; charset=utf-8"})
- end
- it "requests the correct resource" do
- @client.users("sferik", "pengwynn")
- expect(a_post("/1.1/users/lookup.json").with(:body => {:screen_name => "sferik,pengwynn"})).to have_been_made
- end
- it "returns up to 100 users worth of extended information" do
- users = @client.users("sferik", "pengwynn")
- expect(users).to be_an Array
- expect(users.first).to be_a Twitter::User
- expect(users.first.id).to eq 7505382
- end
- end
- context "with numeric screen names passed" do
- before do
- stub_post("/1.1/users/lookup.json").with(:body => {:screen_name => "0,311"}).to_return(:body => fixture("users.json"), :headers => {:content_type => "application/json; charset=utf-8"})
- end
- it "requests the correct resource" do
- @client.users("0", "311")
- expect(a_post("/1.1/users/lookup.json").with(:body => {:screen_name => "0,311"})).to have_been_made
+ context "using a post request" do
+ context "with screen names passed" do
+ before do
+ stub_post("/1.1/users/lookup.json").with(:body => {:screen_name => "sferik,pengwynn"}).to_return(:body => fixture("users.json"), :headers => {:content_type => "application/json; charset=utf-8"})
+ end
+ it "requests the correct resource" do
+ @client.users("sferik", "pengwynn")
+ expect(a_post("/1.1/users/lookup.json").with(:body => {:screen_name => "sferik,pengwynn"})).to have_been_made
+ end
+ it "returns up to 100 users worth of extended information" do
+ users = @client.users("sferik", "pengwynn")
+ expect(users).to be_an Array
+ expect(users.first).to be_a Twitter::User
+ expect(users.first.id).to eq 7505382
+ end
+ end
+ context "with numeric screen names passed" do
+ before do
+ stub_post("/1.1/users/lookup.json").with(:body => {:screen_name => "0,311"}).to_return(:body => fixture("users.json"), :headers => {:content_type => "application/json; charset=utf-8"})
+ end
+ it "requests the correct resource" do
+ @client.users("0", "311")
+ expect(a_post("/1.1/users/lookup.json").with(:body => {:screen_name => "0,311"})).to have_been_made
+ end
+ end
+ context "with user IDs passed" do
+ before do
+ stub_post("/1.1/users/lookup.json").with(:body => {:user_id => "7505382,14100886"}).to_return(:body => fixture("users.json"), :headers => {:content_type => "application/json; charset=utf-8"})
+ end
+ it "requests the correct resource" do
+ @client.users(7505382, 14100886)
+ expect(a_post("/1.1/users/lookup.json").with(:body => {:user_id => "7505382,14100886"})).to have_been_made
+ end
+ end
+ context "with both screen names and user IDs passed" do
+ before do
+ stub_post("/1.1/users/lookup.json").with(:body => {:screen_name => "sferik", :user_id => "14100886"}).to_return(:body => fixture("users.json"), :headers => {:content_type => "application/json; charset=utf-8"})
+ end
+ it "requests the correct resource" do
+ @client.users("sferik", 14100886)
+ expect(a_post("/1.1/users/lookup.json").with(:body => {:screen_name => "sferik", :user_id => "14100886"})).to have_been_made
+ end
+ end
+ context "with user objects passed" do
+ before do
+ stub_post("/1.1/users/lookup.json").with(:body => {:user_id => "7505382,14100886"}).to_return(:body => fixture("users.json"), :headers => {:content_type => "application/json; charset=utf-8"})
+ end
+ it "requests the correct resource" do
+ user1 = Twitter::User.new(:id => '7505382')
+ user2 = Twitter::User.new(:id => '14100886')
+ @client.users(user1, user2)
+ expect(a_post("/1.1/users/lookup.json").with(:body => {:user_id => "7505382,14100886"})).to have_been_made
+ end
end
end
- context "with user IDs passed" do
- before do
- stub_post("/1.1/users/lookup.json").with(:body => {:user_id => "7505382,14100886"}).to_return(:body => fixture("users.json"), :headers => {:content_type => "application/json; charset=utf-8"})
- end
- it "requests the correct resource" do
- @client.users(7505382, 14100886)
- expect(a_post("/1.1/users/lookup.json").with(:body => {:user_id => "7505382,14100886"})).to have_been_made
- end
- end
- context "with both screen names and user IDs passed" do
- before do
- stub_post("/1.1/users/lookup.json").with(:body => {:screen_name => "sferik", :user_id => "14100886"}).to_return(:body => fixture("users.json"), :headers => {:content_type => "application/json; charset=utf-8"})
- end
- it "requests the correct resource" do
- @client.users("sferik", 14100886)
- expect(a_post("/1.1/users/lookup.json").with(:body => {:screen_name => "sferik", :user_id => "14100886"})).to have_been_made
- end
- end
- context "with user objects passed" do
- before do
- stub_post("/1.1/users/lookup.json").with(:body => {:user_id => "7505382,14100886"}).to_return(:body => fixture("users.json"), :headers => {:content_type => "application/json; charset=utf-8"})
- end
- it "requests the correct resource" do
- user1 = Twitter::User.new(:id => '7505382')
- user2 = Twitter::User.new(:id => '14100886')
- @client.users(user1, user2)
- expect(a_post("/1.1/users/lookup.json").with(:body => {:user_id => "7505382,14100886"})).to have_been_made
+
+ context "using a get request" do
+ context "with screen names passed" do
+ before do
+ stub_get("/1.1/users/lookup.json").with(:query => {:screen_name => "sferik,pengwynn"}).to_return(:body => fixture("users.json"), :headers => {:content_type => "application/json; charset=utf-8"})
+ end
+ it "requests the correct resource" do
+ @client.users("sferik", "pengwynn", method: :get)
+ expect(a_get("/1.1/users/lookup.json").with(:query => {:screen_name => "sferik,pengwynn"})).to have_been_made
+ end
+ it "returns up to 100 users worth of extended information" do
+ users = @client.users("sferik", "pengwynn", method: :get)
+ expect(users).to be_an Array
+ expect(users.first).to be_a Twitter::User
+ expect(users.first.id).to eq 7505382
+ end
+ end
+ context "with numeric screen names passed" do
+ before do
+ stub_get("/1.1/users/lookup.json").with(:query => {:screen_name => "0,311"}).to_return(:body => fixture("users.json"), :headers => {:content_type => "application/json; charset=utf-8"})
+ end
+ it "requests the correct resource" do
+ @client.users("0", "311", method: :get)
+ expect(a_get("/1.1/users/lookup.json").with(:query => {:screen_name => "0,311"})).to have_been_made
+ end
+ end
+ context "with user IDs passed" do
+ before do
+ stub_get("/1.1/users/lookup.json").with(:query => {:user_id => "7505382,14100886"}).to_return(:body => fixture("users.json"), :headers => {:content_type => "application/json; charset=utf-8"})
+ end
+ it "requests the correct resource" do
+ @client.users(7505382, 14100886, method: :get)
+ expect(a_get("/1.1/users/lookup.json").with(:query => {:user_id => "7505382,14100886"})).to have_been_made
+ end
+ end
+ context "with both screen names and user IDs passed" do
+ before do
+ stub_get("/1.1/users/lookup.json").with(:query => {:screen_name => "sferik", :user_id => "14100886"}).to_return(:body => fixture("users.json"), :headers => {:content_type => "application/json; charset=utf-8"})
+ end
+ it "requests the correct resource" do
+ @client.users("sferik", 14100886, method: :get)
+ expect(a_get("/1.1/users/lookup.json").with(:query => {:screen_name => "sferik", :user_id => "14100886"})).to have_been_made
+ end
+ end
+ context "with user objects passed" do
+ before do
+ stub_get("/1.1/users/lookup.json").with(:query => {:user_id => "7505382,14100886"}).to_return(:body => fixture("users.json"), :headers => {:content_type => "application/json; charset=utf-8"})
+ end
+ it "requests the correct resource" do
+ user1 = Twitter::User.new(:id => '7505382')
+ user2 = Twitter::User.new(:id => '14100886')
+ @client.users(user1, user2, method: :get)
+ expect(a_get("/1.1/users/lookup.json").with(:query => {:user_id => "7505382,14100886"})).to have_been_made
+ end
end
end
end
View
19 spec/twitter/error/client_error_spec.rb
@@ -21,11 +21,22 @@
end
context "when response status is 404 from lookup" do
- before do
- stub_post("/1.1/users/lookup.json").with(:body => {:screen_name => "not_on_twitter"}).to_return(:status => 404, :body => fixture('no_user_matches.json'))
+ context "using a post request" do
+ before do
+ stub_post("/1.1/users/lookup.json").with(:body => {:screen_name => "not_on_twitter"}).to_return(:status => 404, :body => fixture('no_user_matches.json'))
+ end
+ it "raises Twitter::Error::NotFound" do
+ expect{@client.users('not_on_twitter')}.to raise_error Twitter::Error::NotFound
+ end
end
- it "raises Twitter::Error::NotFound" do
- expect{@client.users('not_on_twitter')}.to raise_error Twitter::Error::NotFound
+
+ context "using a get request" do
+ before do
+ stub_get("/1.1/users/lookup.json").with(:query => {:screen_name => "not_on_twitter"}).to_return(:status => 404, :body => fixture('no_user_matches.json'))
+ end
+ it "raises Twitter::Error::NotFound" do
+ expect{@client.users('not_on_twitter', method: :get)}.to raise_error Twitter::Error::NotFound
+ end
end
end

0 comments on commit 4885c8d

Please sign in to comment.