Skip to content

Commit aa1760e

Browse files
committed
extract pagination methods into its own module
1 parent 6bfbcc6 commit aa1760e

File tree

3 files changed

+110
-102
lines changed

3 files changed

+110
-102
lines changed

.rubocop_todo.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -176,6 +176,7 @@ Naming/AccessorMethodName:
176176
- 'lib/zendesk_api/collection.rb'
177177
- 'lib/zendesk_api/lru_cache.rb'
178178
- 'lib/zendesk_api/resources.rb'
179+
- 'lib/zendesk_api/pagination.rb'
179180

180181
# Offense count: 1
181182
# Configuration parameters: EnforcedStyleForLeadingUnderscores.

lib/zendesk_api/collection.rb

Lines changed: 2 additions & 102 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,14 @@
11
require 'zendesk_api/resource'
22
require 'zendesk_api/resources'
33
require 'zendesk_api/search'
4+
require 'zendesk_api/pagination'
45

56
module 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
583483
end

lib/zendesk_api/pagination.rb

Lines changed: 107 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,107 @@
1+
module ZendeskAPI
2+
# Contains all methods related to pagination in an attempt to slim down collection.rb
3+
module Pagination
4+
DEFAULT_PAGE_SIZE = 100
5+
def more_results?(response)
6+
Helpers.present?(response["meta"]) && response["meta"]["has_more"]
7+
end
8+
alias has_more_results? more_results? # For backward compatibility with 1.33.0 and 1.34.0
9+
10+
def each_page!(*args, &block)
11+
warn "ZendeskAPI::Collection#each_page! is deprecated, please use ZendeskAPI::Collection#all!"
12+
all!(*args, &block)
13+
end
14+
15+
def each_page(*args, &block)
16+
warn "ZendeskAPI::Collection#each_page is deprecated, please use ZendeskAPI::Collection#all"
17+
all(*args, &block)
18+
end
19+
20+
# Changes the per_page option. Returns self, so it can be chained. No execution.
21+
# @return [Collection] self
22+
def per_page(count)
23+
clear_cache if count
24+
@options["per_page"] = count
25+
self
26+
end
27+
28+
# Changes the page option. Returns self, so it can be chained. No execution.
29+
# @return [Collection] self
30+
def page(number)
31+
clear_cache if number
32+
@options["page"] = number
33+
self
34+
end
35+
36+
def first_page?
37+
!@prev_page
38+
end
39+
40+
def last_page?
41+
!@next_page || @next_page == @query
42+
end
43+
44+
private
45+
46+
def page_links(body)
47+
if body["meta"] && body["links"]
48+
[body["links"]["next"], body["links"]["prev"]]
49+
else
50+
[body["next_page"], body["previous_page"]]
51+
end
52+
end
53+
54+
def cbp_response?(body)
55+
!!(body["meta"] && body["links"])
56+
end
57+
58+
def set_cbp_options
59+
@options_per_page_was = @options.delete("per_page")
60+
# Default to CBP by using the page param as a map
61+
@options.page = { size: (@options_per_page_was || DEFAULT_PAGE_SIZE) }
62+
end
63+
64+
# CBP requests look like: `/resources?page[size]=100`
65+
# OBP requests look like: `/resources?page=2`
66+
def cbp_request?
67+
@options["page"].is_a?(Hash)
68+
end
69+
70+
def intentional_obp_request?
71+
Helpers.present?(@options["page"]) && !cbp_request?
72+
end
73+
74+
def supports_cbp?
75+
@resource_class.cbp_path_regexes.any? { |supported_path_regex| path.match?(supported_path_regex) }
76+
end
77+
78+
def first_cbp_request?
79+
# @next_page will be nil when making the first cbp request
80+
@next_page.nil?
81+
end
82+
83+
def set_page_and_count(body)
84+
@count = (body["count"] || @resources.size).to_i
85+
@next_page, @prev_page = page_links(body)
86+
87+
if cbp_response?(body)
88+
set_cbp_response_options(body)
89+
elsif @next_page =~ /page=(\d+)/
90+
@options["page"] = Regexp.last_match(1).to_i - 1
91+
elsif @prev_page =~ /page=(\d+)/
92+
@options["page"] = Regexp.last_match(1).to_i + 1
93+
end
94+
end
95+
96+
def set_cbp_response_options(body)
97+
@options.page = {} unless cbp_request?
98+
# the line above means an intentional CBP request where page[size] is passed on the query
99+
# this is to cater for CBP responses where we don't specify page[size] but the endpoint
100+
# responds CBP by default. i.e `client.trigger_categories.fetch`
101+
@options.page.merge!(
102+
before: body["meta"]["before_cursor"],
103+
after: body["meta"]["after_cursor"]
104+
)
105+
end
106+
end
107+
end

0 commit comments

Comments
 (0)