Issue 59 - handle 204 responses more gracefully

This commit is contained in:
Steven Bazyl 2012-10-04 18:17:54 -07:00
parent cd37e4bfea
commit 30d0ffc698
2 changed files with 48 additions and 22 deletions

View File

@ -71,7 +71,11 @@ module Google
_, content_type = self.headers.detect do |h, v| _, content_type = self.headers.detect do |h, v|
h.downcase == 'Content-Type'.downcase h.downcase == 'Content-Type'.downcase
end end
content_type[/^([^;]*);?.*$/, 1].strip.downcase if content_type
return content_type[/^([^;]*);?.*$/, 1].strip.downcase
else
return nil
end
end end
## ##
@ -121,7 +125,7 @@ module Google
# @return [TrueClass, FalseClass] # @return [TrueClass, FalseClass]
# true if body can be parsed # true if body can be parsed
def data? def data?
self.media_type == 'application/json' !(self.body.nil? || self.body.empty? || self.media_type != 'application/json')
end end
## ##
@ -132,26 +136,28 @@ module Google
# Object if body parsable from API schema, Hash if JSON, raw body if unable to parse # Object if body parsable from API schema, Hash if JSON, raw body if unable to parse
def data def data
return @data ||= (begin return @data ||= (begin
media_type = self.media_type if self.data?
data = self.body media_type = self.media_type
case media_type data = self.body
when 'application/json' case media_type
data = MultiJson.load(data) when 'application/json'
# Strip data wrapper, if present data = MultiJson.load(data)
data = data['data'] if data.has_key?('data') # Strip data wrapper, if present
else data = data['data'] if data.has_key?('data')
raise ArgumentError, else
"Content-Type not supported for parsing: #{media_type}" raise ArgumentError,
end "Content-Type not supported for parsing: #{media_type}"
if @request.api_method && @request.api_method.response_schema end
# Automatically parse using the schema designated for the if @request.api_method && @request.api_method.response_schema
# response of this API method. # Automatically parse using the schema designated for the
data = @request.api_method.response_schema.new(data) # response of this API method.
data data = @request.api_method.response_schema.new(data)
else data
# Otherwise, return the raw unparsed value. else
# This value must be indexable like a Hash. # Otherwise, return the raw unparsed value.
data # This value must be indexable like a Hash.
data
end
end end
end) end)
end end

View File

@ -180,7 +180,27 @@ describe Google::APIClient::Result do
it 'should return the correct error message' do it 'should return the correct error message' do
@result.error_message.should == 'Parse Error' @result.error_message.should == 'Parse Error'
end end
end
describe 'with 204 No Content response' do
before do
@response.stub(:body).and_return('')
@response.stub(:status).and_return(204)
@response.stub(:headers).and_return({})
@result = Google::APIClient::Result.new(@reference, @response)
end
it 'should indicate no data is available' do
@result.data?.should be_false
end
it 'should return nil for data' do
@result.data.should == nil
end
it 'should return nil for media_type' do
@result.media_type.should == nil
end
end end
end end
end end