Class: Hubspot::Association
- Inherits:
-
Object
- Object
- Hubspot::Association
- 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 =
{ "Contact" => { "Deal" => 4 }, "Company" => { "Contact" => 2, "Company" => 13, "Deal" => 6 }, "Deal" => { "Contact" => 3, "Company" => 5 }, "Task" => { "Contact" => 204, "Company" => 192, "Deal" => 216, "Ticket" => 230 }, "Ticket" => { "Contact" => 16, "Conversation" => 32, "Company" => 339, "Deal" => 28, "Task" => 229 } }.freeze
Class Method Summary collapse
-
.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”).
-
.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]]).
-
.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}]).
-
.build_association_param(origin, associated, associated_id) ⇒ Object
Utility function to build the association list required by some API endpoints.
- .create(object_type, object_id, to_object_type, to_object_id) ⇒ Object
- .delete(object_type, object_id, to_object_type, to_object_id) ⇒ Object
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”)
71 72 73 74 75 76 77 |
# File 'lib/hubspot/association.rb', line 71 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]])
45 46 47 48 49 50 51 52 |
# File 'lib/hubspot/association.rb', line 45 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}])
62 63 64 65 |
# File 'lib/hubspot/association.rb', line 62 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 |
.build_association_param(origin, associated, associated_id) ⇒ Object
Utility function to build the association list required by some API endpoints
80 81 82 83 84 85 86 |
# File 'lib/hubspot/association.rb', line 80 def build_association_param(origin, associated, associated_id) { to: { id: associated_id }, types: [{ associationCategory: 'HUBSPOT_DEFINED', associationTypeId: Hubspot::Association::ASSOCIATION_DEFINITIONS[origin][associated] }] } end |
.create(object_type, object_id, to_object_type, to_object_id) ⇒ Object
37 38 39 |
# File 'lib/hubspot/association.rb', line 37 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
54 55 56 |
# File 'lib/hubspot/association.rb', line 54 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 |