diff --git a/lib/twitter/api/users.rb b/lib/twitter/api/users.rb index 31ae70713..01c83d3b4 100644 --- a/lib/twitter/api/users.rb +++ b/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 diff --git a/spec/twitter/api/users_spec.rb b/spec/twitter/api/users_spec.rb index f8639a09e..a88ce8025 100644 --- a/spec/twitter/api/users_spec.rb +++ b/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 diff --git a/spec/twitter/error/client_error_spec.rb b/spec/twitter/error/client_error_spec.rb index 411647315..d55e676be 100644 --- a/spec/twitter/error/client_error_spec.rb +++ b/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