From 0b65f3b828116ef3dafe9ae44e9fe05c3dd2aac4 Mon Sep 17 00:00:00 2001 From: beeleethebee Date: Thu, 24 Oct 2024 17:25:42 +0200 Subject: [PATCH 1/8] =?UTF-8?q?=E2=9C=A8=20send=20tags=20over=20the=20API?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- lib/zoho_hub/base_record.rb | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/lib/zoho_hub/base_record.rb b/lib/zoho_hub/base_record.rb index 5899aa7..008a056 100644 --- a/lib/zoho_hub/base_record.rb +++ b/lib/zoho_hub/base_record.rb @@ -223,6 +223,12 @@ def update(params) build_response(body) end + def associate_tag(tag_name) + body = post(File.join(self.class.request_path, id, "actions", "add_tags?tag_names=#{tag_name}")) + response = build_response(body) + response.data.first + end + def delete_record body = delete(File.join(self.class.request_path, id)) From 480aa0ee018bed3f1aea5537c70dba2794733864 Mon Sep 17 00:00:00 2001 From: beeleethebee Date: Thu, 24 Oct 2024 17:28:46 +0200 Subject: [PATCH 2/8] =?UTF-8?q?=F0=9F=9A=A8?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- lib/zoho_hub/base_record.rb | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/lib/zoho_hub/base_record.rb b/lib/zoho_hub/base_record.rb index 008a056..36661d5 100644 --- a/lib/zoho_hub/base_record.rb +++ b/lib/zoho_hub/base_record.rb @@ -223,10 +223,11 @@ def update(params) build_response(body) end - def associate_tag(tag_name) - body = post(File.join(self.class.request_path, id, "actions", "add_tags?tag_names=#{tag_name}")) - response = build_response(body) - response.data.first + def associate_tags(tag_names = []) + url = File.join( + self.class.request_path, id, 'actions', "add_tags?tag_names=#{tag_names.join(', ')}" + ) + build_response(post(url)).data.first end def delete_record From ff70fc5a0ef23b92cb892e12cff2e4a205020594 Mon Sep 17 00:00:00 2001 From: beeleethebee Date: Thu, 24 Oct 2024 18:10:59 +0200 Subject: [PATCH 3/8] =?UTF-8?q?=E2=9C=A8=20add=20associate=5Ftags=20for=20?= =?UTF-8?q?base=5Frecord=20class?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- lib/zoho_hub/base_record.rb | 12 +++++++++++- 1 file changed, 11 insertions(+), 1 deletion(-) diff --git a/lib/zoho_hub/base_record.rb b/lib/zoho_hub/base_record.rb index 36661d5..dd07521 100644 --- a/lib/zoho_hub/base_record.rb +++ b/lib/zoho_hub/base_record.rb @@ -165,6 +165,16 @@ def exists?(id) false end + def associate_tags(ressource_ids, tag_names = []) + responses = [] + ressource_ids.each_slice(100) do |ids| + parameters = "add_tags?tag_names=#{tag_names.join(',')}&ids=#{ids.join(',')}" + url = File.join(self.class.request_path, 'actions', parameters) + responses << build_response(post(url)).data.first + end + responses + end + alias exist? exists? # rubocop:disable Metrics/AbcSize @@ -225,7 +235,7 @@ def update(params) def associate_tags(tag_names = []) url = File.join( - self.class.request_path, id, 'actions', "add_tags?tag_names=#{tag_names.join(', ')}" + self.class.request_path, id, 'actions', "add_tags?tag_names=#{tag_names.join(',')}" ) build_response(post(url)).data.first end From 96586b34a30dd543e0639957c51fbc3d2b99b59f Mon Sep 17 00:00:00 2001 From: beeleethebee Date: Fri, 25 Oct 2024 08:50:36 +0200 Subject: [PATCH 4/8] :see_no_evil: ignore .idea for inteliJ users --- .gitignore | 1 + 1 file changed, 1 insertion(+) diff --git a/.gitignore b/.gitignore index 491c5b6..6b09925 100644 --- a/.gitignore +++ b/.gitignore @@ -12,3 +12,4 @@ Gemfile.lock # rspec failure tracking .rspec_status +.idea/ \ No newline at end of file From 906a79a68f2656a982bfd9efbcd583dd16a92be6 Mon Sep 17 00:00:00 2001 From: beeleethebee Date: Fri, 25 Oct 2024 08:51:54 +0200 Subject: [PATCH 5/8] :white_check_mark: test tags related methods --- lib/zoho_hub/base_record.rb | 6 ++-- spec/zoho_hub/base_record_spec.rb | 47 +++++++++++++++++++++++++++++++ 2 files changed, 50 insertions(+), 3 deletions(-) diff --git a/lib/zoho_hub/base_record.rb b/lib/zoho_hub/base_record.rb index dd07521..b4b65bc 100644 --- a/lib/zoho_hub/base_record.rb +++ b/lib/zoho_hub/base_record.rb @@ -165,11 +165,11 @@ def exists?(id) false end - def associate_tags(ressource_ids, tag_names = []) + def associate_tags(resource_ids, tag_names = []) responses = [] - ressource_ids.each_slice(100) do |ids| + resource_ids.each_slice(100) do |ids| parameters = "add_tags?tag_names=#{tag_names.join(',')}&ids=#{ids.join(',')}" - url = File.join(self.class.request_path, 'actions', parameters) + url = File.join(request_path, 'actions', parameters) responses << build_response(post(url)).data.first end responses diff --git a/spec/zoho_hub/base_record_spec.rb b/spec/zoho_hub/base_record_spec.rb index 2d89cc1..5201f7f 100644 --- a/spec/zoho_hub/base_record_spec.rb +++ b/spec/zoho_hub/base_record_spec.rb @@ -41,6 +41,37 @@ end end + describe '.associate_tags' do + before { allow(test_class).to receive(:request_path).and_return('Leads') } + + let!(:stub_add_tags_request) do + stub_request(:post, 'https://crmsandbox.zoho.eu/crm/v2/Leads/actions/add_tags?tag_names=tag1,tag2&ids=1,2') + .to_return(status: 200, body: '', headers: { "Content-Type": 'application/json' }) + end + + it 'associate tags to records' do + test_class.associate_tags([1, 2], %w[tag1 tag2]) + expect(stub_add_tags_request).to have_been_requested + end + + context 'when the request is too big' do + let!(:stub_add_tags_request_first_batch) do + stub_request(:post, "https://crmsandbox.zoho.eu/crm/v2/Leads/actions/add_tags?tag_names=tag1,tag2&ids=#{(1..100).to_a.join(',')}") + .to_return(status: 200, body: '', headers: { "Content-Type": 'application/json' }) + end + let!(:stub_add_tags_request_second_batch) do + stub_request(:post, "https://crmsandbox.zoho.eu/crm/v2/Leads/actions/add_tags?tag_names=tag1,tag2&ids=#{(101..200).to_a.join(',')}") + .to_return(status: 200, body: '', headers: { "Content-Type": 'application/json' }) + end + + it 'splits the request in batches' do + test_class.associate_tags((1..200).to_a, %w[tag1 tag2]) + expect(stub_add_tags_request_first_batch).to have_been_requested + expect(stub_add_tags_request_second_batch).to have_been_requested + end + end + end + describe '#build_response' do context 'with an empty string and a "false" boolean' do let(:body) { { data: [{ My_String: '', My_Bool: false }] } } @@ -83,6 +114,22 @@ end end + describe '#associate_tags' do + let(:test_instance) { test_class.new(id: '123456789') } + let(:tag_names) { %w[tag1 tag2] } + let!(:associate_tags_stub) do + stub_request(:post, "https://crmsandbox.zoho.eu/crm/v2/Leads/#{test_instance.id}/actions/add_tags?tag_names=tag1,tag2") + .to_return(status: 200, body: {}.to_json, headers: { "Content-Type": 'application/json' }) + end + + before { allow(test_class).to receive(:request_path).and_return('Leads') } + + it 'associates tags to the record' do + test_instance.associate_tags(tag_names) + expect(associate_tags_stub).to have_been_requested + end + end + describe '#notes' do let(:test_instance) { test_class.new(id: '123456789') } let(:data_notes) { { data: [{ Note_Title: 'Title', Note_Content: 'content' }] } } From a617bf7cce8e3079a88c37102058ab85ea63f85c Mon Sep 17 00:00:00 2001 From: beeleethebee Date: Fri, 25 Oct 2024 08:53:18 +0200 Subject: [PATCH 6/8] :rotating_light: --- spec/zoho_hub/base_record_spec.rb | 2 +- spec/zoho_hub/connection_spec.rb | 6 +++--- spec/zoho_hub/with_attributes_spec.rb | 2 +- spec/zoho_hub_spec.rb | 2 +- 4 files changed, 6 insertions(+), 6 deletions(-) diff --git a/spec/zoho_hub/base_record_spec.rb b/spec/zoho_hub/base_record_spec.rb index 5201f7f..7ded44a 100644 --- a/spec/zoho_hub/base_record_spec.rb +++ b/spec/zoho_hub/base_record_spec.rb @@ -81,7 +81,7 @@ record = test_class.new(response.data.first) expect(record.my_string).to eq('') - expect(record.my_bool).to eq(false) + expect(record.my_bool).to be(false) end end end diff --git a/spec/zoho_hub/connection_spec.rb b/spec/zoho_hub/connection_spec.rb index 31d9303..0699c08 100644 --- a/spec/zoho_hub/connection_spec.rb +++ b/spec/zoho_hub/connection_spec.rb @@ -107,7 +107,7 @@ expect(connection.access_token).to eq('foo') expect do connection - .send(:with_refresh) { instance_double('Response', body: invalid_http_response) } + .send(:with_refresh) { instance_double(Response, body: invalid_http_response) } end.to change(connection, :access_token).to eq('123') end @@ -118,7 +118,7 @@ expect(connection.access_token).to eq('bar') expect do connection - .send(:with_refresh) { instance_double('Response', body: invalid_http_response) } + .send(:with_refresh) { instance_double(Response, body: invalid_http_response) } end.not_to change(connection, :access_token) expect(connection.access_token).to eq('bar') end @@ -136,7 +136,7 @@ it 'ensures to always use the current access token value' do expect(adapter.headers['Authorization']).to eq('Zoho-oauthtoken bar123') connection.send(:with_refresh) do - instance_double('Response', body: { data: [{ code: nil }] }) + instance_double(Response, body: { data: [{ code: nil }] }) end expect(adapter.headers['Authorization']).to eq('Zoho-oauthtoken bar456') end diff --git a/spec/zoho_hub/with_attributes_spec.rb b/spec/zoho_hub/with_attributes_spec.rb index 184da02..9754d0c 100644 --- a/spec/zoho_hub/with_attributes_spec.rb +++ b/spec/zoho_hub/with_attributes_spec.rb @@ -48,7 +48,7 @@ test.assign_attributes(one: 1, two: 2) expect(test.one).to eq(1) expect(test.two).to eq(2) - expect(test.three).to eq(nil) + expect(test.three).to be_nil end end end diff --git a/spec/zoho_hub_spec.rb b/spec/zoho_hub_spec.rb index 1f85934..ebf3a6b 100644 --- a/spec/zoho_hub_spec.rb +++ b/spec/zoho_hub_spec.rb @@ -2,7 +2,7 @@ RSpec.describe ZohoHub do it 'has a version number' do - expect(ZohoHub::VERSION).not_to be nil + expect(ZohoHub::VERSION).not_to be_nil end describe '#on_initialize_connection' do From 4081b6664d848933a2fffd3539454dc9ed0b53bb Mon Sep 17 00:00:00 2001 From: beeleethebee Date: Fri, 25 Oct 2024 08:57:01 +0200 Subject: [PATCH 7/8] :white_check_mark: stub correctly the faraday response in tests --- spec/zoho_hub/connection_spec.rb | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) diff --git a/spec/zoho_hub/connection_spec.rb b/spec/zoho_hub/connection_spec.rb index 0699c08..4e0bf65 100644 --- a/spec/zoho_hub/connection_spec.rb +++ b/spec/zoho_hub/connection_spec.rb @@ -107,7 +107,7 @@ expect(connection.access_token).to eq('foo') expect do connection - .send(:with_refresh) { instance_double(Response, body: invalid_http_response) } + .send(:with_refresh) { instance_double(Faraday::Response, body: invalid_http_response) } end.to change(connection, :access_token).to eq('123') end @@ -118,7 +118,10 @@ expect(connection.access_token).to eq('bar') expect do connection - .send(:with_refresh) { instance_double(Response, body: invalid_http_response) } + .send(:with_refresh) do + instance_double(Faraday::Response, + body: invalid_http_response) + end end.not_to change(connection, :access_token) expect(connection.access_token).to eq('bar') end @@ -136,7 +139,7 @@ it 'ensures to always use the current access token value' do expect(adapter.headers['Authorization']).to eq('Zoho-oauthtoken bar123') connection.send(:with_refresh) do - instance_double(Response, body: { data: [{ code: nil }] }) + instance_double(Faraday::Response, body: { data: [{ code: nil }] }) end expect(adapter.headers['Authorization']).to eq('Zoho-oauthtoken bar456') end From f73b2b53d09d8a113cfa6007a68913f53cc65e90 Mon Sep 17 00:00:00 2001 From: beeleethebee Date: Fri, 25 Oct 2024 09:00:14 +0200 Subject: [PATCH 8/8] :memo: update README to integrate tags handling --- README.md | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/README.md b/README.md index 2a57b93..9f8cb75 100644 --- a/README.md +++ b/README.md @@ -361,6 +361,15 @@ Adding notes: Lead.add_note(id: lead.id, title: 'Note title', content: 'Note content') ``` +Associate tags to records: + +```ruby +Lead.associate_tags([id1, id2], %w[tag1 tag2]) + +# Or +lead.associate_tags(%w[tag1 tag2]) +``` + Related records: ```ruby