Skip to content

Commit

Permalink
Add Association.all
Browse files Browse the repository at this point in the history
  • Loading branch information
fonji committed Nov 21, 2019
1 parent b2c47e7 commit 84540cb
Show file tree
Hide file tree
Showing 4 changed files with 424 additions and 1 deletion.
2 changes: 1 addition & 1 deletion lib/hubspot-ruby.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
require 'hubspot/collection'
require 'hubspot/paged_collection'
require 'hubspot/properties'
require 'hubspot/association'
require 'hubspot/company'
require 'hubspot/company_properties'
require 'hubspot/config'
Expand All @@ -20,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
33 changes: 33 additions & 0 deletions lib/hubspot/association.rb
Original file line number Diff line number Diff line change
@@ -1,10 +1,20 @@
class Hubspot::Association
COMPANY_TO_CONTACT = 2
DEAL_TO_CONTACT = 3
CONTACT_TO_DEAL = 4
DEAL_TO_COMPANY = 5
COMPANY_TO_DEAL = 6
DEFINITION_TARGET_TO_CLASS = {
2 => Hubspot::Contact,
3 => Hubspot::Contact,
4 => Hubspot::Deal,
5 => Hubspot::Company,
6 => Hubspot::Deal
}.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)
Expand Down Expand Up @@ -33,6 +43,29 @@ def batch_delete(associations)
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)
Expand Down
Loading

0 comments on commit 84540cb

Please sign in to comment.