Skip to content
This repository was archived by the owner on May 25, 2021. It is now read-only.

Commit e9e41b0

Browse files
Sébastien Puyetsferik
Sébastien Puyet
authored andcommitted
Improve unfavorite consistency (sferik#828)
* improve unfavorite consistency * update documentation
1 parent a6114e9 commit e9e41b0

File tree

2 files changed

+71
-1
lines changed

2 files changed

+71
-1
lines changed

lib/twitter/rest/favorites.rb

+25-1
Original file line numberDiff line numberDiff line change
@@ -48,10 +48,34 @@ def favorites(*args)
4848
# @param tweets [Enumerable<Integer, String, URI, Twitter::Tweet>] A collection of Tweet IDs, URIs, or objects.
4949
# @param options [Hash] A customizable set of options.
5050
def unfavorite(*args)
51-
parallel_objects_from_response(Twitter::Tweet, :post, '/1.1/favorites/destroy.json', args)
51+
arguments = Twitter::Arguments.new(args)
52+
pmap(arguments) do |tweet|
53+
begin
54+
perform_post_with_object('/1.1/favorites/destroy.json', arguments.options.merge(id: extract_id(tweet)), Twitter::Tweet)
55+
rescue Twitter::Error::NotFound
56+
next
57+
end
58+
end.compact
5259
end
5360
alias destroy_favorite unfavorite
5461

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

spec/twitter/rest/favorites_spec.rb

+46
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,14 @@
5959
expect(tweets.first).to be_a Twitter::Tweet
6060
expect(tweets.first.text).to eq('Powerful cartoon by @BillBramhall: http://t.co/IOEbc5QoES')
6161
end
62+
context 'not found' do
63+
before do
64+
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'})
65+
end
66+
it 'does not raise an error' do
67+
expect { @client.unfavorite(540_897_316_908_331_009) }.not_to raise_error
68+
end
69+
end
6270
context 'with a URI object passed' do
6371
it 'requests the correct resource' do
6472
tweet = URI.parse('https://twitter.com/sferik/status/540897316908331009')
@@ -75,6 +83,44 @@
7583
end
7684
end
7785

86+
describe '#unfavorite!' do
87+
before do
88+
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'})
89+
end
90+
it 'requests the correct resource' do
91+
@client.unfavorite!(540_897_316_908_331_009)
92+
expect(a_post('/1.1/favorites/destroy.json').with(body: {id: '540897316908331009'})).to have_been_made
93+
end
94+
it 'returns an array of un-favorited Tweets' do
95+
tweets = @client.unfavorite!(540_897_316_908_331_009)
96+
expect(tweets).to be_an Array
97+
expect(tweets.first).to be_a Twitter::Tweet
98+
expect(tweets.first.text).to eq('Powerful cartoon by @BillBramhall: http://t.co/IOEbc5QoES')
99+
end
100+
context 'does not exist' do
101+
before do
102+
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'})
103+
end
104+
it 'raises a NotFound error' do
105+
expect { @client.unfavorite!(540_897_316_908_331_009) }.to raise_error(Twitter::Error::NotFound)
106+
end
107+
end
108+
context 'with a URI object passed' do
109+
it 'requests the correct resource' do
110+
tweet = URI.parse('https://twitter.com/sferik/status/540897316908331009')
111+
@client.unfavorite!(tweet)
112+
expect(a_post('/1.1/favorites/destroy.json').with(body: {id: '540897316908331009'})).to have_been_made
113+
end
114+
end
115+
context 'with a Tweet passed' do
116+
it 'requests the correct resource' do
117+
tweet = Twitter::Tweet.new(id: 540_897_316_908_331_009)
118+
@client.unfavorite!(tweet)
119+
expect(a_post('/1.1/favorites/destroy.json').with(body: {id: '540897316908331009'})).to have_been_made
120+
end
121+
end
122+
end
123+
78124
describe '#favorite' do
79125
before do
80126
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'})

0 commit comments

Comments
 (0)