Skip to content

Commit

Permalink
Expose a property (waiting_time) in EnhanceYourCalm for http header "…
Browse files Browse the repository at this point in the history
…Retry-After"
  • Loading branch information
duylam committed Oct 18, 2010
1 parent 5c2e197 commit 7ab91f9
Show file tree
Hide file tree
Showing 6 changed files with 38 additions and 9 deletions.
2 changes: 1 addition & 1 deletion lib/faraday/raise_http_4xx.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
20 changes: 17 additions & 3 deletions lib/twitter.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
8 changes: 6 additions & 2 deletions test/faraday/raise_errors_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
1 change: 0 additions & 1 deletion test/fixtures/enhance_your_calm.json

This file was deleted.

11 changes: 11 additions & 0 deletions test/fixtures/enhance_your_calm.text
Original file line number Diff line number Diff line change
@@ -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

5 changes: 3 additions & 2 deletions test/test_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down

0 comments on commit 7ab91f9

Please sign in to comment.