lib/zendesk_api/collection.rb in zendesk_api-1.1.3 vs lib/zendesk_api/collection.rb in zendesk_api-1.2.1

- old
+ new

@@ -165,11 +165,11 @@ elsif association && association.options.parent && association.options.parent.new_record? return (@resources = []) end @response = get_response(@query || self.path) - handle_response(@response.body.dup) + handle_response(@response.body) @query = nil @resources end @@ -373,11 +373,16 @@ req.params.merge!(opts) end end end - def handle_response(body) + def handle_response(response_body) + unless response_body + raise ZendeskAPI::Error::NetworkError, @response.env + end + + body = response_body.dup results = body.delete(@resource_class.model_key) || body.delete("results") @resources = results.map do |res| wrap_resource(res) end @@ -385,21 +390,27 @@ set_page_and_count(body) set_includes(@resources, @includes, body) end # Simplified Associations#wrap_resource - def wrap_resource(res, with_association = [Tag, Setting].include?(@resource_class)) + def wrap_resource(res, with_association = with_association?) case res when Array - wrap_resource(Hash[*res]) + wrap_resource(Hash[*res], with_association) when Hash res = res.merge(:association => @association) if with_association @resource_class.new(@client, res) else res = { :id => res } res.merge!(:association => @association) if with_association @resource_class.new(@client, res) end + end + + # Two special cases, and all namespaced classes + def with_association? + [Tag, Setting].include?(@resource_class) || + @resource_class.to_s.split("::").size > 2 end ## Method missing def array_method(name, *args, &block)