class Hubspot::Deal

HubSpot Deals API

{developers.hubspot.com/docs/methods/deals/deals_overview}

Constants

ALL_DEALS_PATH
CREATE_DEAL_PATH
DEAL_PATH
RECENT_UPDATED_PATH
UPDATE_DEAL_PATH

Attributes

company_ids[R]
deal_id[R]
portal_id[R]
properties[R]
vids[R]

Public Class Methods

all(opts = {}) click to toggle source
# File lib/hubspot/deal.rb, line 95
def all(opts = {})
  path = ALL_DEALS_PATH

  opts[:includeAssociations] = true # Needed for initialize to work
  response = Hubspot::Connection.get_json(path, opts)

  result = {}
  result['deals'] = response['deals'].map { |d| new(d) }
  result['offset'] = response['offset']
  result['hasMore'] = response['hasMore']
  return result
end
associate!(deal_id, company_ids=[], vids=[]) click to toggle source

Associate a deal with a contact or company {developers.hubspot.com/docs/methods/deals/associate_deal} Usage Hubspot::Deal.associate!(45146940, [32], [52])

# File lib/hubspot/deal.rb, line 66
def associate!(deal_id, company_ids=[], vids=[])
  associations = company_ids.map do |id|
    { from_id: deal_id, to_id: id, definition_id: Hubspot::Association::DEAL_TO_COMPANY }
  end
  associations += vids.map do |id|
    { from_id: deal_id, to_id: id, definition_id: Hubspot::Association::DEAL_TO_CONTACT }
  end
  Hubspot::Association.batch_create(associations)
end
create!(portal_id, company_ids, vids, params={}) click to toggle source
# File lib/hubspot/deal.rb, line 31
def create!(portal_id, company_ids, vids, params={})
  #TODO: clean following hash, Hubspot::Utils should do the trick
  associations_hash = {"portalId" => portal_id, "associations" => { "associatedCompanyIds" => company_ids, "associatedVids" => vids}}
  post_data = associations_hash.merge({ properties: Hubspot::Utils.hash_to_properties(params, key_name: "name") })

  response = Hubspot::Connection.post_json(CREATE_DEAL_PATH, params: {}, body: post_data )
  new(response)
end
dissociate!(deal_id, company_ids=[], vids=[]) click to toggle source

Didssociate a deal with a contact or company {developers.hubspot.com/docs/methods/deals/delete_association} Usage Hubspot::Deal.dissociate!(45146940, [32], [52])

# File lib/hubspot/deal.rb, line 80
def dissociate!(deal_id, company_ids=[], vids=[])
  associations = company_ids.map do |id|
    { from_id: deal_id, to_id: id, definition_id: Hubspot::Association::DEAL_TO_COMPANY }
  end
  associations += vids.map do |id|
    { from_id: deal_id, to_id: id, definition_id: Hubspot::Association::DEAL_TO_CONTACT }
  end
  Hubspot::Association.batch_delete(associations)
end
find(deal_id) click to toggle source
# File lib/hubspot/deal.rb, line 90
def find(deal_id)
  response = Hubspot::Connection.get_json(DEAL_PATH, { deal_id: deal_id })
  new(response)
end
find_by_association(object) click to toggle source

Find all deals associated to a contact or company @param object [Hubspot::Contact || Hubspot::Company] a contact or company @return [Array] Array of Hubspot::Deal records

# File lib/hubspot/deal.rb, line 136
def find_by_association(object)
  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
find_by_company(company) click to toggle source

Find all deals associated to a company {developers.hubspot.com/docs/methods/deals/get-associated-deals} @param company [Hubspot::Company] the company @return [Array] Array of Hubspot::Deal records

# File lib/hubspot/deal.rb, line 121
def find_by_company(company)
  find_by_association company
end
find_by_contact(contact) click to toggle source

Find all deals associated to a contact {developers.hubspot.com/docs/methods/deals/get-associated-deals} @param contact [Hubspot::Contact] the contact @return [Array] Array of Hubspot::Deal records

# File lib/hubspot/deal.rb, line 129
def find_by_contact(contact)
  find_by_association contact
end
new(response_hash) click to toggle source
# File lib/hubspot/deal.rb, line 22
def initialize(response_hash)
  @portal_id = response_hash["portalId"]
  @deal_id = response_hash["dealId"]
  @company_ids = response_hash["associations"]["associatedCompanyIds"]
  @vids = response_hash["associations"]["associatedVids"]
  @properties = Hubspot::Utils.properties_to_hash(response_hash["properties"])
end
recent(opts = {}) click to toggle source

Find recent updated deals. {developers.hubspot.com/docs/methods/deals/get_deals_modified} @param count [Integer] the amount of deals to return. @param offset [Integer] pages back through recent contacts.

# File lib/hubspot/deal.rb, line 112
def recent(opts = {})
  response = Hubspot::Connection.get_json(RECENT_UPDATED_PATH, opts)
  response['results'].map { |d| new(d) }
end
update(id, properties = {}) click to toggle source

Updates the properties of a deal {developers.hubspot.com/docs/methods/deals/update_deal} @param deal_id [Integer] hubspot deal_id @param params [Hash] hash of properties to update @return [boolean] success

# File lib/hubspot/deal.rb, line 45
def update(id, properties = {})
  update!(id, properties)
rescue Hubspot::RequestError => e
  false
end
update!(id, properties = {}) click to toggle source

Updates the properties of a deal {developers.hubspot.com/docs/methods/deals/update_deal} @param deal_id [Integer] hubspot deal_id @param params [Hash] hash of properties to update @return [Hubspot::Deal] Deal record

# File lib/hubspot/deal.rb, line 56
def update!(id, properties = {})
  request = { properties: Hubspot::Utils.hash_to_properties(properties.stringify_keys, key_name: 'name') }
  response = Hubspot::Connection.put_json(UPDATE_DEAL_PATH, params: { deal_id: id, no_parse: true }, body: request)
  response.success?
end

Public Instance Methods

[](property) click to toggle source
# File lib/hubspot/deal.rb, line 158
def [](property)
  @properties[property]
end
destroy!() click to toggle source

Archives the contact in hubspot {developers.hubspot.com/docs/methods/contacts/delete_contact} @return [TrueClass] true

# File lib/hubspot/deal.rb, line 149
def destroy!
  Hubspot::Connection.delete_json(DEAL_PATH, {deal_id: deal_id})
  @destroyed = true
end
destroyed?() click to toggle source
# File lib/hubspot/deal.rb, line 154
def destroyed?
  !!@destroyed
end
update(params)
Alias for: update!
update!(params) click to toggle source

Updates the properties of a deal {developers.hubspot.com/docs/methods/deals/update_deal} @param params [Hash] hash of properties to update @return [Hubspot::Deal] self

# File lib/hubspot/deal.rb, line 166
def update!(params)
  query = { 'properties' => Hubspot::Utils.hash_to_properties(params.stringify_keys!, key_name: 'name') }
  Hubspot::Connection.put_json(UPDATE_DEAL_PATH, params: { deal_id: deal_id }, body: query)
  @properties.merge!(params)
  self
end
Also aliased as: update