From 9293d11015005cad4b259dfde75bd5862b3fae63 Mon Sep 17 00:00:00 2001 From: vikas-a2 Date: Thu, 1 Aug 2019 16:58:07 +0530 Subject: [PATCH 1/7] Made the changes accordingly to save the hubspot company and contact --- lib/hubspot/company.rb | 33 ++++++++++++++++++++++++++++----- lib/hubspot/contact.rb | 4 +--- 2 files changed, 29 insertions(+), 8 deletions(-) diff --git a/lib/hubspot/company.rb b/lib/hubspot/company.rb index 8f6e2d77..fd976c80 100644 --- a/lib/hubspot/company.rb +++ b/lib/hubspot/company.rb @@ -15,6 +15,7 @@ class Hubspot::Company < Hubspot::Resource REMOVE_CONTACT_PATH = '/companies/v2/companies/:id/contacts/:contact_id' SEARCH_DOMAIN_PATH = '/companies/v2/domains/:domain/companies' UPDATE_PATH = '/companies/v2/companies/:id' + ASSOCIATE_COMPANY_TO_CONTACT_PATH = '/crm-associations/v1/associations' class << self def all(opts = {}) @@ -33,26 +34,48 @@ def all(opts = {}) def search_domain(domain, opts = {}) Hubspot::PagedCollection.new(opts) do |options, offset, limit| request = { - "limit" => limit, - "requestOptions" => options, + "limit" => 2, + "requestOptions" => { 'properties': ['domain', 'createdate', 'name', 'hs_lastmodifieddate'] }, "offset" => { "isPrimary" => true, - "companyId" => offset + "companyId" => 0 } } - 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 + def create_or_update(company_id, properties = {}) + if company_id.present? + path = UPDATE_PATH + params = { id: company_id} + else + path = CREATE_PATH + params = {} + end + request = JSON.parse(properties) + if company_id.present? + response = Hubspot::Connection.put_json(path, params: params, body: request) + else + response = Hubspot::Connection.post_json(path, params: params, body: request) + end + from_result(response) + end + + def associate_contact_to_company(properties = {}) + response = Hubspot::Connection.put_json( + ASSOCIATE_COMPANY_TO_CONTACT_PATH, + params: {}, body: properties + ) + end + def recently_created(opts = {}) Hubspot::PagedCollection.new(opts) do |options, offset, limit| response = Hubspot::Connection.get_json( diff --git a/lib/hubspot/contact.rb b/lib/hubspot/contact.rb index 7101c048..5ae40781 100644 --- a/lib/hubspot/contact.rb +++ b/lib/hubspot/contact.rb @@ -42,9 +42,7 @@ def create(email, properties = {}) end def create_or_update(email, properties = {}) - request = { - properties: Hubspot::Utils.hash_to_properties(properties.stringify_keys, key_name: "property") - } + request = JSON.parse(properties) response = Hubspot::Connection.post_json(CREATE_OR_UPDATE_PATH, params: {email: email}, body: request) from_result(response) end From 092b90d31b63b752e0e69b8dd9975c6750f3504d Mon Sep 17 00:00:00 2001 From: vikas-a2 Date: Thu, 1 Aug 2019 16:59:17 +0530 Subject: [PATCH 2/7] Revert "Made the changes accordingly to save the hubspot company and contact" This reverts commit 9293d11015005cad4b259dfde75bd5862b3fae63. --- lib/hubspot/company.rb | 33 +++++---------------------------- lib/hubspot/contact.rb | 4 +++- 2 files changed, 8 insertions(+), 29 deletions(-) diff --git a/lib/hubspot/company.rb b/lib/hubspot/company.rb index fd976c80..8f6e2d77 100644 --- a/lib/hubspot/company.rb +++ b/lib/hubspot/company.rb @@ -15,7 +15,6 @@ class Hubspot::Company < Hubspot::Resource REMOVE_CONTACT_PATH = '/companies/v2/companies/:id/contacts/:contact_id' SEARCH_DOMAIN_PATH = '/companies/v2/domains/:domain/companies' UPDATE_PATH = '/companies/v2/companies/:id' - ASSOCIATE_COMPANY_TO_CONTACT_PATH = '/crm-associations/v1/associations' class << self def all(opts = {}) @@ -34,48 +33,26 @@ def all(opts = {}) def search_domain(domain, opts = {}) Hubspot::PagedCollection.new(opts) do |options, offset, limit| request = { - "limit" => 2, - "requestOptions" => { 'properties': ['domain', 'createdate', 'name', 'hs_lastmodifieddate'] }, + "limit" => limit, + "requestOptions" => options, "offset" => { "isPrimary" => true, - "companyId" => 0 + "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 - def create_or_update(company_id, properties = {}) - if company_id.present? - path = UPDATE_PATH - params = { id: company_id} - else - path = CREATE_PATH - params = {} - end - request = JSON.parse(properties) - if company_id.present? - response = Hubspot::Connection.put_json(path, params: params, body: request) - else - response = Hubspot::Connection.post_json(path, params: params, body: request) - end - from_result(response) - end - - def associate_contact_to_company(properties = {}) - response = Hubspot::Connection.put_json( - ASSOCIATE_COMPANY_TO_CONTACT_PATH, - params: {}, body: properties - ) - end - def recently_created(opts = {}) Hubspot::PagedCollection.new(opts) do |options, offset, limit| response = Hubspot::Connection.get_json( diff --git a/lib/hubspot/contact.rb b/lib/hubspot/contact.rb index 5ae40781..7101c048 100644 --- a/lib/hubspot/contact.rb +++ b/lib/hubspot/contact.rb @@ -42,7 +42,9 @@ def create(email, properties = {}) end def create_or_update(email, properties = {}) - request = JSON.parse(properties) + request = { + properties: Hubspot::Utils.hash_to_properties(properties.stringify_keys, key_name: "property") + } response = Hubspot::Connection.post_json(CREATE_OR_UPDATE_PATH, params: {email: email}, body: request) from_result(response) end From 127d332e335df36699911617459162702fa04298 Mon Sep 17 00:00:00 2001 From: vikas-a2 Date: Thu, 1 Aug 2019 17:01:59 +0530 Subject: [PATCH 3/7] Made the changes accordingly to save the hubspot company and associated contact --- lib/hubspot/company.rb | 31 ++++++++++++++++++++++++++++--- lib/hubspot/contact.rb | 4 +--- 2 files changed, 29 insertions(+), 6 deletions(-) diff --git a/lib/hubspot/company.rb b/lib/hubspot/company.rb index 8f6e2d77..a2354568 100644 --- a/lib/hubspot/company.rb +++ b/lib/hubspot/company.rb @@ -15,6 +15,7 @@ class Hubspot::Company < Hubspot::Resource REMOVE_CONTACT_PATH = '/companies/v2/companies/:id/contacts/:contact_id' SEARCH_DOMAIN_PATH = '/companies/v2/domains/:domain/companies' UPDATE_PATH = '/companies/v2/companies/:id' + ASSOCIATE_COMPANY_TO_CONTACT_PATH = '/crm-associations/v1/associations' class << self def all(opts = {}) @@ -33,11 +34,11 @@ def all(opts = {}) def search_domain(domain, opts = {}) Hubspot::PagedCollection.new(opts) do |options, offset, limit| request = { - "limit" => limit, - "requestOptions" => options, + "limit" => 2, + "requestOptions" => { 'properties': ['domain', 'createdate', 'name', 'hs_lastmodifieddate'] }, "offset" => { "isPrimary" => true, - "companyId" => offset + "companyId" => 0 } } @@ -53,6 +54,30 @@ def search_domain(domain, opts = {}) end end + def create_or_update(company_id, properties = {}) + if company_id.present? + path = UPDATE_PATH + params = { id: company_id} + else + path = CREATE_PATH + params = {} + end + request = JSON.parse(properties) + if company_id.present? + response = Hubspot::Connection.put_json(path, params: params, body: request) + else + response = Hubspot::Connection.post_json(path, params: params, body: request) + end + from_result(response) + end + + def associate_contact_to_company(properties = {}) + response = Hubspot::Connection.put_json( + ASSOCIATE_COMPANY_TO_CONTACT_PATH, + params: {}, body: properties + ) + end + def recently_created(opts = {}) Hubspot::PagedCollection.new(opts) do |options, offset, limit| response = Hubspot::Connection.get_json( diff --git a/lib/hubspot/contact.rb b/lib/hubspot/contact.rb index 7101c048..5ae40781 100644 --- a/lib/hubspot/contact.rb +++ b/lib/hubspot/contact.rb @@ -42,9 +42,7 @@ def create(email, properties = {}) end def create_or_update(email, properties = {}) - request = { - properties: Hubspot::Utils.hash_to_properties(properties.stringify_keys, key_name: "property") - } + request = JSON.parse(properties) response = Hubspot::Connection.post_json(CREATE_OR_UPDATE_PATH, params: {email: email}, body: request) from_result(response) end From ee2c0b6194249188d0d721d71b7d42ee962c6e1f Mon Sep 17 00:00:00 2001 From: Vikas Kamsabai Date: Mon, 11 Nov 2019 11:53:35 +0530 Subject: [PATCH 4/7] Added the company update method separately --- lib/hubspot/company.rb | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/lib/hubspot/company.rb b/lib/hubspot/company.rb index a2354568..ec3f2e48 100644 --- a/lib/hubspot/company.rb +++ b/lib/hubspot/company.rb @@ -54,6 +54,18 @@ def search_domain(domain, opts = {}) end end + def update_company(company_id, properties = {}) + if company_id.present? + path = UPDATE_PATH + params = { id: company_id} + end + request = JSON.parse(properties) + if company_id.present? + response = Hubspot::Connection.put_json(path, params: params, body: request) + end + from_result(response) + end + def create_or_update(company_id, properties = {}) if company_id.present? path = UPDATE_PATH From 1a0e524a944eb4b265c5cf49d5f19b014c05d43d Mon Sep 17 00:00:00 2001 From: Vikas Kamsabai Date: Wed, 20 Nov 2019 12:14:38 +0530 Subject: [PATCH 5/7] Added update only method to update the contact --- lib/hubspot/contact.rb | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/lib/hubspot/contact.rb b/lib/hubspot/contact.rb index 5ae40781..c408eed0 100644 --- a/lib/hubspot/contact.rb +++ b/lib/hubspot/contact.rb @@ -47,6 +47,12 @@ def create_or_update(email, properties = {}) from_result(response) end + def update_contact(contact, properties = {}) + request = JSON.parse(properties) + params = { id: contact, no_parse: true} + response = Hubspot::Connection.post_json(UPDATE_PATH, params: params, body: request) + end + def search(query, opts = {}) Hubspot::PagedCollection.new(opts) do |options, offset, limit| response = Hubspot::Connection.get_json( From 282a066e1fc6d14885d6acfad31e33f7db59487a Mon Sep 17 00:00:00 2001 From: vikas k Date: Wed, 13 Jan 2021 22:05:47 +0530 Subject: [PATCH 6/7] Added fixes for hubspot sentry errors --- lib/hubspot/company.rb | 8 ++++++++ lib/hubspot/connection.rb | 2 +- 2 files changed, 9 insertions(+), 1 deletion(-) diff --git a/lib/hubspot/company.rb b/lib/hubspot/company.rb index ec3f2e48..f1aa4887 100644 --- a/lib/hubspot/company.rb +++ b/lib/hubspot/company.rb @@ -158,6 +158,14 @@ def batch_update(companies, opts = {}) true end + + def company_by_id(company_id) + if company_id.present? + path = FIND_PATH + params = { id: company_id } + response = Hubspot::Connection.get_json(path, params: params) + end + end end def contacts(opts = {}) diff --git a/lib/hubspot/connection.rb b/lib/hubspot/connection.rb index 7a9c4b48..f0fcb0da 100644 --- a/lib/hubspot/connection.rb +++ b/lib/hubspot/connection.rb @@ -4,7 +4,7 @@ class Connection class << self def get_json(path, opts) - url = generate_url(path, opts) + url = generate_url(path, opts[:params]) response = get(url, format: :json, read_timeout: read_timeout(opts), open_timeout: open_timeout(opts)) log_request_and_response url, response handle_response(response) From 5f9deddd3767e6fa4abae1817fa722c47e71448e Mon Sep 17 00:00:00 2001 From: vikas k Date: Tue, 27 Apr 2021 16:32:28 +0530 Subject: [PATCH 7/7] Modified batch update of company --- lib/hubspot/company.rb | 26 +++++++++++++------------- 1 file changed, 13 insertions(+), 13 deletions(-) diff --git a/lib/hubspot/company.rb b/lib/hubspot/company.rb index f1aa4887..5a30959d 100644 --- a/lib/hubspot/company.rb +++ b/lib/hubspot/company.rb @@ -133,21 +133,21 @@ def remove_contact(id, contact_id) true end - 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 + def batch_update(request) + # 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! + request.compact return true if request.empty? Hubspot::Connection.post_json(