Skip to content

Commit

Permalink
Add Twitter::Error::AlreadyFavorited
Browse files Browse the repository at this point in the history
  • Loading branch information
sferik committed Oct 29, 2012
1 parent 65c0113 commit 3471092
Show file tree
Hide file tree
Showing 4 changed files with 48 additions and 3 deletions.
16 changes: 13 additions & 3 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_favorited'
require 'twitter/error/already_retweeted'
require 'twitter/error/forbidden'
require 'twitter/error/not_found'
Expand Down Expand Up @@ -1446,7 +1447,8 @@ def favorite(*args)
args.flatten.threaded_map do |id|
begin
object_from_response(Twitter::Tweet, :post, "/1.1/favorites/create.json", options.merge(:id => id))
rescue Twitter::Error::Forbidden
rescue Twitter::Error::Forbidden => error
raise unless error.message == Twitter::Error::AlreadyFavorited::MESSAGE
end
end.compact
end
Expand All @@ -1459,7 +1461,7 @@ def favorite(*args)
# @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::AlreadyFavorited] 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)
Expand All @@ -1472,7 +1474,15 @@ def favorite(*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))
begin
object_from_response(Twitter::Tweet, :post, "/1.1/favorites/create.json", options.merge(:id => id))
rescue Twitter::Error::Forbidden => error
if error.message == "You have already favorited this status"
raise Twitter::Error::AlreadyFavorited.new("Tweet with the ID #{id} has already been favorited by the authenticated user.")
else
raise
end
end
end
end
alias fav! favorite!
Expand Down
10 changes: 10 additions & 0 deletions lib/twitter/error/already_favorited.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 favorited
class AlreadyFavorited < Twitter::Error::Forbidden
MESSAGE = "You have already favorited this status"
end
end
end
1 change: 1 addition & 0 deletions spec/fixtures/already_favorited.json
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{"errors":[{"message":"You have already favorited this status","code":139}]}
24 changes: 24 additions & 0 deletions spec/twitter/api/statuses_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,14 @@
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
context "already favorited" do
before do
stub_post("/1.1/favorites/create.json").with(:body => {:id => "25938088801"}).to_return(:status => 403, :body => fixture("already_favorited.json"), :headers => {:content_type => "application/json; charset=utf-8"})
end
it "does not raises an error" do
expect{@client.favorite(25938088801)}.not_to raise_error
end
end
end

describe "#favorite!" do
Expand All @@ -69,6 +77,22 @@
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
context "forbidden" do
before do
stub_post("/1.1/favorites/create.json").with(:body => {:id => "25938088801"}).to_return(:status => 403, :headers => {:content_type => "application/json; charset=utf-8"})
end
it "raises a Forbidden error" do
expect{@client.favorite!(25938088801)}.to raise_error(Twitter::Error::Forbidden)
end
end
context "already favorited" do
before do
stub_post("/1.1/favorites/create.json").with(:body => {:id => "25938088801"}).to_return(:status => 403, :body => fixture("already_favorited.json"), :headers => {:content_type => "application/json; charset=utf-8"})
end
it "raises an AlreadyFavorited error" do
expect{@client.favorite!(25938088801)}.to raise_error(Twitter::Error::AlreadyFavorited, "Tweet with the ID 25938088801 has already been favorited by the authenticated user.")
end
end
end

describe "#unfavorite" do
Expand Down

0 comments on commit 3471092

Please sign in to comment.