Class: Hubspot::Engagement

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

Overview

Direct Known Subclasses

EngagementCall, EngagementNote

Constant Summary collapse

ALL_ENGAGEMENTS_PATH =
'/engagements/v1/engagements/paged'
RECENT_ENGAGEMENT_PATH =
'/engagements/v1/engagements/recent/modified'
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.



25
26
27
28
29
30
31
# File 'lib/hubspot/engagement.rb', line 25

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.



21
22
23
# File 'lib/hubspot/engagement.rb', line 21

def associations
  @associations
end

#attachmentsObject (readonly)

Returns the value of attribute attachments.



22
23
24
# File 'lib/hubspot/engagement.rb', line 22

def attachments
  @attachments
end

#engagementObject (readonly)

Returns the value of attribute engagement.



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

def engagement
  @engagement
end

#idObject (readonly)

Returns the value of attribute id.



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

def id
  @id
end

#metadataObject (readonly)

Returns the value of attribute metadata.



23
24
25
# File 'lib/hubspot/engagement.rb', line 23

def 
  @metadata
end

Class Method Details

.all(opts = {}) ⇒ Object



52
53
54
55
56
57
58
59
60
61
62
# File 'lib/hubspot/engagement.rb', line 52

def all(opts = {})
  path = ALL_ENGAGEMENTS_PATH

  response = Hubspot::Connection.get_json(path, opts)

  result = {}
  result['engagements'] = response['results'].map { |d| new(d) }
  result['offset'] = response['offset']
  result['hasMore'] = response['hasMore']
  return result
end

.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



105
106
107
108
109
110
111
112
# File 'lib/hubspot/engagement.rb', line 105

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



34
35
36
37
# File 'lib/hubspot/engagement.rb', line 34

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

.find(engagement_id) ⇒ Object



39
40
41
42
43
44
45
46
47
48
49
50
# File 'lib/hubspot/engagement.rb', line 39

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



84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
# File 'lib/hubspot/engagement.rb', line 84

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



76
77
78
# File 'lib/hubspot/engagement.rb', line 76

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

.find_by_contact(contact_id) ⇒ Object



80
81
82
# File 'lib/hubspot/engagement.rb', line 80

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

.recent(opts = {}) ⇒ Object



64
65
66
67
68
69
70
71
72
73
74
# File 'lib/hubspot/engagement.rb', line 64

def recent(opts = {})
  path = RECENT_ENGAGEMENT_PATH

  response = Hubspot::Connection.get_json(path, opts)

  result = {}
  result['engagements'] = response['results'].map { |d| new(d) }
  result['offset'] = response['offset']
  result['hasMore'] = response['hasMore']
  result
end

Instance Method Details

#[](property) ⇒ Object



127
128
129
# File 'lib/hubspot/engagement.rb', line 127

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

#destroy!TrueClass

Returns:

  • (TrueClass)

    true



118
119
120
121
# File 'lib/hubspot/engagement.rb', line 118

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

#destroyed?Boolean

Returns:

  • (Boolean)


123
124
125
# File 'lib/hubspot/engagement.rb', line 123

def destroyed?
  !!@destroyed
end

#update!(params) ⇒ Hubspot::Engagement

Parameters:

  • params (Hash)

    hash of properties to update

Returns:



135
136
137
138
139
140
141
142
143
144
145
# File 'lib/hubspot/engagement.rb', line 135

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