Permalink
Browse files

Expose a property (waiting_time) in EnhanceYourCalm for http header "…

…Retry-After"
  • Loading branch information...
1 parent 5c2e197 commit 7ab91f9d26351f52d3c803bb191d33bdacff5094 @duylam duylam committed Oct 18, 2010
@@ -14,7 +14,7 @@ def self.register_on_complete(env)
when 406
raise Twitter::NotAcceptable, error_message(response)
when 420
- raise Twitter::EnhanceYourCalm, error_message(response)
+ raise Twitter::EnhanceYourCalm.new error_message(response), response[:response_headers]
end
end
end
View
@@ -92,10 +92,24 @@ class BadRequest < StandardError; end
class Unauthorized < StandardError; end
class Forbidden < StandardError; end
class NotFound < StandardError; end
- class NotAcceptable < StandardError; end
- class EnhanceYourCalm < StandardError; end
+ class NotAcceptable < StandardError; end
class InternalServerError < StandardError; end
class BadGateway < StandardError; end
class ServiceUnavailable < StandardError; end
-
+
+ class EnhanceYourCalm < StandardError
+
+ def initialize(message, http_headers)
+ @http_headers = Hash[http_headers]
+ super message
+ end
+
+ # Returns number of seconds the application should wait before requesting data from the Search API again.
+ def waiting_time
+ header_value = @http_headers["retry-after"] || @http_headers["Retry-After"]
+ header_value.to_i
+ end
+
+ end
+
end
@@ -45,10 +45,14 @@ class RaiseErrorsTest < Test::Unit::TestCase
end
should "raise EnhanceYourCalm when search is rate limited" do
- stub_get('https://search.twitter.com/search.json?q=from%3Asferik', 'enhance_your_calm.json', 420)
- assert_raise Twitter::EnhanceYourCalm do
+ stub_get('https://search.twitter.com/search.json?q=from%3Asferik', 'enhance_your_calm.text', 420, nil, true)
+ begin
@search.from('sferik')
@search.fetch
+ flunk 'Should have exception at this point'
+ rescue => err
+ assert_instance_of Twitter::EnhanceYourCalm, err
+ assert_operator err.waiting_time, :> , 0
end
end
@@ -1 +0,0 @@
-{"error":"Enhance Your Calm","request":"/search.json?q=from%3Asferik"}
@@ -0,0 +1,11 @@
+HTTP/1.1 420
+Location: https://search.twitter.com
+Content-Type: application/json; charset=UTF-8
+Date: Mon, 18 Oct 2010 10:20:23 GMT
+Expires: Wed, 17 Nov 2010 10:20:23 GMT
+Cache-Control: public, max-age=2592000
+Server: gws
+Content-Length: 219
+X-XSS-Protection: 1; mode=block
+Retry-After: 100
+
View
@@ -23,8 +23,9 @@ def twitter_url(url)
url =~ /^http/ ? url : "https://api.twitter.com#{url}"
end
-def stub_get(url, filename, status=nil, location=nil)
- options = {:body => fixture_file(filename)}
+def stub_get(url, filename, status=nil, location=nil, content_as_full_response = false)
+ response_content = content_as_full_response ? 'response' : 'body'
+ options = { response_content.to_sym => fixture_file(filename) }
options.merge!({:status => status}) unless status.nil?
options.merge!({:location => location}) unless location.nil?
FakeWeb.register_uri(:get, twitter_url(url), options)

0 comments on commit 7ab91f9

Please sign in to comment.