-
Notifications
You must be signed in to change notification settings - Fork 256
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
Use crm associations endpoints #210
Open
fonji
wants to merge
10
commits into
HubspotCommunity:master
Choose a base branch
from
fonji:use_crm_associations_endpoints
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
0eaa795
Add assocation object
fonji d22c108
Add create specs
fonji 25f26ea
Add batch_create specs
fonji d5f20d2
Add association.delete specs
fonji dc9998d
Company uses Association to associate Contact
fonji 77d156c
Add documentation
fonji 3f137a2
Deal.associate use Association
fonji b2c47e7
Deal.associate uses Association
fonji 84540cb
Add Association.all
fonji 9f29322
Deal.find_by_association uses Association
fonji File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,80 @@ | ||
class Hubspot::Association | ||
COMPANY_TO_CONTACT = 2 | ||
DEAL_TO_CONTACT = 3 | ||
CONTACT_TO_DEAL = 4 | ||
DEAL_TO_COMPANY = 5 | ||
COMPANY_TO_DEAL = 6 | ||
DEFINITION_TARGET_TO_CLASS = { | ||
2 => Hubspot::Contact, | ||
3 => Hubspot::Contact, | ||
4 => Hubspot::Deal, | ||
5 => Hubspot::Company, | ||
6 => Hubspot::Deal | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Can't use |
||
}.freeze | ||
|
||
BATCH_CREATE_PATH = '/crm-associations/v1/associations/create-batch' | ||
BATCH_DELETE_PATH = '/crm-associations/v1/associations/delete-batch' | ||
ASSOCIATIONS_PATH = '/crm-associations/v1/associations/:resource_id/HUBSPOT_DEFINED/:definition_id' | ||
|
||
class << self | ||
def create(from_id, to_id, definition_id) | ||
batch_create([{ from_id: from_id, to_id: to_id, definition_id: definition_id }]) | ||
end | ||
|
||
# Make multiple associations in a single API call | ||
# {https://developers.hubspot.com/docs/methods/crm-associations/batch-associate-objects} | ||
# usage: | ||
# Hubspot::Association.batch_create([{ from_id: 1, to_id: 2, definition_id: Hubspot::Association::COMPANY_TO_CONTACT }]) | ||
def batch_create(associations) | ||
request = associations.map { |assocation| build_association_body(assocation) } | ||
Hubspot::Connection.put_json(BATCH_CREATE_PATH, params: { no_parse: true }, body: request).success? | ||
end | ||
|
||
def delete(from_id, to_id, definition_id) | ||
batch_delete([{from_id: from_id, to_id: to_id, definition_id: definition_id}]) | ||
end | ||
|
||
# Remove multiple associations in a single API call | ||
# {https://developers.hubspot.com/docs/methods/crm-associations/batch-delete-associations} | ||
# usage: | ||
# Hubspot::Association.batch_delete([{ from_id: 1, to_id: 2, definition_id: Hubspot::Association::COMPANY_TO_CONTACT }]) | ||
def batch_delete(associations) | ||
request = associations.map { |assocation| build_association_body(assocation) } | ||
Hubspot::Connection.put_json(BATCH_DELETE_PATH, params: { no_parse: true }, body: request).success? | ||
end | ||
|
||
# Retrieve all associated resources given a source (resource_id) and a kind (definition_id) | ||
# Example: if resource_id is a deal, using DEAL_TO_CONTACT will find every contact associated with the deal | ||
# {https://developers.hubspot.com/docs/methods/crm-associations/get-associations} | ||
# Warning: it will make N+M queries, where | ||
# N is the number of PagedCollection requests necessary to get all ids, | ||
# and M is the number of results, each resulting in a find | ||
# usage: | ||
# Hubspot::Association.all(42, Hubspot::Association::DEAL_TO_CONTACT) | ||
def all(resource_id, definition_id) | ||
opts = { resource_id: resource_id, definition_id: definition_id } | ||
klass = DEFINITION_TARGET_TO_CLASS[definition_id] | ||
raise(Hubspot::InvalidParams, 'Definition not supported') unless klass.present? | ||
|
||
collection = Hubspot::PagedCollection.new(opts) do |options, offset, limit| | ||
params = options.merge(offset: offset, limit: limit) | ||
response = Hubspot::Connection.get_json(ASSOCIATIONS_PATH, params) | ||
|
||
resources = response['results'].map { |result| klass.find(result) } | ||
[resources, response['offset'], response['has-more']] | ||
end | ||
collection.resources | ||
end | ||
|
||
private | ||
|
||
def build_association_body(assocation) | ||
{ | ||
fromObjectId: assocation[:from_id], | ||
toObjectId: assocation[:to_id], | ||
category: 'HUBSPOT_DEFINED', | ||
definitionId: assocation[:definition_id] | ||
} | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I added the bare minimum, the long list is long and is here.
I guess we can make this list grow with time?