Skip to content

Commit

Permalink
Add more associations to tasks (#26)
Browse files Browse the repository at this point in the history
* create utils to add association more quickly

* update task endpoint to add more associations

* bump gem version

* add spec to cover append_association nil case

* move append_association to Hubspot::Association

* Ticket.create! and Task.create! now take array association param

* make hashes more ruby3 compliant
  • Loading branch information
Paul-Yves authored Nov 21, 2024
1 parent 820b9ac commit 08acce2
Show file tree
Hide file tree
Showing 9 changed files with 57 additions and 49 deletions.
4 changes: 4 additions & 0 deletions History.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
## 0.18.0

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

## 0.17.1

* Add default properties for Ticket#find
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.1"
s.version = "0.18.0"
s.require_paths = ["lib"]
s.authors = ["Jonathan"]
s.email = ["jonathan@hoggo.com"]
Expand Down
9 changes: 9 additions & 0 deletions lib/hubspot/association.rb
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,15 @@ def all(object_type, object_id, to_object_type)
response['results'].map { |result| klass.find(result["toObjectId"]) }
end

# Utility function to build the association list required by some API endpoints
def build_association_param(origin, associated, associated_id)
{
to: { id: associated_id },
types: [{ associationCategory: 'HUBSPOT_DEFINED',
associationTypeId: Hubspot::Association::ASSOCIATION_DEFINITIONS[origin][associated] }]
}
end

private

def build_create_association_body(association, definition_id)
Expand Down
13 changes: 3 additions & 10 deletions lib/hubspot/task.rb
Original file line number Diff line number Diff line change
Expand Up @@ -20,17 +20,10 @@ def initialize(response_hash)
end

class << self
def create!(params = {}, ticket_id: nil)
associations_hash = { 'associations' => [] }
if ticket_id.present?
associations_hash['associations'] << {
"to": { "id": ticket_id },
"types": [{ "associationCategory": 'HUBSPOT_DEFINED',
"associationTypeId": Hubspot::Association::ASSOCIATION_DEFINITIONS['Task']['Ticket'] }]
}
end
def create!(params = {}, associations: [])
associations_hash = { associations: }
properties = { hs_task_status: 'NOT_STARTED', hs_task_type: 'TODO' }.merge(params)
post_data = associations_hash.merge({ properties: properties })
post_data = associations_hash.merge(properties:)

response = Hubspot::Connection.post_json(TASKS_PATH, params: {}, body: post_data)
new(response)
Expand Down
26 changes: 2 additions & 24 deletions lib/hubspot/ticket.rb
Original file line number Diff line number Diff line change
Expand Up @@ -20,31 +20,9 @@ def initialize(response_hash)
end

class << self
def create!(params = {}, contact_id: nil, company_id: nil, deal_id: nil)
associations_hash = { 'associations' => [] }
if contact_id.present?
associations_hash['associations'] << {
"to": { "id": contact_id },
"types": [{ "associationCategory": 'HUBSPOT_DEFINED',
"associationTypeId": Hubspot::Association::ASSOCIATION_DEFINITIONS['Ticket']['Contact'] }]
}
end
if company_id.present?
associations_hash['associations'] << {
"to": { "id": company_id },
"types": [{ "associationCategory": 'HUBSPOT_DEFINED',
"associationTypeId": Hubspot::Association::ASSOCIATION_DEFINITIONS['Ticket']['Company'] }]
}
end
if deal_id.present?
associations_hash['associations'] << {
"to": { "id": deal_id },
"types": [{ "associationCategory": 'HUBSPOT_DEFINED',
"associationTypeId": Hubspot::Association::ASSOCIATION_DEFINITIONS['Ticket']['Deal'] }]
}
end
def create!(params = {}, associations: [])
associations_hash = { associations: }
post_data = associations_hash.merge({ properties: params })

response = Hubspot::Connection.post_json(TICKETS_PATH, params: {}, body: post_data)
new(response)
end
Expand Down
24 changes: 12 additions & 12 deletions spec/fixtures/vcr_cassettes/task.yml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

13 changes: 13 additions & 0 deletions spec/lib/hubspot/association_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -162,4 +162,17 @@
end
end
end

describe '.build_association_param' do
it 'returns proper association hash' do
result = described_class.build_association_param('Ticket', 'Company', 1337)
expect(result).to eq(
{
"to": { "id": 1337 },
"types": [{ "associationCategory": 'HUBSPOT_DEFINED',
"associationTypeId": 339 }]
}
)
end
end
end
8 changes: 7 additions & 1 deletion spec/lib/hubspot/task_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,13 @@
hs_task_subject: 'title of task',
hs_timestamp: DateTime.now.strftime('%Q')
}
described_class.create!(params, ticket_id: 16_174_569_112)
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 08acce2

Please sign in to comment.