Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Hubspot ten secondly rolling #229

Open
wants to merge 10 commits into
base: master
Choose a base branch
from
75 changes: 60 additions & 15 deletions lib/hubspot/company.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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 = {})
Expand All @@ -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
}
}

Expand All @@ -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(
Expand Down Expand Up @@ -96,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
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
# 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(
Expand All @@ -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 = {})
Expand Down
2 changes: 1 addition & 1 deletion lib/hubspot/connection.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
10 changes: 7 additions & 3 deletions lib/hubspot/contact.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down