Skip to content

Commit

Permalink
Add Twitter::Error::AlreadyRetweeted
Browse files Browse the repository at this point in the history
  • Loading branch information
sferik committed Oct 29, 2012
1 parent f1322ab commit 2a231a0
Show file tree
Hide file tree
Showing 4 changed files with 53 additions and 8 deletions.
26 changes: 18 additions & 8 deletions lib/twitter/api.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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'
Expand Down Expand Up @@ -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
Expand All @@ -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)
Expand All @@ -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
Expand Down
10 changes: 10 additions & 0 deletions lib/twitter/error/already_retweeted.rb
Original file line number Diff line number Diff line change
@@ -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
1 change: 1 addition & 0 deletions spec/fixtures/already_retweeted.json
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{"errors":"sharing is not permissible for this status (Share validations failed)"}
24 changes: 24 additions & 0 deletions spec/twitter/api/statuses_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down

0 comments on commit 2a231a0

Please sign in to comment.