Skip to content

Commit

Permalink
- more specific error handling for 403 situations
Browse files Browse the repository at this point in the history
- adds exceptions CantFindUsers, AlreadyFollowing, and CantFollowUser
- one rspect test for creating a frienship
- need to work out a means to spec the exception throwing, gonna
 test by hand for now

Signed-off-by: John Nunemaker <[email protected]>
  • Loading branch information
billymeltdown authored and jnunemaker committed Jan 5, 2009
1 parent a85cb35 commit 2b85bed
Show file tree
Hide file tree
Showing 5 changed files with 36 additions and 0 deletions.
3 changes: 3 additions & 0 deletions lib/twitter.rb
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,9 @@ class CantConnect < StandardError; end
class BadResponse < StandardError; end
class UnknownTimeline < ArgumentError; end
class RateExceeded < StandardError; end
class CantFindUsers < ArgumentError; end
class AlreadyFollowing < StandardError; end
class CantFollowUser < StandardError; end

SourceName = 'twittergem'
end
4 changes: 4 additions & 0 deletions lib/twitter/base.rb
Original file line number Diff line number Diff line change
Expand Up @@ -230,6 +230,10 @@ def request(path, options={})
raise Unavailable, response.message
elsif response.code == '401'
raise CantConnect, 'Authentication failed. Check your username and password'
elsif response.code == '403'
raise CantFindUsers if (response/:hash/:error).text =~ /Could not find both specified users/
raise AlreadyFollowing if (response/:hash/:error).text =~ /already on your list/
raise CantFollowUser, "Response code #{response.code}: #{response.message} #{(response/:hash/:error).text}"
else
raise CantConnect, "Twitter is returning a #{response.code}: #{response.message}"
end
Expand Down
12 changes: 12 additions & 0 deletions spec/base_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,18 @@
timeline.size.should == 29
timeline.first.name.should == 'Blaine Cook'
end

it "should be able to create a friendship" do
data = open(File.dirname(__FILE__) + '/fixtures/friendship_created.xml').read
@base.should_receive(:request).and_return(Hpricot::XML(data))
user = @base.create_friendship('jnunemaker')
end

it "should bomb if friendship already exists" #do
# data = open(File.dirname(__FILE__) + '/fixtures/friendship_already_exists.xml').read
# @base.should_receive(:request).and_return(Hpricot::XML(data))
# lambda { @base.create_friendship('billymeltdown') }.should raise_error(Twitter::AlreadyFollowing)
#end
end

it "should be able to get single status" do
Expand Down
5 changes: 5 additions & 0 deletions spec/fixtures/friendship_already_exists.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
<?xml version="1.0" encoding="UTF-8"?>
<hash>
<request>/friendships/create/billymeltdown.xml</request>
<error>Could not follow user: billymeltdown is already on your list.</error>
</hash>
12 changes: 12 additions & 0 deletions spec/fixtures/friendship_created.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<?xml version="1.0" encoding="UTF-8"?>
<user>
<id>4243</id>
<name>John Nunemaker</name>
<screen_name>jnunemaker</screen_name>
<location>Indiana</location>
<description>Loves his wife, ruby, notre dame football and iu basketball</description>
<profile_image_url>http://s3.amazonaws.com/twitter_production/profile_images/52619256/ruby_enterprise_shirt_normal.jpg</profile_image_url>
<url>http://addictedtonew.com</url>
<protected>false</protected>
<followers_count>363</followers_count>
</user>

0 comments on commit 2b85bed

Please sign in to comment.