Skip to content

Commit

Permalink
Rescue Twitter::Error::NotFound for safe #favorite and #retweet
Browse files Browse the repository at this point in the history
Closes #584.
  • Loading branch information
sferik committed Jun 24, 2014
1 parent f6b0996 commit 5e6223d
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 3 deletions.
3 changes: 2 additions & 1 deletion lib/twitter/rest/favorites.rb
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ def favorite(*args)
pmap(arguments) do |tweet|
begin
perform_with_object(:post, '/1.1/favorites/create.json', arguments.options.merge(:id => extract_id(tweet)), Twitter::Tweet)
rescue Twitter::Error::AlreadyFavorited
rescue Twitter::Error::AlreadyFavorited, Twitter::Error::NotFound
next
end
end.compact
Expand All @@ -86,6 +86,7 @@ def favorite(*args)
# @rate_limited No
# @authentication Requires user context
# @raise [Twitter::Error::AlreadyFavorited] Error raised when tweet has already been favorited.
# @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 favorited Tweets.
# @overload favorite(*tweets)
Expand Down
3 changes: 2 additions & 1 deletion lib/twitter/rest/tweets.rb
Original file line number Diff line number Diff line change
Expand Up @@ -178,7 +178,7 @@ def retweet(*args)
pmap(arguments) do |tweet|
begin
post_retweet(extract_id(tweet), arguments.options)
rescue Twitter::Error::AlreadyRetweeted
rescue Twitter::Error::AlreadyRetweeted, Twitter::Error::NotFound
next
end
end.compact
Expand All @@ -190,6 +190,7 @@ def retweet(*args)
# @rate_limited Yes
# @authentication Requires user context
# @raise [Twitter::Error::AlreadyRetweeted] Error raised when tweet has already been retweeted.
# @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 original tweets with retweet details embedded.
# @overload retweet!(*tweets)
Expand Down
18 changes: 17 additions & 1 deletion spec/twitter/rest/favorites_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,15 @@
before do
stub_post('/1.1/favorites/create.json').with(:body => {:id => '25938088801'}).to_return(:status => 403, :body => fixture('already_favorited.json'), :headers => {:content_type => 'application/json; charset=utf-8'})
end
it 'does not raises an error' do
it 'does not raise an error' do
expect { @client.favorite(25_938_088_801) }.not_to raise_error
end
end
context 'not found' do
before do
stub_post('/1.1/favorites/create.json').with(:body => {:id => '25938088801'}).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.favorite(25_938_088_801) }.not_to raise_error
end
end
Expand Down Expand Up @@ -163,6 +171,14 @@
expect { @client.favorite!(25_938_088_801) }.to raise_error(Twitter::Error::AlreadyFavorited)
end
end
context 'does not exist' do
before do
stub_post('/1.1/favorites/create.json').with(:body => {:id => '25938088801'}).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.favorite!(25_938_088_801) }.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/25938088801')
Expand Down
16 changes: 16 additions & 0 deletions spec/twitter/rest/tweets_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -358,6 +358,14 @@
expect { @client.retweet(25_938_088_801) }.not_to raise_error
end
end
context 'not found' do
before do
stub_post('/1.1/statuses/retweet/25938088801.json').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.retweet(25_938_088_801) }.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/25938088801')
Expand Down Expand Up @@ -412,6 +420,14 @@
expect { @client.retweet!(25_938_088_801) }.to raise_error(Twitter::Error::AlreadyRetweeted)
end
end
context 'not found' do
before do
stub_post('/1.1/statuses/retweet/25938088801.json').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.retweet!(25_938_088_801) }.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/25938088801')
Expand Down

0 comments on commit 5e6223d

Please sign in to comment.