Skip to content
This repository
Browse code

- more specific error handling for 403 situations

- 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 <nunemaker@gmail.com>
  • Loading branch information...
commit 2b85bed874902d184e5d53c0a0bd249fd1ed3b8b 1 parent a85cb35
Billy Gray billymeltdown authored jnunemaker committed
3  lib/twitter.rb
@@ -24,6 +24,9 @@ class CantConnect < StandardError; end
24 24 class BadResponse < StandardError; end
25 25 class UnknownTimeline < ArgumentError; end
26 26 class RateExceeded < StandardError; end
  27 + class CantFindUsers < ArgumentError; end
  28 + class AlreadyFollowing < StandardError; end
  29 + class CantFollowUser < StandardError; end
27 30
28 31 SourceName = 'twittergem'
29 32 end
4 lib/twitter/base.rb
@@ -230,6 +230,10 @@ def request(path, options={})
230 230 raise Unavailable, response.message
231 231 elsif response.code == '401'
232 232 raise CantConnect, 'Authentication failed. Check your username and password'
  233 + elsif response.code == '403'
  234 + raise CantFindUsers if (response/:hash/:error).text =~ /Could not find both specified users/
  235 + raise AlreadyFollowing if (response/:hash/:error).text =~ /already on your list/
  236 + raise CantFollowUser, "Response code #{response.code}: #{response.message} #{(response/:hash/:error).text}"
233 237 else
234 238 raise CantConnect, "Twitter is returning a #{response.code}: #{response.message}"
235 239 end
12 spec/base_spec.rb
@@ -69,6 +69,18 @@
69 69 timeline.size.should == 29
70 70 timeline.first.name.should == 'Blaine Cook'
71 71 end
  72 +
  73 + it "should be able to create a friendship" do
  74 + data = open(File.dirname(__FILE__) + '/fixtures/friendship_created.xml').read
  75 + @base.should_receive(:request).and_return(Hpricot::XML(data))
  76 + user = @base.create_friendship('jnunemaker')
  77 + end
  78 +
  79 + it "should bomb if friendship already exists" #do
  80 + # data = open(File.dirname(__FILE__) + '/fixtures/friendship_already_exists.xml').read
  81 + # @base.should_receive(:request).and_return(Hpricot::XML(data))
  82 + # lambda { @base.create_friendship('billymeltdown') }.should raise_error(Twitter::AlreadyFollowing)
  83 + #end
72 84 end
73 85
74 86 it "should be able to get single status" do
5 spec/fixtures/friendship_already_exists.xml
... ... @@ -0,0 +1,5 @@
  1 +<?xml version="1.0" encoding="UTF-8"?>
  2 +<hash>
  3 + <request>/friendships/create/billymeltdown.xml</request>
  4 + <error>Could not follow user: billymeltdown is already on your list.</error>
  5 +</hash>
12 spec/fixtures/friendship_created.xml
... ... @@ -0,0 +1,12 @@
  1 +<?xml version="1.0" encoding="UTF-8"?>
  2 +<user>
  3 + <id>4243</id>
  4 + <name>John Nunemaker</name>
  5 + <screen_name>jnunemaker</screen_name>
  6 + <location>Indiana</location>
  7 + <description>Loves his wife, ruby, notre dame football and iu basketball</description>
  8 + <profile_image_url>http://s3.amazonaws.com/twitter_production/profile_images/52619256/ruby_enterprise_shirt_normal.jpg</profile_image_url>
  9 + <url>http://addictedtonew.com</url>
  10 + <protected>false</protected>
  11 + <followers_count>363</followers_count>
  12 +</user>

0 comments on commit 2b85bed

Please sign in to comment.
Something went wrong with that request. Please try again.