diff --git a/lib/twitter/api.rb b/lib/twitter/api.rb index 84fd6dc99..2ee947a8d 100644 --- a/lib/twitter/api.rb +++ b/lib/twitter/api.rb @@ -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] The original tweets with retweet details embedded. # @overload retweet(*ids) @@ -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] The original tweets with retweet details embedded. + # @overload retweet(*ids) + # @param ids [Array, Set] An array of Tweet IDs. + # @example Retweet the Tweet with the ID 28561922516 + # Twitter.retweet(28561922516) + # @overload retweet(*ids, options) + # @param ids [Array, Set] 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) diff --git a/spec/twitter/api/statuses_spec.rb b/spec/twitter/api/statuses_spec.rb index 9fbb92954..59323887e 100644 --- a/spec/twitter/api/statuses_spec.rb +++ b/spec/twitter/api/statuses_spec.rb @@ -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"})