Skip to content

Commit

Permalink
Ticket.create! and Task.create! now take array association param
Browse files Browse the repository at this point in the history
  • Loading branch information
Paul-Yves committed Nov 21, 2024
1 parent bde22b7 commit 1cc210f
Show file tree
Hide file tree
Showing 8 changed files with 29 additions and 39 deletions.
4 changes: 2 additions & 2 deletions History.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
## 0.17.2
## 0.18.0

* Add more params for task association in Task#create
* BREAKING CHANGE : Ticket#create! and Task#create now takes association param (array), build it using Association.build_association_param

## 0.17.1

Expand Down
2 changes: 1 addition & 1 deletion hubspot-api-ruby.gemspec
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
Gem::Specification.new do |s|
s.name = "hubspot-api-ruby"
s.version = "0.17.2"
s.version = "0.18.0"
s.require_paths = ["lib"]
s.authors = ["Jonathan"]
s.email = ["[email protected]"]
Expand Down
6 changes: 2 additions & 4 deletions lib/hubspot/association.rb
Original file line number Diff line number Diff line change
Expand Up @@ -76,10 +76,8 @@ def all(object_type, object_id, to_object_type)
end

# Utility function to build the association list required by some API endpoints
def append_association(association_list, origin, associated, associated_id)
return unless associated_id.present?

association_list << {
def build_association_param(origin, associated, associated_id)
{
"to": { "id": associated_id },
"types": [{ "associationCategory": 'HUBSPOT_DEFINED',
"associationTypeId": Hubspot::Association::ASSOCIATION_DEFINITIONS[origin][associated] }]
Expand Down
8 changes: 2 additions & 6 deletions lib/hubspot/task.rb
Original file line number Diff line number Diff line change
Expand Up @@ -20,12 +20,8 @@ def initialize(response_hash)
end

class << self
def create!(params = {}, ticket_id: nil, contact_id: nil, company_id: nil, deal_id: nil)
associations_hash = { 'associations' => [] }
Hubspot::Association.append_association(associations_hash['associations'], 'Task', 'Ticket', ticket_id)
Hubspot::Association.append_association(associations_hash['associations'], 'Task', 'Contact', contact_id)
Hubspot::Association.append_association(associations_hash['associations'], 'Task', 'Company', company_id)
Hubspot::Association.append_association(associations_hash['associations'], 'Task', 'Deal', deal_id)
def create!(params = {}, associations: [])
associations_hash = { 'associations' => associations }
properties = { hs_task_status: 'NOT_STARTED', hs_task_type: 'TODO' }.merge(params)
post_data = associations_hash.merge({ properties: properties })

Expand Down
7 changes: 2 additions & 5 deletions lib/hubspot/ticket.rb
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,8 @@ def initialize(response_hash)
end

class << self
def create!(params = {}, contact_id: nil, company_id: nil, deal_id: nil)
associations_hash = { 'associations' => [] }
Hubspot::Association.append_association(associations_hash['associations'], 'Ticket', 'Contact', contact_id)
Hubspot::Association.append_association(associations_hash['associations'], 'Ticket', 'Company', company_id)
Hubspot::Association.append_association(associations_hash['associations'], 'Ticket', 'Deal', deal_id)
def create!(params = {}, associations: [])
associations_hash = { 'associations' => associations }
post_data = associations_hash.merge({ properties: params })
response = Hubspot::Connection.post_json(TICKETS_PATH, params: {}, body: post_data)
new(response)
Expand Down
25 changes: 7 additions & 18 deletions spec/lib/hubspot/association_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -163,27 +163,16 @@
end
end

describe '.append_association' do
it 'adds proper association hash to target list' do
associations_hash = { 'associations' => [] }
described_class.append_association(associations_hash['associations'], 'Ticket', 'Company', 1337)
expect(associations_hash).to eq(
describe '.build_association_param' do
it 'returns proper association hash' do
result = described_class.build_association_param('Ticket', 'Company', 1337)
expect(result).to eq(
{
'associations' => [
"to": { "id": 1337 },
"types": [{ "associationCategory": 'HUBSPOT_DEFINED',
"associationTypeId": 339 }]
]
"to": { "id": 1337 },
"types": [{ "associationCategory": 'HUBSPOT_DEFINED',
"associationTypeId": 339 }]
}
)
end

context 'when no associated_id' do
it 'does not change the list' do
associations_hash = { 'associations' => [] }
described_class.append_association(associations_hash['associations'], 'Ticket', 'Company', nil)
expect(associations_hash).to eq({ 'associations' => [] })
end
end
end
end
9 changes: 7 additions & 2 deletions spec/lib/hubspot/task_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,13 @@
hs_task_subject: 'title of task',
hs_timestamp: DateTime.now.strftime('%Q')
}
described_class.create!(params, ticket_id: 16_174_569_112, contact_id: 75_761_595_194, company_id: 25_571_271_600,
deal_id: 28_806_796_888)
associations = [
Hubspot::Association.build_association_param('Task', 'Ticket', 16_174_569_112),
Hubspot::Association.build_association_param('Task', 'Contact', 75_761_595_194),
Hubspot::Association.build_association_param('Task', 'Company', 25_571_271_600),
Hubspot::Association.build_association_param('Task', 'Deal', 28_806_796_888),
]
described_class.create!(params, associations:)
end

it 'creates a new task with valid properties' do
Expand Down
7 changes: 6 additions & 1 deletion spec/lib/hubspot/ticket_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,12 @@
hs_ticket_priority: 'MEDIUM',
subject: 'test ticket'
}
described_class.create!(params, contact_id: 75_761_595_194, company_id: 25_571_271_600, deal_id: 28_806_796_888)
associations = [
Hubspot::Association.build_association_param('Ticket', 'Contact', 75_761_595_194),
Hubspot::Association.build_association_param('Ticket', 'Company', 25_571_271_600),
Hubspot::Association.build_association_param('Ticket', 'Deal', 28_806_796_888),
]
described_class.create!(params, associations:)
end

it 'creates a new ticket with valid properties' do
Expand Down

0 comments on commit 1cc210f

Please sign in to comment.