lib/hyper_resource/modules/http.rb in hyperresource-0.9.2 vs lib/hyper_resource/modules/http.rb in hyperresource-0.9.3
- old
+ new
@@ -260,22 +260,23 @@
status = response.status
is_success = (status / 100 == 2)
adapter = self.resource.adapter || HyperResource::Adapter::HAL_JSON
body = nil
- begin
- if response.body
- body = adapter.deserialize(response.body)
+ unless empty_body?(response.body)
+ begin
+ body = adapter.deserialize(response.body)
+ rescue StandardError => e
+ if is_success
+ raise HyperResource::ResponseError.new(
+ "Error when deserializing response body",
+ :response => response,
+ :cause => e
+ )
+ end
+
end
- rescue StandardError => e
- if is_success
- raise HyperResource::ResponseError.new(
- "Error when deserializing response body",
- :response => response,
- :cause => e
- )
- end
end
new_rsrc = resource.new_from(:link => self,
:body => body,
:response => response)
@@ -297,9 +298,16 @@
raise HyperResource::ResponseError.new("Unknown status #{status}",
:response => response,
:body => body)
end
+ end
+
+ def empty_body?(body)
+ return true if body.nil?
+ return true if body.respond_to?(:empty?) && body.empty?
+ return true if body.class == String && body =~ /^['"]+$/ # special case for status code with optional body, example Grape API with status 405
+ false
end
end
end
end