Class: Hubspot::Association

Inherits:
Object
  • Object
show all
Defined in:
lib/hubspot/association.rb

Constant Summary collapse

OBJECT_TARGET_TO_CLASS =
{
  "Contact" => Hubspot::Contact,
  "Deal" => Hubspot::Deal,
  "Company" => Hubspot::Company
}.freeze
ASSOCIATION_DEFINITIONS =
{
  "Company" => {
    "Contact" => 2,
    "Deal" => 6,
    "Company" => 13
  },
  "Deal" => {
    "Company" => 5,
    "Contact" => 3
  },
  "Contact" => {
    "Deal" => 4
  }
}.freeze

Class Method Summary collapse

Class Method Details

.all(object_type, object_id, to_object_type) ⇒ Object

Retrieve all associated resources given a source (object_type and object_id) and a relation type (to_object_type) https://developers.hubspot.com/docs/api/crm/associations Warning: it will return at most 1000 objects and make up to 1001 queries Hubspot::Association.all(“Company”, 42, “Contact”)



58
59
60
61
62
63
64
# File 'lib/hubspot/association.rb', line 58

def all(object_type, object_id, to_object_type)
  klass = OBJECT_TARGET_TO_CLASS[to_object_type]
  raise(Hubspot::InvalidParams, 'Object type not supported') unless klass.present?

  response = Hubspot::Connection.get_json("/crm/v4/objects/#{object_type}/#{object_id}/associations/#{to_object_type}", {})
  response['results'].map { |result| klass.find(result["toObjectId"]) }
end

.batch_create(from_object_type, to_object_type, associations) ⇒ Object

Make multiple associations in a single API call https://developers.hubspot.com/docs/api/crm/associations usage: Hubspot::Association.batch_create(“Company”, “Contact”, [1, to_id: 2]])



32
33
34
35
36
37
38
39
# File 'lib/hubspot/association.rb', line 32

def batch_create(from_object_type, to_object_type, associations)
  definition_id = ASSOCIATION_DEFINITIONS.dig(from_object_type, to_object_type)
  request = { inputs: associations.map { |assocation| build_create_association_body(assocation, definition_id) } }
  response = Hubspot::Connection.post_json("/crm/v4/associations/#{from_object_type}/#{to_object_type}/batch/create", params: { no_parse: true }, body: request)
  return false if response.parsed_response["errors"].present?

  response.success?
end

.batch_delete(from_object_type, to_object_type, associations) ⇒ Object

Remove multiple associations in a single API call https://developers.hubspot.com/docs/api/crm/associations usage: Hubspot::Association.batch_delete(“Company”, “Contact”, [{ from_id: 1, to_id: 2}])



49
50
51
52
# File 'lib/hubspot/association.rb', line 49

def batch_delete(from_object_type, to_object_type, associations)
  request = { inputs: build_delete_associations_body(associations) }
  Hubspot::Connection.post_json("/crm/v4/associations/#{from_object_type}/#{to_object_type}/batch/archive", params: { no_parse: true }, body: request).success?
end

.create(object_type, object_id, to_object_type, to_object_id) ⇒ Object



24
25
26
# File 'lib/hubspot/association.rb', line 24

def create(object_type, object_id, to_object_type, to_object_id)
  batch_create(object_type, to_object_type, [{from_id: object_id, to_id: to_object_id}])
end

.delete(object_type, object_id, to_object_type, to_object_id) ⇒ Object



41
42
43
# File 'lib/hubspot/association.rb', line 41

def delete(object_type, object_id, to_object_type, to_object_id)
  batch_delete(object_type, to_object_type, [{from_id: object_id, to_id: to_object_id}])
end