Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Add Twitter::Error::AlreadyFavorited

  • Loading branch information...
commit 34710927e00d4dc5abc049bfc198bdd337fba1bd 1 parent 65c0113
@sferik authored
View
16 lib/twitter/api.rb
@@ -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'
@@ -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
@@ -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)
@@ -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!
View
10 lib/twitter/error/already_favorited.rb
@@ -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
View
1  spec/fixtures/already_favorited.json
@@ -0,0 +1 @@
+{"errors":[{"message":"You have already favorited this status","code":139}]}
View
24 spec/twitter/api/statuses_spec.rb
@@ -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
@@ -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
Please sign in to comment.
Something went wrong with that request. Please try again.