Class: Hubspot::Engagement

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

Overview

Direct Known Subclasses

EngagementCall, EngagementNote

Constant Summary collapse

CREATE_ENGAGMEMENT_PATH =
'/engagements/v1/engagements'
ENGAGEMENT_PATH =
'/engagements/v1/engagements/:engagement_id'
ASSOCIATE_ENGAGEMENT_PATH =
'/engagements/v1/engagements/:engagement_id/associations/:object_type/:object_vid'
GET_ASSOCIATED_ENGAGEMENTS =
'/engagements/v1/engagements/associated/:objectType/:objectId/paged'

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(response_hash) ⇒ Engagement

Returns a new instance of Engagement.



21
22
23
24
25
26
27
28
# File 'lib/hubspot/engagement.rb', line 21

def initialize(response_hash)

  @engagement = response_hash["engagement"]
  @associations = response_hash["associations"]
  @attachments = response_hash["attachments"]
  @metadata = response_hash["metadata"]
  @id = engagement["id"]
end

Instance Attribute Details

#associationsObject (readonly)

Returns the value of attribute associations.



17
18
19
# File 'lib/hubspot/engagement.rb', line 17

def associations
  @associations
end

#attachmentsObject (readonly)

Returns the value of attribute attachments.



18
19
20
# File 'lib/hubspot/engagement.rb', line 18

def attachments
  @attachments
end

#engagementObject (readonly)

Returns the value of attribute engagement.



16
17
18
# File 'lib/hubspot/engagement.rb', line 16

def engagement
  @engagement
end

#idObject (readonly)

Returns the value of attribute id.



15
16
17
# File 'lib/hubspot/engagement.rb', line 15

def id
  @id
end

#metadataObject (readonly)

Returns the value of attribute metadata.



19
20
21
# File 'lib/hubspot/engagement.rb', line 19

def 
  @metadata
end

Class Method Details

.associate!(engagement_id, object_type, object_vid) ⇒ Object

Parameters:

  • engagement_id (int)

    id of the engagement to associate

  • object_type (string)

    one of contact, company, or deal

  • object_vid (int)

    id of the contact, company, or deal to associate



78
79
80
81
82
83
84
85
# File 'lib/hubspot/engagement.rb', line 78

def associate!(engagement_id, object_type, object_vid)
  Hubspot::Connection.put_json(ASSOCIATE_ENGAGEMENT_PATH,
                               params: {
                                 engagement_id: engagement_id,
                                 object_type: object_type,
                                 object_vid: object_vid
                               })
end

.create!(params = {}) ⇒ Object



31
32
33
34
# File 'lib/hubspot/engagement.rb', line 31

def create!(params={})
  response = Hubspot::Connection.post_json(CREATE_ENGAGMEMENT_PATH, params: {}, body: params )
  new(HashWithIndifferentAccess.new(response))
end

.find(engagement_id) ⇒ Object



36
37
38
39
40
41
42
43
44
45
46
47
# File 'lib/hubspot/engagement.rb', line 36

def find(engagement_id)
  begin
    response = Hubspot::Connection.get_json(ENGAGEMENT_PATH, { engagement_id: engagement_id })
    response ? new(HashWithIndifferentAccess.new(response)) : nil
  rescue Hubspot::RequestError => ex
    if ex.response.code == 404
      return nil
    else
      raise ex
    end
  end
end

.find_by_association(association_id, association_type) ⇒ Object



57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
# File 'lib/hubspot/engagement.rb', line 57

def find_by_association(association_id, association_type)
  path = GET_ASSOCIATED_ENGAGEMENTS
  params = { objectType: association_type, objectId: association_id }
  raise Hubspot::InvalidParams, 'expecting Integer parameter' unless association_id.try(:is_a?, Integer)
  raise Hubspot::InvalidParams, 'expecting String parameter' unless association_type.try(:is_a?, String)

  engagements = []
  begin
    response = Hubspot::Connection.get_json(path, params)
    engagements = response["results"].try(:map) { |engagement| new(engagement) }
  rescue => e
    raise e unless e.message =~ /not found/
  end
  engagements
end

.find_by_company(company_id) ⇒ Object



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

def find_by_company(company_id)
  find_by_association company_id, 'COMPANY'
end

.find_by_contact(contact_id) ⇒ Object



53
54
55
# File 'lib/hubspot/engagement.rb', line 53

def find_by_contact(contact_id)
  find_by_association contact_id, 'CONTACT'
end

Instance Method Details

#[](property) ⇒ Object



100
101
102
# File 'lib/hubspot/engagement.rb', line 100

def [](property)
  @properties[property]
end

#destroy!TrueClass

Returns:

  • (TrueClass)

    true



91
92
93
94
# File 'lib/hubspot/engagement.rb', line 91

def destroy!
  Hubspot::Connection.delete_json(ENGAGEMENT_PATH, {engagement_id: id})
  @destroyed = true
end

#destroyed?Boolean

Returns:

  • (Boolean)


96
97
98
# File 'lib/hubspot/engagement.rb', line 96

def destroyed?
  !!@destroyed
end

#update!(params) ⇒ Hubspot::Engagement

Parameters:

  • params (Hash)

    hash of properties to update

Returns:



108
109
110
111
112
113
114
115
116
117
118
# File 'lib/hubspot/engagement.rb', line 108

def update!(params)
  data = {
    engagement: params[:engagement]     || engagement,
    associations: params[:associations] || associations,
    attachments: params[:attachments]   || attachments,
    metadata: params[:metadata]         || 
  }

  Hubspot::Connection.put_json(ENGAGEMENT_PATH, params: { engagement_id: id }, body: data)
  self
end