Skip to content

Commit

Permalink
Allow users lookup to optionally be handled via a GET request
Browse files Browse the repository at this point in the history
  • Loading branch information
thomasklemm committed Nov 29, 2012
1 parent ce0c97d commit 4885c8d
Show file tree
Hide file tree
Showing 3 changed files with 124 additions and 54 deletions.
3 changes: 2 additions & 1 deletion lib/twitter/api/users.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
156 changes: 107 additions & 49 deletions spec/twitter/api/users_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
19 changes: 15 additions & 4 deletions spec/twitter/error/client_error_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down

0 comments on commit 4885c8d

Please sign in to comment.