Skip to content

Commit eb5eec0

Browse files
committed
small refactoring
1 parent e9107f8 commit eb5eec0

File tree

3 files changed

+31
-22
lines changed

3 files changed

+31
-22
lines changed

lib/zendesk_api/client.rb

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,8 @@ def method_missing(method, *args, &block)
5454
end
5555

5656
def respond_to?(method, *args)
57-
((cache = @resource_cache[method]) && cache[:class]) || !resource_class_for(method).nil? || super
57+
cache = @resource_cache[method]
58+
!!(cache.to_h[:class] || resource_class_for(method) || super)
5859
end
5960

6061
# Returns the current user (aka me)
@@ -185,8 +186,8 @@ def build_connection
185186
private
186187

187188
def resource_class_for(method)
188-
klass_as_string = ZendeskAPI::Helpers.modulize_string(Inflection.singular(method.to_s.gsub(/\W/, '')))
189-
ZendeskAPI::Association.class_from_namespace(klass_as_string)
189+
resource_name = ZendeskAPI::Helpers.modulize_string(Inflection.singular(method.to_s.gsub(/\W/, '')))
190+
ZendeskAPI::Association.class_from_namespace(resource_name)
190191
end
191192

192193
def check_url

lib/zendesk_api/collection.rb

Lines changed: 26 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -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
575583
end

spec/core/collection_spec.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -563,7 +563,7 @@ def to_proc
563563
stub_json_request(:get, %r{test_resources}, json(cbp_response))
564564
end
565565

566-
it "should set the next and previous pages and cursors" do
566+
it "sets the next and previous pages and cursors" do
567567
subject.fetch
568568
expect(subject.instance_variable_get(:@next_page)).to eq("next_page")
569569
expect(subject.instance_variable_get(:@prev_page)).to eq("previous_page")

0 commit comments

Comments
 (0)