Class: Hubspot::Company

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

Overview

Constant Summary collapse

CREATE_COMPANY_PATH =
"/companies/v2/companies/"
RECENTLY_CREATED_COMPANIES_PATH =
"/companies/v2/companies/recent/created"
RECENTLY_MODIFIED_COMPANIES_PATH =
"/companies/v2/companies/recent/modified"
GET_COMPANY_BY_ID_PATH =
"/companies/v2/companies/:company_id"
GET_COMPANY_BY_DOMAIN_PATH =
"/companies/v2/domains/:domain/companies"
UPDATE_COMPANY_PATH =
"/companies/v2/companies/:company_id"
GET_COMPANY_CONTACT_VIDS_PATH =
"/companies/v2/companies/:company_id/vids"
ADD_CONTACT_TO_COMPANY_PATH =
"/companies/v2/companies/:company_id/contacts/:vid"
DESTROY_COMPANY_PATH =
"/companies/v2/companies/:company_id"
GET_COMPANY_CONTACTS_PATH =
"/companies/v2/companies/:company_id/contacts"
BATCH_UPDATE_PATH =
"/companies/v1/batch-async/update"

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(response_hash) ⇒ Company

Returns a new instance of Company.



173
174
175
176
177
178
# File 'lib/hubspot/company.rb', line 173

def initialize(response_hash)
  props = response_hash['properties'] || {}
  @properties = Hubspot::Utils.properties_to_hash(props)
  @vid = response_hash["companyId"]
  @name = @properties.try(:[], "name")
end

Instance Attribute Details

#nameObject (readonly)

Returns the value of attribute name.



171
172
173
# File 'lib/hubspot/company.rb', line 171

def name
  @name
end

#propertiesObject (readonly)

Returns the value of attribute properties.



170
171
172
# File 'lib/hubspot/company.rb', line 170

def properties
  @properties
end

#vidObject (readonly)

Returns the value of attribute vid.



171
172
173
# File 'lib/hubspot/company.rb', line 171

def vid
  @vid
end

Class Method Details

.add_contact!(company_vid, contact_vid) ⇒ Object

Parameters:

  • company_vid (Integer)

    The ID of a company to add a contact to

  • contact_vid (Integer)

    contact id to add

Returns:

  • parsed response



160
161
162
163
164
165
166
167
# File 'lib/hubspot/company.rb', line 160

def add_contact!(company_vid, contact_vid)
  Hubspot::Connection.put_json(ADD_CONTACT_TO_COMPANY_PATH,
                               params: {
                                 company_id: company_vid,
                                 vid: contact_vid,
                               },
                               body: nil)
end

.all(opts = {}) ⇒ Array

Parameters:

  • opts (Hash) (defaults to: {})

    Possible options are: recently_updated [boolean] (for querying all accounts by modified time) count [Integer] for pagination offset [Integer] for pagination

Returns:

  • (Array)

    Array of Hubspot::Company records



29
30
31
32
33
34
35
36
37
38
39
40
41
# File 'lib/hubspot/company.rb', line 29

def all(opts={})
  recently_updated = opts.delete(:recently_updated) { false }
  # limit = opts.delete(:limit) { 20 }
  # skip = opts.delete(:skip) { 0 }
  path = if recently_updated
    RECENTLY_MODIFIED_COMPANIES_PATH
  else
    RECENTLY_CREATED_COMPANIES_PATH
  end

  response = Hubspot::Connection.get_json(path, opts)
  response['results'].map { |c| new(c) }
end

.all_with_offset(opts = {}) ⇒ Object

Find all companies by created date (descending)

recently_updated [boolean] (for querying all accounts by modified time)
count [Integer] for pagination
offset [Integer] for pagination

http://developers.hubspot.com/docs/methods/companies/get_companies_created http://developers.hubspot.com/docs/methods/companies/get_companies_modified response.results for [Array] response.hasMore for [Boolean] response.offset for [Integer]

Returns:

  • (Object)

    , you can get:



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

def all_with_offset(opts = {})
  recently_updated = opts.delete(:recently_updated) { false }

  path = if recently_updated
    RECENTLY_MODIFIED_COMPANIES_PATH
  else
    RECENTLY_CREATED_COMPANIES_PATH
  end

  response = Hubspot::Connection.get_json(path, opts)
  response_with_offset = {}
  response_with_offset['results'] = response['results'].map { |c| new(c) }
  response_with_offset['hasMore'] = response['hasMore']
  response_with_offset['offset'] = response['offset']
  response_with_offset
end

.batch_update!(companies) ⇒ Object

Updates the properties of companies NOTE: Up to 100 companies can be updated in a single request. There is no limit to the number of properties that can be updated per company. https://developers.hubspot.com/docs/methods/companies/batch-update-companies Returns a 202 Accepted response on success.



133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
# File 'lib/hubspot/company.rb', line 133

def batch_update!(companies)
  query = companies.map do |company|
    company_hash = company.with_indifferent_access
    if company_hash[:vid]
      # For consistency - Since vid has been used everywhere.
      company_param = {
        objectId: company_hash[:vid],
        properties: Hubspot::Utils.hash_to_properties(company_hash.except(:vid).stringify_keys!, key_name: 'name'),
      }
    elsif company_hash[:objectId]
      company_param = {
        objectId: company_hash[:objectId],
        properties: Hubspot::Utils.hash_to_properties(company_hash.except(:objectId).stringify_keys!, key_name: 'name'),
      }
    else
      raise Hubspot::InvalidParams, 'expecting vid or objectId for company'
    end
    company_param
  end
  Hubspot::Connection.post_json(BATCH_UPDATE_PATH, params: {}, body: query)
