Permalink
Browse files

Better error handling for client.rb.

Support for empty HTTP responses with code 204.
Version bump to 1.0.7
  • Loading branch information...
dbuthay committed Jan 7, 2011
1 parent 325e3ff commit aa3206dd3b8159fd114aedcde13e21efba08ee0b
Showing with 37 additions and 7 deletions.
  1. +1 −1 VERSION
  2. +4 −4 indextank.gemspec
  3. +1 −1 lib/indextank.rb
  4. +24 −1 lib/indextank/client.rb
  5. +6 −0 lib/indextank/document.rb
  6. +1 −0 lib/indextank/exceptions.rb
View
@@ -1 +1 @@
-0.0.7
+1.0.7
View
@@ -2,13 +2,13 @@
Gem::Specification.new do |s|
s.name = %q{indextank}
- s.version = "0.0.7"
+ s.version = "1.0.7"
s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
- s.authors = ["Santiago Perez", "Terence Lee"]
- s.date = %q{2011-01-06}
+ s.authors = ["Santiago Perez", "Terence Lee", "Diego Buthay"]
+ s.date = %q{2011-01-07}
s.description = %q{Ruby Client for the IndexTank API}
- s.email = ["santip@santip.com.ar", "hone02@gmail.com"]
+ s.email = ["santip@santip.com.ar", "hone02@gmail.com", "dbuthay@gmail.com"]
s.files = ["lib/indextank.rb", "lib/indextank/client.rb", "lib/indextank/document.rb", "lib/indextank/exceptions.rb", "lib/indextank/function.rb", "lib/indextank/index.rb", "spec/lib/indextank/client_spec.rb", "spec/lib/indextank/document_spec.rb", "spec/lib/indextank/function_spec.rb", "spec/lib/indextank/index_spec.rb", "spec/spec_helper.rb"]
s.homepage = %q{http://www.indextank.com}
s.require_paths = ["lib"]
View
@@ -5,7 +5,7 @@
require File.join(directory, 'indextank', 'client')
module IndexTank
- VERSION = "0.0.7"
+ VERSION = "1.0.7"
def self.setup_connection(url, &block)
@conn = Faraday::Connection.new(:url => url) do |builder|
View
@@ -7,7 +7,8 @@ class Client
def initialize(api_url)
@uri = api_url
- @conn = IndexTank.setup_connection(api_url)
+ builder = Proc.new { |builder| builder.use ClientResponseMiddleware }
+ @conn = IndexTank.setup_connection(api_url, &builder)
end
def indexes(name = nil)
@@ -33,4 +34,26 @@ def get_index(name)
Index.new("#{@uri}/v1/indexes/#{name}")
end
end
+
+ class ClientResponseMiddleware < Faraday::Response::Middleware
+ def self.register_on_complete(env)
+ env[:response].on_complete do |finished_env|
+ case finished_env[:status]
+ when 200
+ nil # this is the expected return code
+ when 204
+ nil # this is the expected return code for empty responses
+ when 401
+ raise InvalidApiKey
+ else
+ raise UnexpectedHTTPException, finished_env[:body]
+ end
+ end
+ end
+
+ def initialize(app)
+ super
+ @parser = nil
+ end
+ end
end
@@ -67,6 +67,10 @@ class DocumentResponseMiddleware < Faraday::Response::Middleware
def self.register_on_complete(env)
env[:response].on_complete do |finished_env|
case finished_env[:status]
+ when 200
+ nil # this is the expected code
+ when 204
+ nil # this is another expected code, for empty responses
when 401
raise InvalidApiKey
when 409
@@ -75,6 +79,8 @@ def self.register_on_complete(env)
raise NonExistentIndex
when 400
raise InvalidArgument, finished_env[:body]
+ else
+ raise UnexpectedHTTPException, finished_env[:body]
end
end
end
@@ -7,4 +7,5 @@ class InvalidApiKey < StandardError; end
class InvalidQuery < StandardError; end
class IndexInitializing < StandardError; end
class InvalidArgument < StandardError; end
+ class UnexpectedHTTPException < StandardError; end
end

0 comments on commit aa3206d

Please sign in to comment.