@@ -348,33 +348,37 @@ def cbp_response?(body)
348348 !!( body [ "meta" ] && body [ "links" ] )
349349 end
350350
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+
351357 def cbp_request?
358+ # CBP requests look like: `/resources?page[size]=100`
359+ # OBP requests look like: `/resources?page=2`
352360 @options [ "page" ] . is_a? ( Hash )
353361 end
354362
355363 def intentional_obp_request?
356364 Helpers . present? ( @options [ "page" ] ) && !cbp_request?
357365 end
358366
359- def supports_cursor_based_pagination ?
367+ def supports_cbp ?
360368 @resource_class . const_defined? ( :CBP_ACTIONS ) && @resource_class . const_get ( :CBP_ACTIONS ) . any? { |supported_path | path . end_with? ( supported_path ) }
361369 end
362370
363- def first_of_multiple_pages?
371+ def first_cbp_request?
372+ # @next_page will be nil when making the first cbp request
364373 @next_page . nil?
365374 end
366375
367- def modify_request_for_cbp
368- @options_per_page_was = @options . delete ( "per_page" )
369- # Default to CBP by using the page param as a map
370- @options . page = { size : ( @options_per_page_was || DEFAULT_PAGE_SIZE ) }
371- end
372-
373376 def get_resources ( path_query_link )
374377 if intentional_obp_request?
375378 warn "Offset Based Pagination will be deprecated soon"
376- elsif supports_cursor_based_pagination? && first_of_multiple_pages?
377- modify_request_for_cbp
379+ elsif supports_cbp? && first_cbp_request?
380+ # only set cbp options if it's the first request, otherwise the options would be already in place
381+ set_cbp_options
378382 end
379383 @response = get_response ( path_query_link )
380384
@@ -391,14 +395,7 @@ def set_page_and_count(body)
391395 @next_page , @prev_page = page_links ( body )
392396
393397 if cbp_response? ( body )
394- @options . page = { } unless cbp_request?
395- # the line above means an intentional CBP request where page[size] is passed on the query
396- # this is to cater for CBP responses where we don't specify page[size] but the endpoint
397- # responds CBP by default. i.e `client.trigger_categories.fetch`
398- @options . page . merge! (
399- after : body [ "meta" ] [ "after_cursor" ] ,
400- before : body [ "meta" ] [ "before_cursor" ]
401- )
398+ set_cbp_response_options ( body )
402399 elsif @next_page =~ /page=(\d +)/
403400 @options [ "page" ] = $1. to_i - 1
404401 elsif @prev_page =~ /page=(\d +)/
@@ -571,5 +568,16 @@ def assert_results(results, body)
571568 return if results
572569 raise ZendeskAPI ::Error ::ClientError , "Expected #{ @resource_class . model_key } or 'results' in response keys: #{ body . keys . inspect } "
573570 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
574582 end
575583end
0 commit comments