end

.create!(name, params = {}) ⇒ Hubspot::Company

Parameters:

  • name (String)

Returns:



122
123
124
125
126
127
# File 'lib/hubspot/company.rb', line 122

def create!(name, params={})
  params_with_name = params.stringify_keys.merge("name" => name)
  post_data = {properties: Hubspot::Utils.hash_to_properties(params_with_name, key_name: "name")}
  response = Hubspot::Connection.post_json(CREATE_COMPANY_PATH, params: {}, body: post_data )
  new(response)
end

.find_by_domain(domain, options = {}) ⇒ Array

Parameters:

  • domain (String)

    company domain to search by

  • options (Hash) (defaults to: {})

    Possible options are: limit [Integer] for pagination properties [Array] list of company properties to recieve offset_company_id [Integer] for pagination (should be company ID)

Returns:

  • (Array)

    Array of Hubspot::Company records

Raises:



78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
# File 'lib/hubspot/company.rb', line 78

def find_by_domain(domain, options = {})
  raise Hubspot::InvalidParams, 'expecting String parameter' unless domain.try(:is_a?, String)

  limit = options.fetch(:limit, 100)
  properties = options.fetch(:properties) { Hubspot::CompanyProperties.all.map { |property| property["name"] } }
  offset_company_id = options.fetch(:offset_company_id, nil)

  post_data = {
    "limit" => limit,
    "requestOptions" => {
      "properties" => properties
    }
  }
  post_data["offset"] = {
    "isPrimary" => true,
    "companyId" => offset_company_id
  } if offset_company_id

  companies = []
  begin
    response = Hubspot::Connection.post_json(GET_COMPANY_BY_DOMAIN_PATH, params: { domain: domain }, body: post_data )
    companies = response["results"].try(:map) { |company| new(company) }
  rescue => e
    raise e unless e.message =~ /not found/ # 404 / hanle the error and kindly return an empty array
  end
  companies
end

.find_by_id(id) ⇒ Hubspot::Company

Parameters:

  • id (Integer)

    company id to search by

Returns:

Raises:



110
111
112
113
114
115
116
# File 'lib/hubspot/company.rb', line 110

def find_by_id(id)
  path = GET_COMPANY_BY_ID_PATH
  params = { company_id: id }
  raise Hubspot::InvalidParams, 'expecting Integer parameter' unless id.try(:is_a?, Integer)
  response = Hubspot::Connection.get_json(path, params)
  new(response)
end

Instance Method Details

#[](property) ⇒ Object



180
181
182
# File 'lib/hubspot/company.rb', line 180

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

#add_contact(contact_or_vid) ⇒ Hubspot::Company

Parameters:

  • id (Integer)

    contact id to add

Returns:



217
218
219
220
221
222
223
224
225
# File 'lib/hubspot/company.rb', line 217

def add_contact(contact_or_vid)
  contact_vid = if contact_or_vid.is_a?(Hubspot::Contact)
                  contact_or_vid.vid
                else
                  contact_or_vid
                end
  self.class.add_contact!(vid, contact_vid)
  self
end

#contactsArray

Returns:

  • (Array)

    Array of Hubspot::Contact records



242
243
244
245
246
247
# File 'lib/hubspot/company.rb', line 242

def contacts
  response = Hubspot::Connection.get_json(GET_COMPANY_CONTACTS_PATH, company_id: vid)
  response['contacts'].each_with_object([]) do |contact, memo|
    memo << Hubspot::Contact.find_by_id(contact['vid'])
  end
end

#destroy!TrueClass

Returns:

  • (TrueClass)

    true



230
231
232
233
# File 'lib/hubspot/company.rb', line 230

def destroy!
  Hubspot::Connection.delete_json(DESTROY_COMPANY_PATH, { company_id: vid })
  @destroyed = true
end

#destroyed?Boolean

Returns:

  • (Boolean)


235
236
237
# File 'lib/hubspot/company.rb', line 235

def destroyed?
  !!@destroyed
end

#get_contact_vidsArray

Gets ALL contact vids of a company May make many calls if the company has a mega-ton of contacts http://developers.hubspot.com/docs/methods/companies/get_company_contacts_by_id

Returns:

  • (Array)

    contact vids



199
200
201
202
203
204
205
206
207
208
209
210
211
# File 'lib/hubspot/company.rb', line 199

def get_contact_vids
  vid_offset = nil
  vids = []
  loop do
    data = Hubspot::Connection.get_json(GET_COMPANY_CONTACT_VIDS_PATH,
                                        company_id: vid,
                                        vidOffset: vid_offset)
    vids += data['vids']
    return vids unless data['hasMore']
    vid_offset = data['vidOffset']
  end
  vids # this statement will never be executed.
end

#update!(params) ⇒ Hubspot::Company

Parameters:

  • params (Hash)

    hash of properties to update

Returns:



188
189
190
191
192
193
# File 'lib/hubspot/company.rb', line 188

def update!(params)
  query = {"properties" => Hubspot::Utils.hash_to_properties(params.stringify_keys!, key_name: "name")}
  Hubspot::Connection.put_json(UPDATE_COMPANY_PATH, params: { company_id: vid }, body: query)
  @properties.merge!(params)
  self
end