Skip to content

Commit e9107f8

Browse files
committed
cater for CBP responses even when page[size] is not passed on the request
1 parent 4c7a09f commit e9107f8

File tree

2 files changed

+36
-2
lines changed

2 files changed

+36
-2
lines changed

lib/zendesk_api/collection.rb

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -391,8 +391,14 @@ def set_page_and_count(body)
391391
@next_page, @prev_page = page_links(body)
392392

393393
if cbp_response?(body)
394-
@options["page"]["after"] = body["meta"]["after_cursor"]
395-
@options["page"]["before"] = body["meta"]["before_cursor"]
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+
)
396402
elsif @next_page =~ /page=(\d+)/
397403
@options["page"] = $1.to_i - 1
398404
elsif @prev_page =~ /page=(\d+)/

spec/core/collection_spec.rb

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -543,6 +543,34 @@ def to_proc
543543
expect(subject.fetch(true)).to be_empty
544544
end
545545
end
546+
547+
context "when the request returns a Cursor Based Pagination type of response" do
548+
let(:cbp_response) do
549+
{
550+
"meta" => {
551+
"has_more" => true,
552+
"after_cursor" => 'after_cursor',
553+
"before_cursor" => 'before_cursor'
554+
},
555+
"links" => {
556+
"next" => "next_page",
557+
"prev" => "previous_page"
558+
},
559+
"test_resources" => [{ "id" => 1 }]
560+
}
561+
end
562+
before do
563+
stub_json_request(:get, %r{test_resources}, json(cbp_response))
564+
end
565+
566+
it "should set the next and previous pages and cursors" do
567+
subject.fetch
568+
expect(subject.instance_variable_get(:@next_page)).to eq("next_page")
569+
expect(subject.instance_variable_get(:@prev_page)).to eq("previous_page")
570+
expect(subject.options.page.after).to eq("after_cursor")
571+
expect(subject.options.page.before).to eq("before_cursor")
572+
end
573+
end
546574
end
547575

548576
context "save" do

0 commit comments

Comments
 (0)