Skip to content

Commit

Permalink
Twitter::API#retweet no longer raises Twitter::Error::Forbidden
Browse files Browse the repository at this point in the history
If you depend on this behavior, you can use the new
Twitter::API#retweet! method instead.
  • Loading branch information
sferik committed Oct 29, 2012
1 parent f9d939d commit f1322ab
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 2 deletions.
33 changes: 31 additions & 2 deletions lib/twitter/api.rb
Expand Up @@ -1847,12 +1847,11 @@ def status_destroy(*args)
end
alias tweet_destroy status_destroy

# Retweets tweets
# Retweets the specified Tweets as the authenticating user
#
# @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::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 @@ -1864,6 +1863,36 @@ def status_destroy(*args)
# @param options [Hash] A customizable set of options.
# @option options [Boolean, String, Integer] :trim_user Each tweet returned in a timeline will include a user object with only the author's numerical ID when set to true, 't' or 1.
def retweet(*args)
options = args.extract_options!
args.flatten.threaded_map do |id|
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
end
end.compact
end

# Retweets the specified Tweets as the authenticating user and raises an error if one has already been retweeted
#
# @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::Unauthorized] Error raised when supplied user credentials are not valid.
# @return [Array<Twitter::Tweet>] The original tweets with retweet details embedded.
# @overload retweet(*ids)
# @param ids [Array<Integer>, Set<Integer>] An array of Tweet IDs.
# @example Retweet the Tweet with the ID 28561922516
# Twitter.retweet(28561922516)
# @overload retweet(*ids, options)
# @param ids [Array<Integer>, Set<Integer>] An array of Tweet IDs.
# @param options [Hash] A customizable set of options.
# @option options [Boolean, String, Integer] :trim_user Each tweet returned in a timeline will include a user object with only the author's numerical ID when set to true, 't' or 1.
def retweet!(*args)
options = args.extract_options!
args.flatten.threaded_map do |id|
response = post("/1.1/statuses/retweet/#{id}.json", options)
Expand Down
18 changes: 18 additions & 0 deletions spec/twitter/api/statuses_spec.rb
Expand Up @@ -373,6 +373,24 @@
end
end

describe "#retweet!" do
before do
stub_post("/1.1/statuses/retweet/28561922516.json").to_return(:body => fixture("retweet.json"), :headers => {:content_type => "application/json; charset=utf-8"})
end
it "requests the correct resource" do
@client.retweet!(28561922516)
expect(a_post("/1.1/statuses/retweet/28561922516.json")).to have_been_made
end
it "returns an array of Tweets with retweet details embedded" do
tweets = @client.retweet!(28561922516)
expect(tweets).to be_an Array
expect(tweets.first).to be_a Twitter::Tweet
expect(tweets.first.text).to eq "As for the Series, I'm for the Giants. Fuck Texas, fuck Nolan Ryan, fuck George Bush."
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
end

describe "#tweet" do
before do
stub_post("/1.1/statuses/update.json").with(:body => {:status => "The problem with your code is that it's doing exactly what you told it to do."}).to_return(:body => fixture("status.json"), :headers => {:content_type => "application/json; charset=utf-8"})
Expand Down

0 comments on commit f1322ab

Please sign in to comment.