11require 'zendesk_api/resource'
22require 'zendesk_api/resources'
33require 'zendesk_api/search'
4+ require 'zendesk_api/pagination'
45
56module ZendeskAPI
67 # Represents a collection of resources. Lazily loaded, resources aren't
78 # actually fetched until explicitly needed (e.g. #each, {#fetch}).
89 class Collection
9- DEFAULT_PAGE_SIZE = 100
10-
1110 include ZendeskAPI ::Sideloading
11+ include ZendeskAPI ::Pagination
1212
1313 # Options passed in that are automatically converted from an array to a comma-separated list.
1414 SPECIALLY_JOINED_PARAMS = [ :ids , :only ]
@@ -116,30 +116,6 @@ def count!
116116 @count || -1
117117 end
118118
119- # Changes the per_page option. Returns self, so it can be chained. No execution.
120- # @return [Collection] self
121- def per_page ( count )
122- clear_cache if count
123- @options [ "per_page" ] = count
124- self
125- end
126-
127- # Changes the page option. Returns self, so it can be chained. No execution.
128- # @return [Collection] self
129- def page ( number )
130- clear_cache if number
131- @options [ "page" ] = number
132- self
133- end
134-
135- def first_page?
136- !@prev_page
137- end
138-
139- def last_page?
140- !@next_page || @next_page == @query
141- end
142-
143119 # Saves all newly created resources stored in this collection.
144120 # @return [Collection] self
145121 def save
@@ -222,16 +198,6 @@ def all(start_page = @options["page"], &block)
222198 _all ( start_page , &block )
223199 end
224200
225- def each_page! ( *args , &block )
226- warn "ZendeskAPI::Collection#each_page! is deprecated, please use ZendeskAPI::Collection#all!"
227- all! ( *args , &block )
228- end
229-
230- def each_page ( *args , &block )
231- warn "ZendeskAPI::Collection#each_page is deprecated, please use ZendeskAPI::Collection#all"
232- all ( *args , &block )
233- end
234-
235201 # Replaces the current (loaded or not) resources with the passed in collection
236202 # @option collection [Array] The collection to replace this one with
237203 # @raise [ArgumentError] if any resources passed in don't belong in this collection
@@ -323,11 +289,6 @@ def to_param
323289 map ( &:to_param )
324290 end
325291
326- def more_results? ( response )
327- Helpers . present? ( response [ "meta" ] ) && response [ "meta" ] [ "has_more" ]
328- end
329- alias_method :has_more_results? , :more_results? # For backward compatibility with 1.33.0 and 1.34.0
330-
331292 def get_next_page_data ( original_response_body )
332293 link = original_response_body [ "links" ] [ "next" ]
333294 result_key = @resource_class . model_key || "results"
@@ -344,35 +305,6 @@ def get_next_page_data(original_response_body)
344305
345306 private
346307
347- def cbp_response? ( body )
348- !!( body [ "meta" ] && body [ "links" ] )
349- end
350-
351- def set_cbp_options
352- @options_per_page_was = @options . delete ( "per_page" )
353- # Default to CBP by using the page param as a map
354- @options . page = { size : ( @options_per_page_was || DEFAULT_PAGE_SIZE ) }
355- end
356-
357- # CBP requests look like: `/resources?page[size]=100`
358- # OBP requests look like: `/resources?page=2`
359- def cbp_request?
360- @options [ "page" ] . is_a? ( Hash )
361- end
362-
363- def intentional_obp_request?
364- Helpers . present? ( @options [ "page" ] ) && !cbp_request?
365- end
366-
367- def supports_cbp?
368- @resource_class . cbp_path_regexes . any? { |supported_path_regex | path . match? ( supported_path_regex ) }
369- end
370-
371- def first_cbp_request?
372- # @next_page will be nil when making the first cbp request
373- @next_page . nil?
374- end
375-
376308 def get_resources ( path_query_link )
377309 if intentional_obp_request?
378310 warn "Offset Based Pagination will be deprecated soon"
@@ -390,27 +322,6 @@ def get_resources(path_query_link)
390322 end
391323 end
392324
393- def set_page_and_count ( body )
394- @count = ( body [ "count" ] || @resources . size ) . to_i
395- @next_page , @prev_page = page_links ( body )
396-
397- if cbp_response? ( body )
398- set_cbp_response_options ( body )
399- elsif @next_page =~ /page=(\d +)/
400- @options [ "page" ] = $1. to_i - 1
401- elsif @prev_page =~ /page=(\d +)/
402- @options [ "page" ] = $1. to_i + 1
403- end
404- end
405-
406- def page_links ( body )
407- if body [ "meta" ] && body [ "links" ]
408- [ body [ "links" ] [ "next" ] , body [ "links" ] [ "prev" ] ]
409- else
410- [ body [ "next_page" ] , body [ "previous_page" ] ]
411- end
412- end
413-
414325 def _all ( start_page = @options [ "page" ] , bang = false , &block )
415326 raise ( ArgumentError , "must pass a block" ) unless block
416327
@@ -568,16 +479,5 @@ def assert_results(results, body)
568479 return if results
569480 raise ZendeskAPI ::Error ::ClientError , "Expected #{ @resource_class . model_key } or 'results' in response keys: #{ body . keys . inspect } "
570481 end
571-
572- def set_cbp_response_options ( body )
573- @options . page = { } unless cbp_request?
574- # the line above means an intentional CBP request where page[size] is passed on the query
575- # this is to cater for CBP responses where we don't specify page[size] but the endpoint
576- # responds CBP by default. i.e `client.trigger_categories.fetch`
577- @options . page . merge! (
578- before : body [ "meta" ] [ "before_cursor" ] ,
579- after : body [ "meta" ] [ "after_cursor" ]
580- )
581- end
582482 end
583483end
0 commit comments