Skip to content

Commit

Permalink
Add list_remove_members method
Browse files Browse the repository at this point in the history
  • Loading branch information
sferik committed Mar 27, 2012
1 parent 05ed01f commit 025c528
Show file tree
Hide file tree
Showing 2 changed files with 118 additions and 1 deletion.
44 changes: 43 additions & 1 deletion lib/twitter/client/lists.rb
Original file line number Diff line number Diff line change
Expand Up @@ -324,7 +324,7 @@ def list_unsubscribe(*args)
Twitter::List.new(list)
end

# Adds multiple members to a list
# Adds specified members to a list
#
# @see https://dev.twitter.com/docs/api/1/post/lists/members/create_all
# @note Lists are limited to having 500 members, and you are limited to adding up to 100 members to a list at a time with this method.
Expand Down Expand Up @@ -367,6 +367,48 @@ def list_add_members(*args)
Twitter::List.new(list)
end

# Removes specified members from the list
#
# @see https://dev.twitter.com/docs/api/1/post/lists/members/destroy_all
# @rate_limited No
# @requires_authentication Yes
# @overload list_remove_members(list, users_to_remove, options={})
# @param list [Integer, String] The list_id or slug of the list.
# @param users_to_remove [Array] The user IDs and/or screen names to remove.
# @param options [Hash] A customizable set of options.
# @return [Twitter::List] The list.
# @raise [Twitter::Error::Unauthorized] Error raised when supplied user credentials are not valid.
# @example Remove @BarackObama and @pengwynn from the authenticated user's "presidents" list
# Twitter.list_remove_members('presidents', ['BarackObama', 'pengwynn'])
# Twitter.list_remove_members('presidents', [813286, 18755393])
# Twitter.list_remove_members(8863586, ['BarackObama', 'pengwynn'])
# Twitter.list_remove_members(8863586, [813286, 18755393])
# @overload list_remove_members(user, list, users_to_remove, options={})
# @param user [Integer, String] A Twitter user ID or screen name.
# @param list [Integer, String] The list_id or slug of the list.
# @param users_to_remove [Array] The user IDs and/or screen names to remove.
# @param options [Hash] A customizable set of options.
# @return [Twitter::List] The list.
# @raise [Twitter::Error::Unauthorized] Error raised when supplied user credentials are not valid.
# @example Remove @BarackObama and @pengwynn from @sferik's "presidents" list
# Twitter.list_remove_members('sferik', 'presidents', ['BarackObama', 'pengwynn'])
# Twitter.list_remove_members('sferik', 'presidents', [813286, 18755393])
# Twitter.list_remove_members(7505382, 'presidents', ['BarackObama', 'pengwynn'])
# Twitter.list_remove_members(7505382, 'presidents', [813286, 18755393])
# Twitter.list_remove_members(7505382, 8863586, ['BarackObama', 'pengwynn'])
# Twitter.list_remove_members(7505382, 8863586, [813286, 18755393])
def list_remove_members(*args)
options = args.last.is_a?(Hash) ? args.pop : {}
users_to_remove = args.pop
options.merge_users!(Array(users_to_remove))
list = args.pop
options.merge_list!(list)
owner = args.pop || self.current_user.screen_name
options.merge_owner!(owner)
list = post("/1/lists/members/destroy_all.json", options)
Twitter::List.new(list)
end

# Check if a user is a member of the specified list
#
# @see https://dev.twitter.com/docs/api/1/get/lists/members/show
Expand Down
75 changes: 75 additions & 0 deletions spec/twitter/client/lists_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -632,6 +632,81 @@
end
end

describe ".list_remove_members" do
context "with a screen name passed" do
before do
stub_post("/1/lists/members/destroy_all.json").
with(:body => {:owner_screen_name => 'sferik', :slug => 'presidents', :user_id => "813286,18755393"}).
to_return(:body => fixture("list.json"), :headers => {:content_type => "application/json; charset=utf-8"})
end
it "should request the correct resource" do
@client.list_remove_members("sferik", "presidents", [813286, 18755393])
a_post("/1/lists/members/destroy_all.json").
with(:body => {:owner_screen_name => 'sferik', :slug => 'presidents', :user_id => "813286,18755393"}).
should have_been_made
end
it "should return the list" do
list = @client.list_remove_members("sferik", "presidents", [813286, 18755393])
list.should be_a Twitter::List
list.name.should == "presidents"
end
end
context "with an Integer user_id passed" do
before do
stub_post("/1/lists/members/destroy_all.json").
with(:body => {:owner_id => '12345678', :slug => 'presidents', :user_id => "813286,18755393"}).
to_return(:body => fixture("list.json"), :headers => {:content_type => "application/json; charset=utf-8"})
end
it "should request the correct resource" do
@client.list_remove_members(12345678, "presidents", [813286, 18755393])
a_post("/1/lists/members/destroy_all.json").
with(:body => {:owner_id => '12345678', :slug => 'presidents', :user_id => "813286,18755393"}).
should have_been_made
end
end
context "with an Integer list_id passed" do
before do
stub_post("/1/lists/members/destroy_all.json").
with(:body => {:owner_screen_name => 'sferik', :list_id => '12345678', :user_id => "813286,18755393"}).
to_return(:body => fixture("list.json"), :headers => {:content_type => "application/json; charset=utf-8"})
end
it "should request the correct resource" do
@client.list_remove_members('sferik', 12345678, [813286, 18755393])
a_post("/1/lists/members/destroy_all.json").
with(:body => {:owner_screen_name => 'sferik', :list_id => '12345678', :user_id => "813286,18755393"}).
should have_been_made
end
end
context "with a combination of member IDs and member screen names to add" do
before do
stub_post("/1/lists/members/destroy_all.json").
with(:body => {:owner_screen_name => 'sferik', :slug => 'presidents', :user_id => "813286,18755393", :screen_name => "pengwynn,erebor"}).
to_return(:body => fixture("list.json"), :headers => {:content_type => "application/json; charset=utf-8"})
end
it "should request the correct resource" do
@client.list_remove_members('sferik', 'presidents', [813286, 'pengwynn', 18755393, 'erebor'])
a_post("/1/lists/members/destroy_all.json").
with(:body => {:owner_screen_name => 'sferik', :slug => 'presidents', :user_id => "813286,18755393", :screen_name => "pengwynn,erebor"}).
should have_been_made
end
end
context "without a screen name passed" do
before do
stub_get("/1/account/verify_credentials.json").
to_return(:body => fixture("sferik.json"), :headers => {:content_type => "application/json; charset=utf-8"})
stub_post("/1/lists/members/destroy_all.json").
with(:body => {:owner_screen_name => 'sferik', :slug => 'presidents', :user_id => "813286,18755393"}).
to_return(:body => fixture("list.json"), :headers => {:content_type => "application/json; charset=utf-8"})
end
it "should request the correct resource" do
@client.list_remove_members("presidents", [813286, 18755393])
a_post("/1/lists/members/destroy_all.json").
with(:body => {:owner_screen_name => 'sferik', :slug => 'presidents', :user_id => "813286,18755393"}).
should have_been_made
end
end
end

describe ".list_member?" do
context "with a screen name passed" do
before do
Expand Down

0 comments on commit 025c528

Please sign in to comment.