Skip to content
Browse files

Add Twitter::Error::AlreadyRetweeted

  • Loading branch information...
1 parent f1322ab commit 2a231a0888dcd65dbef2dc92571e06d50f845cca @sferik committed Oct 29, 2012
View
26 lib/twitter/api.rb
@@ -6,6 +6,7 @@
require 'twitter/core_ext/kernel'
require 'twitter/cursor'
require 'twitter/direct_message'
+require 'twitter/error/already_retweeted'
require 'twitter/error/forbidden'
require 'twitter/error/not_found'
require 'twitter/language'
@@ -1871,7 +1872,8 @@ def retweet(*args)
retweeted_status[:body] = response[:body].delete(:retweeted_status)
retweeted_status[:body][:retweeted_status] = response[:body]
Twitter::Tweet.from_response(retweeted_status)
- rescue Twitter::Error::Forbidden
+ rescue Twitter::Error::Forbidden => error
+ raise unless error.message == Twitter::Error::AlreadyRetweeted::MESSAGE
end
end.compact
end
@@ -1881,7 +1883,7 @@ def retweet(*args)
# @see https://dev.twitter.com/docs/api/1.1/post/statuses/retweet/:id
# @rate_limited Yes
# @authentication_required Requires user context
- # @raise [Twitter::Error::Forbidden] Error raised when tweet has already been retweeted.
+ # @raise [Twitter::Error::AlreadyRetweeted] Error raised when tweet has already been retweeted.
# @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(*ids)
@@ -1895,12 +1897,20 @@ def retweet(*args)
def retweet!(*args)
options = args.extract_options!
args.flatten.threaded_map do |id|
- response = post("/1.1/statuses/retweet/#{id}.json", options)
- retweeted_status = response.dup
- retweeted_status[:body] = response[:body].delete(:retweeted_status)
- retweeted_status[:body][:retweeted_status] = response[:body]
- Twitter::Tweet.from_response(retweeted_status)
- end
+ begin
+ response = post("/1.1/statuses/retweet/#{id}.json", options)
+ retweeted_status = response.dup
+ retweeted_status[:body] = response[:body].delete(:retweeted_status)
+ retweeted_status[:body][:retweeted_status] = response[:body]
+ Twitter::Tweet.from_response(retweeted_status)
+ rescue Twitter::Error::Forbidden => error
+ if error.message == "sharing is not permissible for this status (Share validations failed)"
+ raise Twitter::Error::AlreadyRetweeted.new("Tweet with the ID #{id} has already been retweeted by the authenticated user.")
+ else
+ raise
+ end
+ end
+ end.compact
end
# Updates the authenticating user's status
View
10 lib/twitter/error/already_retweeted.rb
@@ -0,0 +1,10 @@
+require 'twitter/error/forbidden'
+
+module Twitter
+ class Error
+ # Raised when a Tweet has already been retweeted
+ class AlreadyRetweeted < Twitter::Error::Forbidden
+ MESSAGE = "sharing is not permissible for this status (Share validations failed)"
+ end
+ end
+end
View
1 spec/fixtures/already_retweeted.json
@@ -0,0 +1 @@
+{"errors":"sharing is not permissible for this status (Share validations failed)"}
View
24 spec/twitter/api/statuses_spec.rb
@@ -371,6 +371,14 @@
expect(tweets.first.retweeted_tweet.text).to eq "RT @gruber: As for the Series, I'm for the Giants. Fuck Texas, fuck Nolan Ryan, fuck George Bush."
expect(tweets.first.retweeted_tweet.id).not_to eq tweets.first.id
end
+ context "already retweeted" do
+ before do
+ stub_post("/1.1/statuses/retweet/28561922516.json").to_return(:status => 403, :body => fixture("already_retweeted.json"), :headers => {:content_type => "application/json; charset=utf-8"})
+ end
+ it "does not raise an error" do
+ expect{@client.retweet(28561922516)}.not_to raise_error
+ end
+ end
end
describe "#retweet!" do
@@ -389,6 +397,22 @@
expect(tweets.first.retweeted_tweet.text).to eq "RT @gruber: As for the Series, I'm for the Giants. Fuck Texas, fuck Nolan Ryan, fuck George Bush."
expect(tweets.first.retweeted_tweet.id).not_to eq tweets.first.id
end
+ context "fobidden" do
+ before do
+ stub_post("/1.1/statuses/retweet/28561922516.json").to_return(:status => 403, :headers => {:content_type => "application/json; charset=utf-8"})
+ end
+ it "raises a Forbidden error" do
+ expect{@client.retweet!(28561922516)}.to raise_error(Twitter::Error::Forbidden)
+ end
+ end
+ context "already retweeted" do
+ before do
+ stub_post("/1.1/statuses/retweet/28561922516.json").to_return(:status => 403, :body => fixture("already_retweeted.json"), :headers => {:content_type => "application/json; charset=utf-8"})
+ end
+ it "raises an AlreadyRetweeted error" do
+ expect{@client.retweet!(28561922516)}.to raise_error(Twitter::Error::AlreadyRetweeted, "Tweet with the ID 28561922516 has already been retweeted by the authenticated user.")
+ end
+ end
end
describe "#tweet" do

0 comments on commit 2a231a0

Please sign in to comment.
Something went wrong with that request. Please try again.