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

Use crm associations endpoints #210

Open
wants to merge 10 commits into
base: master
Choose a base branch
from
Open
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 lib/hubspot-ruby.rb
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
require 'hubspot/deal'
require 'hubspot/deal_pipeline'
require 'hubspot/deal_properties'
require 'hubspot/association'
require 'hubspot/deprecator'
require 'hubspot/owner'
require 'hubspot/engagement'
Expand Down
80 changes: 80 additions & 0 deletions lib/hubspot/association.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
class Hubspot::Association
COMPANY_TO_CONTACT = 2
DEAL_TO_CONTACT = 3
CONTACT_TO_DEAL = 4
DEAL_TO_COMPANY = 5
COMPANY_TO_DEAL = 6
Copy link
Contributor Author

Choose a reason for hiding this comment

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

I added the bare minimum, the long list is long and is here.
I guess we can make this list grow with time?

DEFINITION_TARGET_TO_CLASS = {
2 => Hubspot::Contact,
3 => Hubspot::Contact,
4 => Hubspot::Deal,
5 => Hubspot::Company,
6 => Hubspot::Deal
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Can't use key: value for integers

}.freeze

BATCH_CREATE_PATH = '/crm-associations/v1/associations/create-batch'
BATCH_DELETE_PATH = '/crm-associations/v1/associations/delete-batch'
ASSOCIATIONS_PATH = '/crm-associations/v1/associations/:resource_id/HUBSPOT_DEFINED/:definition_id'

class << self
def create(from_id, to_id, definition_id)
batch_create([{ from_id: from_id, to_id: to_id, definition_id: definition_id }])
end

# Make multiple associations in a single API call
# {https://developers.hubspot.com/docs/methods/crm-associations/batch-associate-objects}
# usage:
# Hubspot::Association.batch_create([{ from_id: 1, to_id: 2, definition_id: Hubspot::Association::COMPANY_TO_CONTACT }])
def batch_create(associations)
request = associations.map { |assocation| build_association_body(assocation) }
Hubspot::Connection.put_json(BATCH_CREATE_PATH, params: { no_parse: true }, body: request).success?
end

def delete(from_id, to_id, definition_id)
batch_delete([{from_id: from_id, to_id: to_id, definition_id: definition_id}])
end

# Remove multiple associations in a single API call
# {https://developers.hubspot.com/docs/methods/crm-associations/batch-delete-associations}
# usage:
# Hubspot::Association.batch_delete([{ from_id: 1, to_id: 2, definition_id: Hubspot::Association::COMPANY_TO_CONTACT }])
def batch_delete(associations)
request = associations.map { |assocation| build_association_body(assocation) }
Hubspot::Connection.put_json(BATCH_DELETE_PATH, params: { no_parse: true }, body: request).success?
end

# Retrieve all associated resources given a source (resource_id) and a kind (definition_id)
# Example: if resource_id is a deal, using DEAL_TO_CONTACT will find every contact associated with the deal
# {https://developers.hubspot.com/docs/methods/crm-associations/get-associations}
# Warning: it will make N+M queries, where
# N is the number of PagedCollection requests necessary to get all ids,
# and M is the number of results, each resulting in a find
# usage:
# Hubspot::Association.all(42, Hubspot::Association::DEAL_TO_CONTACT)
def all(resource_id, definition_id)
opts = { resource_id: resource_id, definition_id: definition_id }
klass = DEFINITION_TARGET_TO_CLASS[definition_id]
raise(Hubspot::InvalidParams, 'Definition not supported') unless klass.present?

collection = Hubspot::PagedCollection.new(opts) do |options, offset, limit|
params = options.merge(offset: offset, limit: limit)
response = Hubspot::Connection.get_json(ASSOCIATIONS_PATH, params)

resources = response['results'].map { |result| klass.find(result) }
[resources, response['offset'], response['has-more']]
end
collection.resources
end

private

def build_association_body(assocation)
{
fromObjectId: assocation[:from_id],
toObjectId: assocation[:to_id],
category: 'HUBSPOT_DEFINED',
definitionId: assocation[:definition_id]
}
end
end
end
15 changes: 2 additions & 13 deletions lib/hubspot/company.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ class Hubspot::Company < Hubspot::Resource
self.id_field = "companyId"
self.property_name_field = "name"

