lib/zendesk_api/collection.rb in zendesk_api-3.0.1 vs lib/zendesk_api/collection.rb in zendesk_api-3.0.2
- old
+ new
@@ -1,16 +1,16 @@
require 'zendesk_api/resource'
require 'zendesk_api/resources'
require 'zendesk_api/search'
+require 'zendesk_api/pagination'
module ZendeskAPI
# Represents a collection of resources. Lazily loaded, resources aren't
# actually fetched until explicitly needed (e.g. #each, {#fetch}).
class Collection
- DEFAULT_PAGE_SIZE = 100
-
include ZendeskAPI::Sideloading
+ include Pagination
# Options passed in that are automatically converted from an array to a comma-separated list.
SPECIALLY_JOINED_PARAMS = [:ids, :only]
# @return [ZendeskAPI::Association] The class association
@@ -114,34 +114,10 @@
def count!
fetch!
@count || -1
end
- # Changes the per_page option. Returns self, so it can be chained. No execution.
- # @return [Collection] self
- def per_page(count)
- clear_cache if count
- @options["per_page"] = count
- self
- end
-
- # Changes the page option. Returns self, so it can be chained. No execution.
- # @return [Collection] self
- def page(number)
- clear_cache if number
- @options["page"] = number
- self
- end
-
- def first_page?
- !@prev_page
- end
-
- def last_page?
- !@next_page || @next_page == @query
- end
-
# Saves all newly created resources stored in this collection.
# @return [Collection] self
def save
_save
end
@@ -321,15 +297,10 @@
def to_param
map(&:to_param)
end
- def more_results?(response)
- Helpers.present?(response["meta"]) && response["meta"]["has_more"]
- end
- alias_method :has_more_results?, :more_results? # For backward compatibility with 1.33.0 and 1.34.0
-
def get_next_page_data(original_response_body)
link = original_response_body["links"]["next"]
result_key = @resource_class.model_key || "results"
while link
response = @client.connection.send("get", link).body
@@ -342,78 +313,27 @@
original_response_body
end
private
- def cbp_response?(body)
- !!(body["meta"] && body["links"])
- end
-
- def cbp_request?
- @options["page"].is_a?(Hash)
- end
-
- def intentional_obp_request?
- Helpers.present?(@options["page"]) && !cbp_request?
- end
-
- def should_try_cbp?(path_query_link)
- not_supported_endpoints = %w[show_many]
- not_supported_endpoints.none? { |endpoint| path_query_link.end_with?(endpoint) }
- end
-
def get_resources(path_query_link)
if intentional_obp_request?
warn "Offset Based Pagination will be deprecated soon"
- elsif @next_page.nil? && should_try_cbp?(path_query_link)
- @options_per_page_was = @options.delete("per_page")
- # Default to CBP by using the page param as a map
- @options.page = { size: (@options_per_page_was || DEFAULT_PAGE_SIZE) }
+ elsif supports_cbp? && first_cbp_request?
+ # only set cbp options if it's the first request, otherwise the options would be already in place
+ set_cbp_options
end
+ @response = get_response(path_query_link)
- begin
- # Try CBP first, unless the user explicitly asked for OBP
- @response = get_response(path_query_link)
- rescue ZendeskAPI::Error::NetworkError => e
- raise e if intentional_obp_request?
- # Fallback to OBP if CBP didn't work, using per_page param
- @options.per_page = @options_per_page_was
- @options.page = nil
- @response = get_response(path_query_link)
- end
-
# Keep pre-existing behaviour for search/export
if path_query_link == "search/export"
handle_search_export_response(@response.body)
else
handle_response(@response.body)
end
end
- def set_page_and_count(body)
- @count = (body["count"] || @resources.size).to_i
- @next_page, @prev_page = page_links(body)
-
- if cbp_response?(body)
- # We'll delete the one we don't need in #next or #prev
- @options["page"]["after"] = body["meta"]["after_cursor"]
- @options["page"]["before"] = body["meta"]["before_cursor"]
- elsif @next_page =~ /page=(\d+)/
- @options["page"] = $1.to_i - 1
- elsif @prev_page =~ /page=(\d+)/
- @options["page"] = $1.to_i + 1
- end
- end
-
- def page_links(body)
- if body["meta"] && body["links"]
- [body["links"]["next"], body["links"]["prev"]]
- else
- [body["next_page"], body["previous_page"]]
- end
- end
-
def _all(start_page = @options["page"], bang = false, &block)
raise(ArgumentError, "must pass a block") unless block
page(start_page)
clear_cache
@@ -539,12 +459,16 @@
def array_method(name, *args, &block)
to_a.public_send(name, *args, &block)
end
+ # If you call client.tickets.foo - and foo is not an attribute nor an association, it ends up here, as a new collection
def next_collection(name, *args, &block)
opts = args.last.is_a?(Hash) ? args.last : {}
- opts.merge!(:collection_path => @collection_path.dup.push(name))
+ opts.merge!(collection_path: [*@collection_path, name], page: nil)
+ # why page: nil ?
+ # when you do client.tickets.fetch followed by client.tickets.foos => the request to /tickets/foos will
+ # have the options page set to whatever the last options were for the tickets collection
self.class.new(@client, @resource_class, @options.merge(opts))
end
def collection_method(name, *args, &block)
@resource_class.send(name, @client, *args, &block)