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

Support batch update resources #344

Merged
merged 5 commits into from
Sep 9, 2017
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 8 additions & 4 deletions lib/zendesk_api/actions.rb
Original file line number Diff line number Diff line change
Expand Up @@ -276,15 +276,19 @@ def update!(client, attributes = {}, &block)
module UpdateMany
# Updates multiple resources using the update_many endpoint.
# @param [Client] client The {Client} object to be used
# @param [Array] ids An array of ids to update
# @param [Array] ids_or_attributes_array An array of ids or arributes including ids to update
# @param [Hash] attributes The attributes to update resources with
# @return [JobStatus] the {JobStatus} instance for this destroy job
def update_many!(client, ids, attributes)
def update_many!(client, ids_or_attributes_array, attributes = {})
Copy link
Contributor Author

Choose a reason for hiding this comment

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

ids_or_attributes_array naming looks not good to me...

Copy link
Contributor

Choose a reason for hiding this comment

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

I'd leave off the _array ... and make attributes=nil to mke the check simpler

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Sounds good to me but L283 might need conditions.

association = attributes.delete(:association) || Association.new(:class => self)

response = client.connection.put("#{association.generate_path}/update_many") do |req|
req.params = { :ids => ids.join(',') }
req.body = { singular_resource_name => attributes }
if attributes == {}
req.body = { resource_name => ids_or_attributes_array }
else
req.params = { :ids => ids_or_attributes_array.join(',') }
req.body = { singular_resource_name => attributes }
end

yield req if block_given?
end
Expand Down
46 changes: 34 additions & 12 deletions spec/core/bulk_actions_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -47,22 +47,44 @@
subject { ZendeskAPI::BulkTestResource }

context "update_many!" do
let(:attributes) { { :name => 'A', :age => 25 } }
context "arity: 3" do
Copy link
Contributor

Choose a reason for hiding this comment

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

updating a list of ids

let(:attributes) { { :name => 'A', :age => 25 } }

before(:each) do
stub_json_request(:put, %r{bulk_test_resources/update_many}, json(:job_status => { :id => 'ghi' }))
@response = subject.update_many!(client, [1, 2, 3], attributes)
end
before(:each) do
stub_json_request(:put, %r{bulk_test_resources/update_many}, json(:job_status => { :id => 'ghi' }))
@response = subject.update_many!(client, [1, 2, 3], attributes)
end

it 'calls the update_many endpoint' do
assert_requested(:put, %r{bulk_test_resources/update_many\?ids=1,2,3$},
:body => json(:bulk_test_resource => attributes)
)
end

it 'calls the update_many endpoint' do
assert_requested(:put, %r{bulk_test_resources/update_many\?ids=1,2,3$},
:body => json(:bulk_test_resource => attributes)
)
it 'returns a JobStatus' do
expect(@response).to be_instance_of(ZendeskAPI::JobStatus)
expect(@response.id).to eq('ghi')
end
end

it 'returns a JobStatus' do
expect(@response).to be_instance_of(ZendeskAPI::JobStatus)
expect(@response.id).to eq('ghi')
context "arity: 2" do
Copy link
Contributor

Choose a reason for hiding this comment

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

updating with multiple attribute hashes

let(:attributes_array) { [{ :id => 1, :name => 'A' }, { :id => 2, :name => 'B' }] }

before(:each) do
stub_json_request(:put, %r{bulk_test_resources/update_many}, json(:job_status => { :id => 'jkl' }))
@response = subject.update_many!(client, attributes_array)
end

it 'calls the update_many endpoint' do
assert_requested(:put, %r{bulk_test_resources/update_many$},
:body => json(:bulk_test_resources => attributes_array)
)
end

it 'returns a JobStatus' do
expect(@response).to be_instance_of(ZendeskAPI::JobStatus)
expect(@response.id).to eq('jkl')
end
end
end
end
Expand Down