class Hubspot::Company

Constants

ALL_PATH
BATCH_UPDATE_PATH
CONTACTS_PATH
CONTACT_IDS_PATH
CREATE_PATH
DELETE_PATH
FIND_PATH
RECENTLY_CREATED_PATH
RECENTLY_MODIFIED_PATH
SEARCH_DOMAIN_PATH
UPDATE_PATH

Public Class Methods

add_contact(id, contact_id) click to toggle source
# File lib/hubspot/company.rb, line 80
def add_contact(id, contact_id)
  Hubspot::Association.create(id, contact_id, Hubspot::Association::COMPANY_TO_CONTACT)
end
all(opts = {}) click to toggle source
# 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 = {}) click to toggle source
# 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 = {}) click to toggle source
# 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 = {}) click to toggle source
# 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) click to toggle source
# File lib/hubspot/company.rb, line 84
def remove_contact(id, contact_id)
  Hubspot::Association.delete(id, contact_id, Hubspot::Association::COMPANY_TO_CONTACT)
end
search_domain(domain, opts = {}) click to toggle source
# 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

Public Instance Methods

add_contact(contact) click to toggle source
# File lib/hubspot/company.rb, line 142
def add_contact(contact)
  self.class.add_contact(@id, contact.to_i)
end
contact_ids(opts = {}) click to toggle source
# 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 = {}) click to toggle source
# 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) click to toggle source
# File lib/hubspot/company.rb, line 146
def remove_contact(contact)
  self.class.remove_contact(@id, contact.to_i)
end