lib/zendesk_api/collection.rb in zendesk_api-1.4.3 vs lib/zendesk_api/collection.rb in zendesk_api-1.4.4

- old
+ new

@@ -17,10 +17,13 @@ attr_reader :response # @return [Hash] query options attr_reader :options + # @return [ZendeskAPI::ClientError] The last response error + attr_reader :error + # Creates a new Collection instance. Does not fetch resources. # Additional options are: verb (default: GET), path (default: resource param), page, per_page. # @param [Client] client The {Client} to use. # @param [String] resource The resource being collected. # @param [Hash] options Any additional options to be passed in. @@ -174,10 +177,12 @@ end def fetch(*args) fetch!(*args) rescue Faraday::Error::ClientError => e + @error = e + [] end # Alias for fetch(false) def to_a @@ -371,10 +376,11 @@ end ## Fetch def get_response(path) + @error = nil @response = @client.connection.send(@verb || "get", path) do |req| opts = @options.delete_if {|_, v| v.nil?} req.params.merge!(:include => @includes.join(",")) if @includes.any? @@ -391,9 +397,13 @@ raise ZendeskAPI::Error::NetworkError, @response.env end body = response_body.dup results = body.delete(@resource_class.model_key) || body.delete("results") + + unless results + raise ZendeskAPI::Error::ClientError, "Expected #{@resource_class.model_key} or 'results' in response keys: #{body.keys.inspect}" + end @resources = results.map do |res| wrap_resource(res) end