ADD_CONTACT_PATH = '/companies/v2/companies/:id/contacts/:contact_id'
ALL_PATH = '/companies/v2/companies/paged'
BATCH_UPDATE_PATH = '/companies/v1/batch-async/update'
CONTACTS_PATH = '/companies/v2/companies/:id/contacts'
Expand All @@ -12,7 +11,6 @@ class Hubspot::Company < Hubspot::Resource
FIND_PATH = '/companies/v2/companies/:id'
RECENTLY_CREATED_PATH = '/companies/v2/companies/recent/created'
RECENTLY_MODIFIED_PATH = '/companies/v2/companies/recent/modified'
REMOVE_CONTACT_PATH = '/companies/v2/companies/:id/contacts/:contact_id'
SEARCH_DOMAIN_PATH = '/companies/v2/domains/:domain/companies'
UPDATE_PATH = '/companies/v2/companies/:id'

Expand Down Expand Up @@ -80,20 +78,11 @@ def recently_modified(opts = {})
end

def add_contact(id, contact_id)
Hubspot::Connection.put_json(
ADD_CONTACT_PATH,
params: { id: id, contact_id: contact_id }
)
true
Hubspot::Association.create(id, contact_id, Hubspot::Association::COMPANY_TO_CONTACT)
end

def remove_contact(id, contact_id)
Hubspot::Connection.delete_json(
REMOVE_CONTACT_PATH,
{ id: id, contact_id: contact_id }
)

true
Hubspot::Association.delete(id, contact_id, Hubspot::Association::COMPANY_TO_CONTACT)
end

def batch_update(companies, opts = {})
Expand Down
42 changes: 19 additions & 23 deletions lib/hubspot/deal.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,6 @@ class Deal
DEAL_PATH = "/deals/v1/deal/:deal_id"
RECENT_UPDATED_PATH = "/deals/v1/deal/recent/modified"
UPDATE_DEAL_PATH = '/deals/v1/deal/:deal_id'
ASSOCIATE_DEAL_PATH = '/deals/v1/deal/:deal_id/associations/:OBJECTTYPE?id=:objectId'
ASSOCIATED_DEAL_PATH = "/deals/v1/deal/associated/:objectType/:objectId"

attr_reader :properties
attr_reader :portal_id
Expand All @@ -39,16 +37,19 @@ def create!(portal_id, company_ids, vids, params={})
new(response)
end

# Associate a deal with a contact or company
# {http://developers.hubspot.com/docs/methods/deals/associate_deal}
# Usage
# Hubspot::Deal.associate!(45146940, [], [52])
def associate!(deal_id, company_ids=[], vids=[])
objecttype = company_ids.any? ? 'COMPANY' : 'CONTACT'
object_ids = (company_ids.any? ? company_ids : vids).join('&id=')
Hubspot::Connection.put_json(ASSOCIATE_DEAL_PATH, params: { deal_id: deal_id, OBJECTTYPE: objecttype, objectId: object_ids}, body: {})
end

# Associate a deal with a contact or company
# {http://developers.hubspot.com/docs/methods/deals/associate_deal}
# Usage
# Hubspot::Deal.associate!(45146940, [32], [52])
def associate!(deal_id, company_ids=[], vids=[])
associations = company_ids.map do |id|
{ from_id: deal_id, to_id: id, definition_id: Hubspot::Association::DEAL_TO_COMPANY }
end
associations += vids.map do |id|
{ from_id: deal_id, to_id: id, definition_id: Hubspot::Association::DEAL_TO_CONTACT }
end
Hubspot::Association.batch_create(associations)
end

def find(deal_id)
response = Hubspot::Connection.get_json(DEAL_PATH, { deal_id: deal_id })
Expand Down Expand Up @@ -94,20 +95,15 @@ def find_by_contact(contact)
end

# Find all deals associated to a contact or company
# {http://developers.hubspot.com/docs/methods/deals/get-associated-deals}
# @param object [Hubspot::Contact || Hubspot::Company] a contact or company
# @return [Array] Array of Hubspot::Deal records
def find_by_association(object)
path = ASSOCIATED_DEAL_PATH
objectType = case object
when Hubspot::Company then :company
when Hubspot::Contact then :contact
else raise(Hubspot::InvalidParams, "Instance type not supported")
end

params = { objectType: objectType, objectId: object.id }
response = Hubspot::Connection.get_json(path, params)
response["results"].map { |deal_id| find(deal_id) }
definition = case object
when Hubspot::Company then Hubspot::Association::COMPANY_TO_DEAL
when Hubspot::Contact then Hubspot::Association::CONTACT_TO_DEAL
else raise(Hubspot::InvalidParams, 'Instance type not supported')
end
Hubspot::Association.all(object.id, definition)
end
end

Expand Down
Loading