Skip to content

Commit

Permalink
Use HTTP POST for users/lookup endpoint
Browse files Browse the repository at this point in the history
"You are strongly encouraged to use a POST for larger requests."
https://dev.twitter.com/docs/api/1.1/get/users/lookup
  • Loading branch information
sferik committed Sep 7, 2012
1 parent e112ce6 commit ff68ff8
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 35 deletions.
2 changes: 1 addition & 1 deletion lib/twitter/api.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2372,7 +2372,7 @@ def suggest_users(slug, options={})
def users(*args)
options = args.extract_options!
args.flatten.each_slice(MAX_USERS_PER_REQUEST).threaded_map do |users|
collection_from_response(Twitter::User, :get, "/1.1/users/lookup.json", options.merge_users(users))
collection_from_response(Twitter::User, :post, "/1.1/users/lookup.json", options.merge_users(users))
end.flatten
end

Expand Down
24 changes: 12 additions & 12 deletions spec/twitter/api/friendships_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -251,8 +251,8 @@
stub_get("/1.1/friends/ids.json").
with(:query => {:cursor => "-1"}).
to_return(:body => fixture("id_list.json"), :headers => {:content_type => "application/json; charset=utf-8"})
stub_get("/1.1/users/lookup.json").
with(:query => {:screen_name => "sferik,pengwynn"}).
stub_post("/1.1/users/lookup.json").
with(:body => {:screen_name => "sferik,pengwynn"}).
to_return(:body => fixture("friendships.json"), :headers => {:content_type => "application/json; charset=utf-8"})
stub_post("/1.1/friendships/create.json").
with(:body => {:user_id => "7505382", :follow => "true"}).
Expand All @@ -263,8 +263,8 @@
a_get("/1.1/friends/ids.json").
with(:query => {:cursor => "-1"}).
should have_been_made
a_get("/1.1/users/lookup.json").
with(:query => {:screen_name => "sferik,pengwynn"}).
a_post("/1.1/users/lookup.json").
with(:body => {:screen_name => "sferik,pengwynn"}).
should have_been_made
a_post("/1.1/friendships/create.json").
with(:body => {:user_id => "7505382", :follow => "true"}).
Expand All @@ -282,8 +282,8 @@
stub_get("/1.1/friends/ids.json").
with(:query => {:cursor => "-1"}).
to_return(:body => fixture("id_list.json"), :headers => {:content_type => "application/json; charset=utf-8"})
stub_get("/1.1/users/lookup.json").
with(:query => {:screen_name => "sferik,pengwynn"}).
stub_post("/1.1/users/lookup.json").
with(:body => {:screen_name => "sferik,pengwynn"}).
to_return(:body => fixture("friendships.json"), :headers => {:content_type => "application/json; charset=utf-8"})
stub_post("/1.1/friendships/create.json").
with(:body => {:user_id => "7505382"}).
Expand All @@ -294,8 +294,8 @@
a_get("/1.1/friends/ids.json").
with(:query => {:cursor => "-1"}).
should have_been_made
a_get("/1.1/users/lookup.json").
with(:query => {:screen_name => "sferik,pengwynn"}).
a_post("/1.1/users/lookup.json").
with(:body => {:screen_name => "sferik,pengwynn"}).
should have_been_made
a_post("/1.1/friendships/create.json").
with(:body => {:user_id => "7505382"}).
Expand All @@ -313,8 +313,8 @@
stub_get("/1.1/friends/ids.json").
with(:query => {:cursor => "-1"}).
to_return(:body => fixture("id_list.json"), :headers => {:content_type => "application/json; charset=utf-8"})
stub_get("/1.1/users/lookup.json").
with(:query => {:screen_name => "sferik,pengwynn"}).
stub_post("/1.1/users/lookup.json").
with(:body => {:screen_name => "sferik,pengwynn"}).
to_return(:body => fixture("friendships.json"), :headers => {:content_type => "application/json; charset=utf-8"})
stub_post("/1.1/friendships/create.json").
with(:body => {:user_id => "7505382"}).
Expand All @@ -325,8 +325,8 @@
a_get("/1.1/friends/ids.json").
with(:query => {:cursor => "-1"}).
should have_been_made
a_get("/1.1/users/lookup.json").
with(:query => {:screen_name => "sferik,pengwynn"}).
a_post("/1.1/users/lookup.json").
with(:body => {:screen_name => "sferik,pengwynn"}).
should have_been_made
a_post("/1.1/friendships/create.json").
with(:body => {:user_id => "7505382"}).
Expand Down
40 changes: 20 additions & 20 deletions spec/twitter/api/users_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -65,14 +65,14 @@
describe "#users" do
context "with screen names passed" do
before do
stub_get("/1.1/users/lookup.json").
with(:query => {:screen_name => "sferik,pengwynn"}).
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")
a_get("/1.1/users/lookup.json").
with(:query => {:screen_name => "sferik,pengwynn"}).
a_post("/1.1/users/lookup.json").
with(:body => {:screen_name => "sferik,pengwynn"}).
should have_been_made
end
it "returns up to 100 users worth of extended information" do
Expand All @@ -84,55 +84,55 @@
end
context "with numeric screen names passed" do
before do
stub_get("/1.1/users/lookup.json").
with(:query => {:screen_name => "0,311"}).
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")
a_get("/1.1/users/lookup.json").
with(:query => {:screen_name => "0,311"}).
a_post("/1.1/users/lookup.json").
with(:body => {:screen_name => "0,311"}).
should 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"}).
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)
a_get("/1.1/users/lookup.json").
with(:query => {:user_id => "7505382,14100886"}).
a_post("/1.1/users/lookup.json").
with(:body => {:user_id => "7505382,14100886"}).
should 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"}).
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)
a_get("/1.1/users/lookup.json").
with(:query => {:screen_name => "sferik", :user_id => "14100886"}).
a_post("/1.1/users/lookup.json").
with(:body => {:screen_name => "sferik", :user_id => "14100886"}).
should 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"}).
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)
a_get("/1.1/users/lookup.json").
with(:query => {:user_id => "7505382,14100886"}).
a_post("/1.1/users/lookup.json").
with(:body => {:user_id => "7505382,14100886"}).
should have_been_made
end
end
Expand Down
4 changes: 2 additions & 2 deletions spec/twitter/error/client_error_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,8 @@

context "when response status is 404 from lookup" do
before do
stub_get("/1.1/users/lookup.json").
with(:query => {:screen_name => "not_on_twitter"}).
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
Expand Down

0 comments on commit ff68ff8

Please sign in to comment.