Class: HubspotClient::Model::Contact

Inherits:
OpenStruct
  • Object
show all
Defined in:
lib/hubspot_client/model/contact.rb

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.create(properties) ⇒ Object



20
21
22
23
24
# File 'lib/hubspot_client/model/contact.rb', line 20

def self.create(properties)
  response = Client::Contact.new.create(properties)

  new(response['properties'])
end

.find(hubspot_id: nil, email: nil) ⇒ Object



8
9
10
11
12
13
14
15
16
17
18
# File 'lib/hubspot_client/model/contact.rb', line 8

def self.find(hubspot_id: nil, email: nil)
  response = if hubspot_id
               Client::Contact.new.find_by_id(hubspot_id)
             elsif email
               Client::Contact.new.find_by_email(email)
             else
               raise MissingParameter, 'email or hubspot_id needs to be set'
             end

  new(response['properties'])
end

Instance Method Details

#assign_attributes(attributes) ⇒ Object



68
69
70
71
72
# File 'lib/hubspot_client/model/contact.rb', line 68

def assign_attributes(attributes)
  attributes.each do |attribute, value|
    self[attribute] = value
  end
end

#associate_primary_company(hubspot_id) ⇒ Object



60
61
62
63
64
65
66
# File 'lib/hubspot_client/model/contact.rb', line 60

def associate_primary_company(hubspot_id)
  response = Client::Contact.new.associate_with(hs_object_id, 'companies', hubspot_id)

  return true if response.code == 200

  false
end

#create_communication_subscription(subscription_id:, legal_basis: nil, legal_basis_explanation: nil) ⇒ Object



44
45
46
47
48
49
50
51
52
53
54
# File 'lib/hubspot_client/model/contact.rb', line 44

def create_communication_subscription(subscription_id:, legal_basis: nil, legal_basis_explanation: nil)
  properties = {
    emailAddress: email,
    subscriptionId: subscription_id
  }

  properties[:legalBasis] = legal_basis if legal_basis
  properties[:legalBasisExplanation] = legal_basis_explanation if legal_basis_explanation

  Client::CommunicationPreference.new.subscribe(properties)
end

#primary_companyObject



56
57
58
# File 'lib/hubspot_client/model/contact.rb', line 56

def primary_company
  @primary_company ||= Company.find(hubspot_id: associatedcompanyid)
end

#reloadObject



35
36
37
38
39
40
41
42
# File 'lib/hubspot_client/model/contact.rb', line 35

def reload
  response = Client::Contact.new.find_by_id(hs_object_id)
  response['properties'].each do |key, value|
    self[key] = value if value != self[key]
  end

  self
end

#update(new_properties = {}) ⇒ Object



26
27
28
29
30
31
32
33
# File 'lib/hubspot_client/model/contact.rb', line 26

def update(new_properties = {})
  assign_attributes(new_properties)
  response = Client::Contact.new.update(hs_object_id, to_h.slice(*writable_properties))

  return true if response.code == 200

  false
end