Skip to content

Commit

Permalink
Deal.find_by_association uses Association
Browse files Browse the repository at this point in the history
  • Loading branch information
fonji committed Nov 21, 2019
1 parent 84540cb commit 9f29322
Show file tree
Hide file tree
Showing 5 changed files with 502 additions and 151 deletions.
18 changes: 6 additions & 12 deletions lib/hubspot/deal.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ class Deal
DEAL_PATH = "/deals/v1/deal/:deal_id"
RECENT_UPDATED_PATH = "/deals/v1/deal/recent/modified"
UPDATE_DEAL_PATH = '/deals/v1/deal/:deal_id'
ASSOCIATED_DEAL_PATH = "/deals/v1/deal/associated/:objectType/:objectId"

attr_reader :properties
attr_reader :portal_id
Expand Down Expand Up @@ -96,20 +95,15 @@ def find_by_contact(contact)
end

# Find all deals associated to a contact or company
# {http://developers.hubspot.com/docs/methods/deals/get-associated-deals}
# @param object [Hubspot::Contact || Hubspot::Company] a contact or company
# @return [Array] Array of Hubspot::Deal records
def find_by_association(object)
path = ASSOCIATED_DEAL_PATH
objectType = case object
when Hubspot::Company then :company
when Hubspot::Contact then :contact
else raise(Hubspot::InvalidParams, "Instance type not supported")
end

params = { objectType: objectType, objectId: object.id }
response = Hubspot::Connection.get_json(path, params)
response["results"].map { |deal_id| find(deal_id) }
definition = case object
when Hubspot::Company then Hubspot::Association::COMPANY_TO_DEAL
when Hubspot::Contact then Hubspot::Association::CONTACT_TO_DEAL
else raise(Hubspot::InvalidParams, 'Instance type not supported')
end
Hubspot::Association.all(object.id, definition)
end
end

Expand Down

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading

0 comments on commit 9f29322

Please sign in to comment.