Skip to content

Commit

Permalink
Add support for configurable http codes that won't raise an error if …
Browse files Browse the repository at this point in the history
…encountered
  • Loading branch information
Hayes Davis committed Jun 13, 2012
1 parent b2096c5 commit 9681b1a
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 4 deletions.
9 changes: 6 additions & 3 deletions lib/grackle/client.rb
Expand Up @@ -79,7 +79,8 @@ def params
end
end

VALID_FORMATS = [:json,:xml,:atom,:rss]
VALID_FORMATS = [:json,:xml,:atom,:rss]
VALID_HTTP_CODES = [200]

# Contains the mapping of API name symbols to actual host (and path)
# prefixes to use with requests. You can add your own to this hash and
Expand Down Expand Up @@ -107,7 +108,7 @@ def params

attr_accessor :auth, :handlers, :default_format, :headers, :ssl, :api,
:transport, :request, :api_hosts, :timeout, :auto_append_ids,
:auto_append_format, :response_headers, :response
:auto_append_format, :response_headers, :response, :valid_http_codes

# Arguments (all are optional):
# - :username - Twitter username to authenticate with (deprecated in favor of :auth arg)
Expand All @@ -122,6 +123,7 @@ def params
# - :type=>:oauth - Include :consumer_key, :consumer_secret, :token and :token_secret keys
# - :auto_append_format - true or false to include format in URI (e.g. /test.json). Default is true
# - :response_headers - array of headers to return from the response
# - :valid_http_codes - array of HTTP codes to consider valid (non-error)
def initialize(options={})
self.transport = Transport.new
self.handlers = {:json=>Handlers::JSONHandler.new,:xml=>Handlers::XMLHandler.new,:unknown=>Handlers::StringHandler.new}
Expand All @@ -136,6 +138,7 @@ def initialize(options={})
self.auto_append_ids = options[:auto_append_ids] == false ? false : true
self.auth = {}
self.response_headers = options[:response_headers] || DEFAULT_RESPONSE_HEADERS
self.valid_http_codes = options[:valid_http_codes] || VALID_HTTP_CODES.dup
if options.has_key?(:username) || options.has_key?(:password)
#Use basic auth if :username and :password args are passed in
self.auth.merge!({:type=>:basic,:username=>options[:username],:password=>options[:password]})
Expand Down Expand Up @@ -260,7 +263,7 @@ def send_request
def process_response(format,res)
fmt_handler = handler(format)
begin
unless res.status == 200
unless self.valid_http_codes.include?(res.status)
handle_error_response(res,fmt_handler)
else
fmt_handler.decode_response(res.body)
Expand Down
14 changes: 13 additions & 1 deletion test/client_test.rb
Expand Up @@ -381,7 +381,19 @@ def test_delete_can_send_body_parameters
assert_equal('/1/some_user/some_list/members.json',client.transport.url.path)
assert_match(/user_id=12345/,Net::HTTP.request.body,"Parameters should be form encoded")
end


def test_valid_http_codes_causes_error_not_to_raise
client = new_client(202,'{"id":12345,"screen_name":"test_user"}')
assert_raise(Grackle::TwitterError) do
value = client.users.show.json? :screen_name=>'test_user'
end

client = new_client(202,'{"id":12345,"screen_name":"test_user"}',:valid_http_codes=>[200,202])
assert_nothing_raised do
value = client.users.show.json? :screen_name=>'test_user'
end
end

private
def with_http_responder(responder)
Net::HTTP.responder = responder
Expand Down

0 comments on commit 9681b1a

Please sign in to comment.