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

Deal.associate! can associate contacts and companies in a single call #120

Closed
Show file tree
Hide file tree
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
30 changes: 19 additions & 11 deletions lib/hubspot/deal.rb
Original file line number Diff line number Diff line change
Expand Up @@ -38,16 +38,15 @@ def create!(portal_id, company_ids, vids, params={})
new(response)
end

# Associate a deal with a contact or company
# {http://developers.hubspot.com/docs/methods/deals/associate_deal}
# Usage
# Hubspot::Deal.associate!(45146940, [], [52])
def associate!(deal_id, company_ids=[], vids=[])
objecttype = company_ids.any? ? 'COMPANY' : 'CONTACT'
object_ids = (company_ids.any? ? company_ids : vids).join('&id=')
Hubspot::Connection.put_json(ASSOCIATE_DEAL_PATH, params: { deal_id: deal_id, OBJECTTYPE: objecttype, objectId: object_ids}, body: {})
end

# Associate a deal with contacts and/or companies
# {http://developers.hubspot.com/docs/methods/deals/associate_deal}
# Can make up to two API calls, one per object type.
# Usage
# Hubspot::Deal.associate!(45146940, [], [52])
def associate!(deal_id, company_ids=[], vids=[])
associate(deal_id, 'CONTACT', vids) if vids.any?
associate(deal_id, 'COMPANY', company_ids) if company_ids.any?
end

def find(deal_id)
response = Hubspot::Connection.get_json(DEAL_PATH, { deal_id: deal_id })
Expand All @@ -62,7 +61,7 @@ def recent(opts = {})
response = Hubspot::Connection.get_json(RECENT_UPDATED_PATH, opts)
response['results'].map { |d| new(d) }
end

# Find all deals associated to a company
# {http://developers.hubspot.com/docs/methods/deals/get-associated-deals}
# @param company [Hubspot::Company] the company
Expand All @@ -74,6 +73,15 @@ def find_by_company(company)
response["results"].map { |deal_id| find(deal_id) }
end

private

def associate(deal_id, object_type, ids)
Hubspot::Connection.put_json(
ASSOCIATE_DEAL_PATH,
params: { deal_id: deal_id, OBJECTTYPE: object_type, objectId: ids.join('&id=') },
body: {}
)
end
end

# Archives the contact in hubspot
Expand Down
270 changes: 270 additions & 0 deletions spec/fixtures/vcr_cassettes/deal_associate.yml

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

16 changes: 16 additions & 0 deletions spec/lib/hubspot/deal_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,22 @@
its(:vids) { should eql [vid]}
end

describe ".associate" do
cassette "deal_associate"
let(:deal) { Hubspot::Deal.create!(portal_id, [company_id], [vid], { amount: amount}) }
let(:company) { Hubspot::Company.create!("New Company #{Time.now.to_i}") }
let(:contact) { Hubspot::Contact.create!("newcontact#{Time.now.to_i}@hsgem.com") }

subject { Hubspot::Deal.associate!(deal.deal_id, [company.vid], [contact.vid]) }

it 'associates the deal to the contact and the company' do
subject
find_deal = Hubspot::Deal.find(deal.deal_id)
find_deal.company_ids.should eql [company.vid]
find_deal.vids.should eql [contact.vid]
end
end

describe ".find" do
cassette "deal_find"
let(:deal) {Hubspot::Deal.create!(portal_id, [company_id], [vid], { amount: amount})}
Expand Down