diff --git a/lib/hubspot/company.rb b/lib/hubspot/company.rb index 8f6e2d77..f1aa4887 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,42 @@ 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 + 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( @@ -121,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) diff --git a/lib/hubspot/contact.rb b/lib/hubspot/contact.rb index 7101c048..c408eed0 100644 --- a/lib/hubspot/contact.rb +++ b/lib/hubspot/contact.rb @@ -42,13 +42,17 @@ 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 + 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(