Skip to content

Commit

Permalink
Twitter::API#favorite 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#favorite! method instead.
  • Loading branch information
sferik committed Oct 29, 2012
1 parent 2a231a0 commit 65c0113
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 2 deletions.
32 changes: 30 additions & 2 deletions lib/twitter/api.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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<Twitter::Tweet>] The favorited Tweets.
# @overload favorite(*ids)
# @param ids [Array<Integer>, Set<Integer>] An array of Tweet IDs.
# @example Favorite the Tweet with the ID 25938088801
# Twitter.favorite(25938088801)
# @overload favorite(*ids, options)
# @param ids [Array<Integer>, Set<Integer>] 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
Expand Down
16 changes: 16 additions & 0 deletions spec/twitter/api/statuses_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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"})
Expand Down

0 comments on commit 65c0113

Please sign in to comment.