From 65c01133a96106a6b0c61bc16cb2ffec38fa5e25 Mon Sep 17 00:00:00 2001 From: Erik Michaels-Ober Date: Mon, 29 Oct 2012 03:38:35 -0700 Subject: [PATCH] Twitter::API#favorite no longer raises Twitter::Error::Forbidden If you depend on this behavior, you can use the new Twitter::API#favorite! method instead. --- lib/twitter/api.rb | 32 +++++++++++++++++++++++++++++-- spec/twitter/api/statuses_spec.rb | 16 ++++++++++++++++ 2 files changed, 46 insertions(+), 2 deletions(-) diff --git a/lib/twitter/api.rb b/lib/twitter/api.rb index 5c432357d..ab8d5b12d 100644 --- a/lib/twitter/api.rb +++ b/lib/twitter/api.rb @@ -1444,13 +1444,41 @@ def favorites(*args) def favorite(*args) options = args.extract_options! args.flatten.threaded_map do |id| - object_from_response(Twitter::Tweet, :post, "/1.1/favorites/create.json", options.merge(:id => id)) - end + begin + object_from_response(Twitter::Tweet, :post, "/1.1/favorites/create.json", options.merge(:id => id)) + rescue Twitter::Error::Forbidden + end + end.compact end alias fav favorite alias fave favorite alias favorite_create favorite + # Favorites the specified Tweets as the authenticating user and raises an error if one has already been favorited + # + # @see https://dev.twitter.com/docs/api/1.1/post/favorites/create + # @rate_limited No + # @authentication_required Requires user context + # @raise [Twitter::Error::Forbidden] Error raised when tweet has already been favorited. + # @raise [Twitter::Error::Unauthorized] Error raised when supplied user credentials are not valid. + # @return [Array] The favorited Tweets. + # @overload favorite(*ids) + # @param ids [Array, Set] An array of Tweet IDs. + # @example Favorite the Tweet with the ID 25938088801 + # Twitter.favorite(25938088801) + # @overload favorite(*ids, options) + # @param ids [Array, Set] An array of Tweet IDs. + # @param options [Hash] A customizable set of options. + def favorite!(*args) + options = args.extract_options! + args.flatten.threaded_map do |id| + object_from_response(Twitter::Tweet, :post, "/1.1/favorites/create.json", options.merge(:id => id)) + end + end + alias fav! favorite! + alias fave! favorite! + alias favorite_create! favorite! + # Un-favorites the specified Tweets as the authenticating user # # @see https://dev.twitter.com/docs/api/1.1/post/favorites/destroy diff --git a/spec/twitter/api/statuses_spec.rb b/spec/twitter/api/statuses_spec.rb index e853d3a77..ec7aa6faa 100644 --- a/spec/twitter/api/statuses_spec.rb +++ b/spec/twitter/api/statuses_spec.rb @@ -55,6 +55,22 @@ end end + describe "#favorite!" do + before do + stub_post("/1.1/favorites/create.json").with(:body => {:id => "25938088801"}).to_return(:body => fixture("status.json"), :headers => {:content_type => "application/json; charset=utf-8"}) + end + it "requests the correct resource" do + @client.favorite!(25938088801) + expect(a_post("/1.1/favorites/create.json").with(:body => {:id => "25938088801"})).to have_been_made + end + it "returns an array of favorited Tweets" do + tweets = @client.favorite!(25938088801) + expect(tweets).to be_an Array + expect(tweets.first).to be_a Twitter::Tweet + expect(tweets.first.text).to eq "The problem with your code is that it's doing exactly what you told it to do." + end + end + describe "#unfavorite" do before do stub_post("/1.1/favorites/destroy.json").with(:body => {:id => "25938088801"}).to_return(:body => fixture("status.json"), :headers => {:content_type => "application/json; charset=utf-8"})