Class: Hubspot::Company
- Inherits:
-
Object
- Object
- Hubspot::Company
- Defined in:
- lib/hubspot/company.rb
Overview
HubSpot Companies API
http://developers.hubspot.com/docs/methods/companies/companies-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
-
#name ⇒ Object
readonly
Returns the value of attribute name.
-
#properties ⇒ Object
readonly
Returns the value of attribute properties.
-
#vid ⇒ Object
readonly
Returns the value of attribute vid.
Class Method Summary collapse
-
.add_contact!(company_vid, contact_vid) ⇒ Object
Adds contact to a company http://developers.hubspot.com/docs/methods/companies/add_contact_to_company.
-
.all(opts = {}) ⇒ Array
Find all companies by created date (descending) http://developers.hubspot.com/docs/methods/companies/get_companies_created http://developers.hubspot.com/docs/methods/companies/get_companies_modified.
-
.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].
-
.batch_update!(companies) ⇒ Object
Updates the properties of companies NOTE: Up to 100 companies can be updated in a single request.
-
.create!(name, params = {}) ⇒ Hubspot::Company
Creates a company with a name http://developers.hubspot.com/docs/methods/companies/create_company.
-
.find_by_domain(domain, options = {}) ⇒ Array
Finds a list of companies by domain https://developers.hubspot.com/docs/methods/companies/search_companies_by_domain.
-
.find_by_id(id) ⇒ Hubspot::Company
Finds a company by domain http://developers.hubspot.com/docs/methods/companies/get_company.
Instance Method Summary collapse
- #[](property) ⇒ Object
-
#add_contact(contact_or_vid) ⇒ Hubspot::Company
Adds contact to a company http://developers.hubspot.com/docs/methods/companies/add_contact_to_company.
-
#contacts ⇒ Array
Finds company contacts http://developers.hubspot.com/docs/methods/companies/get_company_contacts.
-
#destroy! ⇒ TrueClass
Archives the company in hubspot http://developers.hubspot.com/docs/methods/companies/delete_company.
- #destroyed? ⇒ Boolean
-
#get_contact_vids ⇒ Array
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.
-
#initialize(response_hash) ⇒ Company
constructor
A new instance of Company.
-
#update!(params) ⇒ Hubspot::Company
Updates the properties of a company http://developers.hubspot.com/docs/methods/companies/update_company.
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
#name ⇒ Object (readonly)
Returns the value of attribute name.
171 172 173 |
# File 'lib/hubspot/company.rb', line 171 def name @name end |
#properties ⇒ Object (readonly)
Returns the value of attribute properties.
170 171 172 |
# File 'lib/hubspot/company.rb', line 170 def properties @properties end |
#vid ⇒ Object (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
Adds contact to a company http://developers.hubspot.com/docs/methods/companies/add_contact_to_company
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
Find all companies by created date (descending) http://developers.hubspot.com/docs/methods/companies/get_companies_created http://developers.hubspot.com/docs/methods/companies/get_companies_modified
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]
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
Creates a company with a name http://developers.hubspot.com/docs/methods/companies/create_company
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
Finds a list of companies by domain https://developers.hubspot.com/docs/methods/companies/search_companies_by_domain
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, = {}) raise Hubspot::InvalidParams, 'expecting String parameter' unless domain.try(:is_a?, String) limit = .fetch(:limit, 100) properties = .fetch(:properties) { Hubspot::CompanyProperties.all.map { |property| property["name"] } } offset_company_id = .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. =~ /not found/ # 404 / hanle the error and kindly return an empty array end companies end |
.find_by_id(id) ⇒ Hubspot::Company
Finds a company by domain http://developers.hubspot.com/docs/methods/companies/get_company
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
Adds contact to a company http://developers.hubspot.com/docs/methods/companies/add_contact_to_company
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 |
#contacts ⇒ Array
Finds company contacts http://developers.hubspot.com/docs/methods/companies/get_company_contacts
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
Archives the company in hubspot http://developers.hubspot.com/docs/methods/companies/delete_company
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
235 236 237 |
# File 'lib/hubspot/company.rb', line 235 def destroyed? !!@destroyed end |
#get_contact_vids ⇒ Array
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
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
Updates the properties of a company http://developers.hubspot.com/docs/methods/companies/update_company
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 |