Skip to content

Commit 4505ac7

Browse files
committed
refactor and add cbp support to organizations and its subsriptions
1 parent 6253045 commit 4505ac7

File tree

9 files changed

+95
-61
lines changed

9 files changed

+95
-61
lines changed

lib/zendesk_api/cbp_support.rb

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
module ZendeskAPI
2+
# Represents the support to Cursor Based Pagination endpoints
3+
module CBPSupport
4+
def self.included(base)
5+
base.extend(ClassMethods)
6+
end
7+
8+
# Returns the paths that support CBP
9+
# @return [Array] of regular expressions
10+
# To add CBP support to a resource, add a constant called CBP_ACTIONS. See examples in resources.rb
11+
module ClassMethods
12+
def cbp_path_regexes
13+
const_defined?(:CBP_ACTIONS) ? const_get(:CBP_ACTIONS) : []
14+
end
15+
end
16+
end
17+
end

lib/zendesk_api/collection.rb

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -365,8 +365,7 @@ def intentional_obp_request?
365365
end
366366

367367
def supports_cbp?
368-
@resource_class.const_defined?(:CBP_ACTIONS) &&
369-
@resource_class.const_get(:CBP_ACTIONS).any? { |supported_path_regex| path.match?(supported_path_regex) }
368+
@resource_class.cbp_path_regexes.any? { |supported_path_regex| path.match?(supported_path_regex) }
370369
end
371370

372371
def first_cbp_request?

lib/zendesk_api/resource.rb

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
require 'zendesk_api/association'
55
require 'zendesk_api/associations'
66
require 'zendesk_api/verbs'
7+
require 'zendesk_api/cbp_support'
78

89
# See docs: https://developer.zendesk.com/api-reference/
910
module ZendeskAPI
@@ -163,6 +164,7 @@ def attribute_changes
163164
class DataResource < Data
164165
attr_accessor :error, :error_message
165166
extend Verbs
167+
include CBPSupport
166168
end
167169

168170
# Represents a resource that can only GET

lib/zendesk_api/resources.rb

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -152,6 +152,8 @@ class Organization < Resource
152152
def self.incremental_export(client, start_time)
153153
ZendeskAPI::Collection.new(client, self, :path => "incremental/organizations?start_time=#{start_time.to_i}")
154154
end
155+
156+
CBP_ACTIONS = [/organizations$/].freeze
155157
end
156158

157159
class Brand < Resource
@@ -180,6 +182,8 @@ class OrganizationSubscription < ReadResource
180182

181183
has User
182184
has Organization
185+
186+
CBP_ACTIONS = [%r{organizations/\d+/subscriptions$}].freeze
183187
end
184188

185189
class Category < Resource
@@ -378,7 +382,7 @@ class Ticket < Resource
378382
extend UpdateMany
379383
extend DestroyMany
380384

381-
CBP_ACTIONS = %w[tickets].freeze
385+
CBP_ACTIONS = [/tickets$/].freeze
382386

383387
# Unlike other attributes, "comment" is not a property of the ticket,
384388
# but is used as a "comment on save", so it should be kept unchanged,

lib/zendesk_api/search.rb

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,10 @@ def self.new(client, attributes)
2525
(klass || Result).new(client, attributes)
2626
end
2727

28+
def self.cbp_path_regexes
29+
[]
30+
end
31+
2832
class Result < Data; end
2933

3034
class << self

spec/core/cbp_helper.rb

Lines changed: 0 additions & 21 deletions
This file was deleted.
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
shared_examples 'an endpoint that supports CBP' do
2+
let(:collection_fetched) do
3+
VCR.use_cassette("cbp_#{described_class}_collection_fetch") do
4+
collection.fetch
5+
collection
6+
end
7+
end
8+
9+
let(:response_body) { collection_fetched.response.body }
10+
let(:collection_fetched_results) { collection_fetched.to_a }
11+
12+
it 'returns a CBP response with all the correct keys' do
13+
expect(response_body).to have_key('meta')
14+
expect(response_body).to have_key('links')
15+
expect(response_body['meta'].keys).to match_array(%w[has_more after_cursor before_cursor])
16+
expect(response_body['links'].keys).to match_array(%w[prev next])
17+
end
18+
19+
it "returns a list of #{described_class} objects" do
20+
expect(collection_fetched_results).to all(be_a(described_class))
21+
end
22+
end

spec/live/cbp_support.rb

Lines changed: 0 additions & 37 deletions
This file was deleted.

spec/live/cbp_support_spec.rb

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
require 'core/spec_helper'
2+
require 'core/resources/cbp_spec_helper'
3+
4+
describe 'Endpoints that support CBP' do
5+
describe ZendeskAPI::Group do
6+
describe '/groups' do
7+
it_behaves_like 'an endpoint that supports CBP' do
8+
let(:collection) { client.groups }
9+
end
10+
end
11+
12+
describe '/groups/assignable' do
13+
it_behaves_like 'an endpoint that supports CBP' do
14+
let(:collection) { client.groups.assignable }
15+
end
16+
end
17+
end
18+
19+
describe ZendeskAPI::GroupMembership do
20+
describe '/groups/:id/memberships' do
21+
let(:one_group) { VCR.use_cassette("cbp_group_memberships_all_groups") { client.groups.fetch.last } }
22+
it_behaves_like 'an endpoint that supports CBP' do
23+
let(:collection) { one_group.memberships }
24+
end
25+
end
26+
end
27+
28+
describe ZendeskAPI::Organization do
29+
describe '/organizations' do
30+
it_behaves_like 'an endpoint that supports CBP' do
31+
let(:collection) { client.organizations }
32+
end
33+
end
34+
end
35+
36+
describe ZendeskAPI::OrganizationMembership do
37+
describe '/organizations/:id/subscriptions' do
38+
let(:one_organization) { VCR.use_cassette("cbp_organization_subscriptions_all_organizations") { client.organizations.fetch.last } }
39+
it_behaves_like 'an endpoint that supports CBP' do
40+
let(:collection) { one_organization.subscriptions }
41+
end
42+
end
43+
end
44+
end

0 commit comments

Comments
 (0)