lib/3scale/api/http_client.rb in 3scale-api-1.1.0 vs lib/3scale/api/http_client.rb in 3scale-api-1.2.0
- old
+ new
@@ -66,22 +66,28 @@
class UnexpectedResponseError < ResponseError; end
class NotFoundError < ResponseError; end
+ class UnknownFormatError < StandardError; end
+
def forbidden!(response)
- raise ForbiddenError.new(response)
+ raise ForbiddenError.new(response, format_response(response))
end
def notfound!(response)
- raise NotFoundError.new(response)
+ raise NotFoundError.new(response, format_response(response))
end
def unexpected!(response)
- raise UnexpectedResponseError.new(response, response.inspect)
+ raise UnexpectedResponseError.new(response, format_response(response))
end
+ def unknownformat!
+ raise UnknownFormatError, "unknown format #{format}"
+ end
+
def serialize(body)
case body
when nil then nil
when String then body
else parser.encode(body)
@@ -89,11 +95,11 @@
end
def parser
case format
when :json then JSONParser
- else "unknown format #{format}"
+ else unknownformat!
end
end
protected
@@ -104,9 +110,19 @@
# Helper to create a string representing a path plus a query string
def format_path_n_query(path, params)
path = "#{path}.#{format}"
path << "?#{URI.encode_www_form(params)}" unless params.nil?
path
+ end
+
+ def format_response(response)
+ body = response.body if text_based?(response)
+ "#{response.inspect} body=#{body}"
+ end
+
+ def text_based?(response)
+ response.content_type =~ /^text/ ||
+ response.content_type =~ /^application/ && !['application/octet-stream', 'application/pdf'].include?(response.content_type)
end
module JSONParser
module_function