Class: Hubspot::Company

Inherits:
Resource show all
Defined in:
lib/hubspot/company.rb

Constant Summary collapse

ALL_PATH =
'/companies/v2/companies/paged'
BATCH_UPDATE_PATH =
'/companies/v1/batch-async/update'
CONTACTS_PATH =
'/companies/v2/companies/:id/contacts'
CONTACT_IDS_PATH =
'/companies/v2/companies/:id/vids'
CREATE_PATH =
'/companies/v2/companies/'
DELETE_PATH =
'/companies/v2/companies/:id'
FIND_PATH =
'/companies/v2/companies/:id'
RECENTLY_CREATED_PATH =
'/companies/v2/companies/recent/created'
RECENTLY_MODIFIED_PATH =
'/companies/v2/companies/recent/modified'
SEARCH_DOMAIN_PATH =
'/companies/v2/domains/:domain/companies'
UPDATE_PATH =
'/companies/v2/companies/:id'

Instance Attribute Summary

Attributes inherited from Resource

#changes, #id, #metadata, #properties

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Resource

#[], #changed?, create, #delete, #deleted?, find, from_result, #initialize, #persisted?, #reload, #save, #to_i, update, #update, update!

Constructor Details

This class inherits a constructor from Hubspot::Resource

Dynamic Method Handling

This class handles dynamic methods through the method_missing method in the class Hubspot::Resource

Class Method Details

.add_contact(id, contact_id) ⇒ Object



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

def add_contact(id, contact_id)
  Hubspot::Association.create("Company", id, "Contact", contact_id)
end

.all(opts = {}) ⇒ Object



18
19
20
21
22
23
24
25
26
27
28
29
# File 'lib/hubspot/company.rb', line 18

def all(opts = {})
  Hubspot::PagedCollection.new(opts) do |options, offset, limit|
    response = Hubspot::Connection.get_json(
      ALL_PATH,
      options.merge(offset: offset, limit: limit)
    )

    companies = response["companies"].map { |result| from_result(result) }

    [companies, response["offset"], response["has-more"]]
  end
end

.batch_update(companies, opts = {}) ⇒ Object



88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
# File 'lib/hubspot/company.rb', line 88

def batch_update(companies, opts = {})
  request = companies.map do |company|
    # Use the specified options or update with the changes
    changes = opts.empty? ? company.changes : opts

    unless changes.empty?
      {
        "objectId" => company.id,
        "properties" => changes.map { |k, v| { "name" => k, "value" => v } }
      }
    end
  end

  # Remove any objects without changes and return if there is nothing to update
  request.compact!
  return true if request.empty?

  Hubspot::Connection.post_json(
    BATCH_UPDATE_PATH,
    params: {},
    body: request
  )

  true
end

.recently_created(opts = {}) ⇒ Object



54
55
56
57
58
59
60
61
62
63
64
65
# File 'lib/hubspot/company.rb', line 54

def recently_created(opts = {})
  Hubspot::PagedCollection.new(opts) do |options, offset, limit|
    response = Hubspot::Connection.get_json(
      RECENTLY_CREATED_PATH,
      {offset: offset, count: limit}
    )

    companies = response["results"].map { |result| from_result(result) }

    [companies, response["offset"], response["hasMore"]]
  end
end

.recently_modified(opts = {}) ⇒ Object



67
68
69
70
71
72
73
74
75
76
77
78
# File 'lib/hubspot/company.rb', line 67

def recently_modified(opts = {})
  Hubspot::PagedCollection.new(opts) do |options, offset, limit|
    response = Hubspot::Connection.get_json(
      RECENTLY_MODIFIED_PATH,
      {offset: offset, count: limit}
    )

    companies = response["results"].map { |result| from_result(result) }

    [companies, response["offset"], response["hasMore"]]
  end
end

.remove_contact(id, contact_id) ⇒ Object



84
85
86
# File 'lib/hubspot/company.rb', line 84

def remove_contact(id, contact_id)
  Hubspot::Association.delete("Company", id, "Contact", contact_id)
end

.search_domain(domain, opts = {}) ⇒ Object



31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
# File 'lib/hubspot/company.rb', line 31

def search_domain(domain, opts = {})
  Hubspot::PagedCollection.new(opts) do |options, offset, limit|
    request = {
      "limit" => limit,
      "requestOptions" => options,
      "offset" => {
        "isPrimary" => true,
        "companyId" => offset
      }
    }

    response = Hubspot::Connection.post_json(
      SEARCH_DOMAIN_PATH,
      params: { domain: domain },
      body: request
    )

    companies = response["results"].map { |result| from_result(result) }

    [companies, response["offset"]["companyId"], response["hasMore"]]
  end
end

Instance Method Details

#add_contact(contact) ⇒ Object



142
143
144
# File 'lib/hubspot/company.rb', line 142

def add_contact(contact)
  self.class.add_contact(@id, contact.to_i)
end

#contact_ids(opts = {}) ⇒ Object



131
132
133
134
135
136
137
138
139
140
# File 'lib/hubspot/company.rb', line 131

def contact_ids(opts = {})
  Hubspot::PagedCollection.new(opts) do |options, offset, limit|
    response = Hubspot::Connection.get_json(
      CONTACT_IDS_PATH,
      {"id" => @id, "vidOffset" => offset, "count" => limit}
    )

    [response["vids"], response["vidOffset"], response["hasMore"]]
  end
end

#contacts(opts = {}) ⇒ Object



115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
# File 'lib/hubspot/company.rb', line 115

def contacts(opts = {})
  Hubspot::PagedCollection.new(opts) do |options, offset, limit|
    response = Hubspot::Connection.get_json(
      CONTACTS_PATH,
      {"id" => @id, "vidOffset" => offset, "count" => limit}
    )

    contacts = response["contacts"].map do |result|
      result["properties"] = Hubspot::Utils.properties_array_to_hash(result["properties"])
      Hubspot::Contact.from_result(result)
    end

    [contacts, response["vidOffset"], response["hasMore"]]
  end
end

#remove_contact(contact) ⇒ Object



146
147
148
# File 'lib/hubspot/company.rb', line 146

def remove_contact(contact)
  self.class.remove_contact(@id, contact.to_i)
end