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

Adds batch update for contacts #204

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 28 additions & 0 deletions lib/hubspot/contact.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@ class Hubspot::Contact < Hubspot::Resource
MERGE_PATH = '/contacts/v1/contact/merge-vids/:id/'
SEARCH_PATH = '/contacts/v1/search/query'
UPDATE_PATH = '/contacts/v1/contact/vid/:id/profile'
UPDATE_PATH = '/contacts/v1/contact/vid/:id/profile'
BATCH_UPDATE_PATH = '/contacts/v1/contact/batch'

class << self
def all(opts = {})
Expand Down Expand Up @@ -70,6 +72,32 @@ def merge(primary, secondary)

true
end

def batch_update(contacts, opts = {})
request = contacts.map do |contact|
# Use the specified options or update with the changes
changes = opts.empty? ? contact.changes : opts

unless changes.empty?
{
"vid" => contact.id,
Copy link

@simkim simkim Jan 16, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could it be possible to allow batch update by email. API allow both (one choice by contact)

"properties" => changes.map { |k, v| { "property" => k, "value" => v } }
}
end
end

# Remove any objects without changes and return if there is nothing to update
request.compact!
return true if request.empty?

Hubspot::Connection.post_json(
BATCH_UPDATE_PATH,
params: {},
body: request
)

true
end
end

def name
Expand Down