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

✨ Associate tag to record #19

Merged
merged 8 commits into from
Nov 5, 2024
Merged
Show file tree
Hide file tree
Changes from all 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
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -12,3 +12,4 @@ Gemfile.lock

# rspec failure tracking
.rspec_status
.idea/
9 changes: 9 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
17 changes: 17 additions & 0 deletions lib/zoho_hub/base_record.rb
Original file line number Diff line number Diff line change
Expand Up @@ -165,6 +165,16 @@ def exists?(id)
false
end

def associate_tags(resource_ids, tag_names = [])
responses = []
resource_ids.each_slice(100) do |ids|
parameters = "add_tags?tag_names=#{tag_names.join(',')}&ids=#{ids.join(',')}"
url = File.join(request_path, 'actions', parameters)
responses << build_response(post(url)).data.first
end
responses
end

alias exist? exists?

# rubocop:disable Metrics/AbcSize
Expand Down Expand Up @@ -223,6 +233,13 @@ def update(params)
build_response(body)
end

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
body = delete(File.join(self.class.request_path, id))

Expand Down
49 changes: 48 additions & 1 deletion spec/zoho_hub/base_record_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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 }] } }
Expand All @@ -50,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
Expand Down Expand Up @@ -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' }] } }
Expand Down
9 changes: 6 additions & 3 deletions spec/zoho_hub/connection_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand All @@ -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
Expand All @@ -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
Expand Down
2 changes: 1 addition & 1 deletion spec/zoho_hub/with_attributes_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
2 changes: 1 addition & 1 deletion spec/zoho_hub_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Loading