Skip to content

Commit

Permalink
Improve unfavorite consistency (sferik#828)
Browse files Browse the repository at this point in the history
* improve unfavorite consistency

* update documentation
  • Loading branch information
Sébastien Puyet authored and sferik committed Nov 8, 2017
1 parent a6114e9 commit e9e41b0
Show file tree
Hide file tree
Showing 2 changed files with 71 additions and 1 deletion.
26 changes: 25 additions & 1 deletion lib/twitter/rest/favorites.rb
Original file line number Diff line number Diff line change
Expand Up @@ -48,10 +48,34 @@ def favorites(*args)
# @param tweets [Enumerable<Integer, String, URI, Twitter::Tweet>] A collection of Tweet IDs, URIs, or objects.
# @param options [Hash] A customizable set of options.
def unfavorite(*args)
parallel_objects_from_response(Twitter::Tweet, :post, '/1.1/favorites/destroy.json', args)
arguments = Twitter::Arguments.new(args)
pmap(arguments) do |tweet|
begin
perform_post_with_object('/1.1/favorites/destroy.json', arguments.options.merge(id: extract_id(tweet)), Twitter::Tweet)
rescue Twitter::Error::NotFound
next
end
end.compact
end
alias destroy_favorite unfavorite

# Un-favorites the specified Tweets as the authenticating user and raises an error if one is not found
#
# @see https://dev.twitter.com/rest/reference/post/favorites/destroy
# @rate_limited No
# @authentication Requires user context
# @raise [Twitter::Error::NotFound] Error raised when tweet does not exist or has been deleted.
# @raise [Twitter::Error::Unauthorized] Error raised when supplied user credentials are not valid.
# @return [Array<Twitter::Tweet>] The un-favorited Tweets.
# @overload unfavorite!(*tweets)
# @param tweets [Enumerable<Integer, String, URI, Twitter::Tweet>] A collection of Tweet IDs, URIs, or objects.
# @overload unfavorite!(*tweets, options)
# @param tweets [Enumerable<Integer, String, URI, Twitter::Tweet>] A collection of Tweet IDs, URIs, or objects.
# @param options [Hash] A customizable set of options.
def unfavorite!(*args)
parallel_objects_from_response(Twitter::Tweet, :post, '/1.1/favorites/destroy.json', args)
end

# Favorites the specified Tweets as the authenticating user
#
# @see https://dev.twitter.com/rest/reference/post/favorites/create
Expand Down
46 changes: 46 additions & 0 deletions spec/twitter/rest/favorites_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,14 @@
expect(tweets.first).to be_a Twitter::Tweet
expect(tweets.first.text).to eq('Powerful cartoon by @BillBramhall: http://t.co/IOEbc5QoES')
end
context 'not found' do
before do
stub_post('/1.1/favorites/destroy.json').with(body: {id: '540897316908331009'}).to_return(status: 404, body: fixture('not_found.json'), headers: {content_type: 'application/json; charset=utf-8'})
end
it 'does not raise an error' do
expect { @client.unfavorite(540_897_316_908_331_009) }.not_to raise_error
end
end
context 'with a URI object passed' do
it 'requests the correct resource' do
tweet = URI.parse('https://twitter.com/sferik/status/540897316908331009')
Expand All @@ -75,6 +83,44 @@
end
end

describe '#unfavorite!' do
before do
stub_post('/1.1/favorites/destroy.json').with(body: {id: '540897316908331009'}).to_return(body: fixture('status.json'), headers: {content_type: 'application/json; charset=utf-8'})
end
it 'requests the correct resource' do
@client.unfavorite!(540_897_316_908_331_009)
expect(a_post('/1.1/favorites/destroy.json').with(body: {id: '540897316908331009'})).to have_been_made
end
it 'returns an array of un-favorited Tweets' do
tweets = @client.unfavorite!(540_897_316_908_331_009)
expect(tweets).to be_an Array
expect(tweets.first).to be_a Twitter::Tweet
expect(tweets.first.text).to eq('Powerful cartoon by @BillBramhall: http://t.co/IOEbc5QoES')
end
context 'does not exist' do
before do
stub_post('/1.1/favorites/destroy.json').with(body: {id: '540897316908331009'}).to_return(status: 404, body: fixture('not_found.json'), headers: {content_type: 'application/json; charset=utf-8'})
end
it 'raises a NotFound error' do
expect { @client.unfavorite!(540_897_316_908_331_009) }.to raise_error(Twitter::Error::NotFound)
end
end
context 'with a URI object passed' do
it 'requests the correct resource' do
tweet = URI.parse('https://twitter.com/sferik/status/540897316908331009')
@client.unfavorite!(tweet)
expect(a_post('/1.1/favorites/destroy.json').with(body: {id: '540897316908331009'})).to have_been_made
end
end
context 'with a Tweet passed' do
it 'requests the correct resource' do
tweet = Twitter::Tweet.new(id: 540_897_316_908_331_009)
@client.unfavorite!(tweet)
expect(a_post('/1.1/favorites/destroy.json').with(body: {id: '540897316908331009'})).to have_been_made
end
end
end

describe '#favorite' do
before do
stub_post('/1.1/favorites/create.json').with(body: {id: '540897316908331009'}).to_return(body: fixture('status.json'), headers: {content_type: 'application/json; charset=utf-8'})
Expand Down

0 comments on commit e9e41b0

Please sign in to comment.