Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[RED-1821] Cater for endpoints with CBP support #548

Merged
merged 8 commits into from
Aug 1, 2023

Conversation

fbvilela
Copy link
Contributor

Description

Adding endpoints that support CBP.

spec/live/cbp_support.rb Outdated Show resolved Hide resolved
describe '/groups/{id}/memberships' do
before do
VCR.use_cassette("cbp_group_memberships_all_groups") do
@groups = client.groups.fetch
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could we use let instead of instance vars?

https://www.betterspecs.org/#let

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done it ✅

@response_body = collection.response.body
end
end
it 'expects an array with the correct element types' do
Copy link
Contributor

@ecoologic ecoologic Jul 26, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe?

Suggested change
it 'expects an array with the correct element types' do
it "returns a list of #{@resource_klass}" do

I think in this context it should be available, you're inside a method....

However, I'm not a fan of extracting whole tests in methods. But I can't verbalize that, so maybe it's a subjective thing. I'm ok with a bit of duplication in the code, or using a list of test params like we do in Go.

The context could also use some more wording.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done it ✅
also using the described_class var provided by rspec, much nicer

def expect_cbp_response_for(collection)
context collection.path.to_s, :vcr do
before do
@resource_klass = collection.instance_variable_get(:@resource_class)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Any way around poking inside the private variables of classes?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yes, this was taken care of

@@ -365,7 +365,8 @@ def intentional_obp_request?
end

def supports_cbp?
@resource_class.const_defined?(:CBP_ACTIONS) && @resource_class.const_get(:CBP_ACTIONS).any? { |supported_path| path.end_with?(supported_path) }
@resource_class.const_defined?(:CBP_ACTIONS) &&
@resource_class.const_get(:CBP_ACTIONS).any? { |supported_path_regex| path.match?(supported_path_regex) }
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

if this was a class method in Resource, returning an empty array, this part would simplify to:

@resource_class.cbp_path_regexes.any? { |r| r =~ path }

Note that now if the type of data a method returns changes, likely the name needs to reflect that change.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done ✅
I created a module for the CBP support code. was that what you were thinking?

Copy link
Contributor

@ecoologic ecoologic Jul 31, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

no... the idea was to create a method in resource, returning an empty array, and override the method in the specific resources.

I'm ok with what we have but if you're curious we can chat about it 👍

Copy link
Contributor

@ecoologic ecoologic left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

✅ This is great work, see my latest comments and if there's anything you'd like to implement. When you're happy with it, I'm happy too.

@fbvilela fbvilela marked this pull request as ready for review July 27, 2023 13:18
@fbvilela fbvilela requested a review from a team as a code owner July 27, 2023 13:18
def cbp_path_regexes
const_defined?(:CBP_ACTIONS) ? const_get(:CBP_ACTIONS) : []
end
end
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this overcomplicates things, too much indirection.

@@ -378,7 +382,7 @@ class Ticket < Resource
extend UpdateMany
extend DestroyMany

CBP_ACTIONS = %w[tickets].freeze
CBP_ACTIONS = [/tickets$/].freeze
Copy link
Contributor

@ecoologic ecoologic Jul 31, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
CBP_ACTIONS = [/tickets$/].freeze
def self.cbp_path_regexes
[/tickets$/].freeze
end

before: body["meta"]["before_cursor"],
after: body["meta"]["after_cursor"]
)
end
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's always great to see files get smaller 😄

def each_page!(*args, &block)
warn "ZendeskAPI::Collection#each_page! is deprecated, please use ZendeskAPI::Collection#all!"
all!(*args, &block)
end
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this (and below) is not pagination logic?

@@ -0,0 +1,107 @@
module ZendeskAPI
# Contains all methods related to pagination in an attempt to slim down collection.rb
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Minor: If you wanted to be super-clear about the fact it's only for collection, you could even namespace it by module Collection here. That way, it wouldn't pollute the namespace in ZendeskAPI when it should only be used in Collection.

)
end
end
end
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice! Scrolling through the file you can kinda understand how pagination is handled. 👍

def last_page?
!@next_page || @next_page == @query
end

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Moving public methods make is a bit harder to follow in case they're called from outside of Collection, but in this case I'm fine with it. 👍

end

class Group < Resource
has_many :memberships, :class => GroupMembership, :path => "memberships"
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Very minor, but why not? 😄

Suggested change
has_many :memberships, :class => GroupMembership, :path => "memberships"
has_many :memberships, class: GroupMembership, path: "memberships"


describe 'Endpoints that support CBP' do
describe ZendeskAPI::Group do
describe '/groups' do
Copy link
Contributor

@ecoologic ecoologic Aug 1, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Minor: I think RSpec is about describing Object#methods. This makes the test suite into documentation. IE: describe "#fetch"

https://www.betterspecs.org/#describe

RSpec.describe ZendeskAPI::Group do
  subject(:collection) { client.groups }

  describe '#fetch' do
    it_behaves_like 'an action that uses CBP'
  end
end

@fbvilela fbvilela merged commit cfc8dc1 into master Aug 1, 2023
7 checks passed
@fbvilela fbvilela deleted the fvilela/add-cbp-endpoints branch August 1, 2023 06:06
@ecoologic ecoologic mentioned this pull request Aug 20, 2023
Closed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants