Class: Hubspot::Deal

Inherits:
Object
  • Object
show all
Defined in:
lib/hubspot/deal.rb

Overview

Constant Summary collapse

ALL_DEALS_PATH =
"/deals/v1/deal/paged"
CREATE_DEAL_PATH =
"/deals/v1/deal"
DEAL_PATH =
"/deals/v1/deal/:deal_id"
RECENT_UPDATED_PATH =
"/deals/v1/deal/recent/modified"
UPDATE_DEAL_PATH =
'/deals/v1/deal/:deal_id'

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(response_hash) ⇒ Deal

Returns a new instance of Deal.



22
23
24
25
26
27
28
# 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

Instance Attribute Details

#company_idsObject (readonly)

Returns the value of attribute company_ids.



19
20
21
# File 'lib/hubspot/deal.rb', line 19

def company_ids
  @company_ids
end

#deal_idObject (readonly)

Returns the value of attribute deal_id.



18
19
20
# File 'lib/hubspot/deal.rb', line 18

def deal_id
  @deal_id
end

#portal_idObject (readonly)

Returns the value of attribute portal_id.



17
18
19
# File 'lib/hubspot/deal.rb', line 17

def portal_id
  @portal_id
end

#propertiesObject (readonly)

Returns the value of attribute properties.



16
17
18
# File 'lib/hubspot/deal.rb', line 16

def properties
  @properties
end

#vidsObject (readonly)

Returns the value of attribute vids.



20
21
22
# File 'lib/hubspot/deal.rb', line 20

def vids
  @vids
end

Class Method Details

.all(opts = {}) ⇒ Object



115
116
117
118
119
120
121
122
123
124
125
126
# File 'lib/hubspot/deal.rb', line 115

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 = []) ⇒ Object

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



66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
# File 'lib/hubspot/deal.rb', line 66

def associate!(deal_id, company_ids=[], vids=[])
  company_associations = associations = company_ids.map do |id|
    { from_id: deal_id, to_id: id }
  end

  contact_associations = vids.map do |id|
    { from_id: deal_id, to_id: id}
  end

  results = []
  if company_associations.any?
    results << HubSpot::Association.batch_create("Deal", "Company", company_associations)
  end
  if contact_associations.any?
    results << HubSpot::Association.batch_create("Deal", "Contact", contact_associations)
  end

  results.all?
end

.create!(portal_id, company_ids, vids, params = {}) ⇒ Object



31
32
33
34
35
36
37
38
# 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 = []) ⇒ Object

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



90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
# File 'lib/hubspot/deal.rb', line 90

def dissociate!(deal_id, company_ids=[], vids=[])
  company_associations = company_ids.map do |id|
    { from_id: deal_id, to_id: id }
  end

  contact_associations = vids.map do |id|
    { from_id: deal_id, to_id: id }
  end

  results = []
  if company_associations.any?
    results << HubSpot::Association.batch_delete("Deal", "Company", company_associations)
  end
  if contact_associations.any?
    results << HubSpot::Association.batch_delete("Deal", "Contact", contact_associations)
  end

  results.all?
end

.find(deal_id) ⇒ Object



110
111
112
113
# File 'lib/hubspot/deal.rb', line 110

def find(deal_id)
  response = Hubspot::Connection.get_json(DEAL_PATH, { deal_id: deal_id })
  new(response)
end

.find_by_association(object) ⇒ Array

Find all deals associated to a contact or company

Parameters:

Returns:

  • (Array)

    Array of Hubspot::Deal records



156
157
158
159
160
161
162
163
# File 'lib/hubspot/deal.rb', line 156

def find_by_association(object)
  to_object_type = case object
               when Hubspot::Company then "Company"
               when Hubspot::Contact then "Contact"
               else raise(Hubspot::InvalidParams, 'Instance type not supported')
               end
  Hubspot::Association.all(to_object_type, object.id, "Deal")
end

.find_by_company(company) ⇒ Array

Parameters:

Returns:

  • (Array)

    Array of Hubspot::Deal records



141
142
143
# File 'lib/hubspot/deal.rb', line 141

def find_by_company(company)
  find_by_association company
end

.find_by_contact(contact) ⇒ Array

Parameters:

Returns:

  • (Array)

    Array of Hubspot::Deal records



149
150
151
# File 'lib/hubspot/deal.rb', line 149

def find_by_contact(contact)
  find_by_association contact
end

.recent(opts = {}) ⇒ Object

Parameters:

  • count (Integer)

    the amount of deals to return.

  • offset (Integer)

    pages back through recent contacts.



132
133
134
135
# File 'lib/hubspot/deal.rb', line 132

def recent(opts = {})
  response = Hubspot::Connection.get_json(RECENT_UPDATED_PATH, opts)
  response['results'].map { |d| new(d) }
end

.update(id, properties = {}) ⇒ boolean

Parameters:

  • deal_id (Integer)

    hubspot deal_id

  • params (Hash)

    hash of properties to update

Returns:

  • (boolean)

    success



45
46
47
48
49
# File 'lib/hubspot/deal.rb', line 45

def update(id, properties = {})
  update!(id, properties)
rescue Hubspot::RequestError => e
  false
end

.update!(id, properties = {}) ⇒ Hubspot::Deal

Parameters:

  • deal_id (Integer)

    hubspot deal_id

  • params (Hash)

    hash of properties to update

Returns:



56
57
58
59
60
# 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

Instance Method Details

#[](property) ⇒ Object



178
179
180
# File 'lib/hubspot/deal.rb', line 178

def [](property)
  @properties[property]
end

#destroy!TrueClass

Returns:

  • (TrueClass)

    true



169
170
171
172
# File 'lib/hubspot/deal.rb', line 169

def destroy!
  Hubspot::Connection.delete_json(DEAL_PATH, {deal_id: deal_id})
  @destroyed = true
end

#destroyed?Boolean

Returns:

  • (Boolean)


174
175
176
# File 'lib/hubspot/deal.rb', line 174

def destroyed?
  !!@destroyed
end

#update!(params) ⇒ Hubspot::Deal Also known as: update

Parameters:

  • params (Hash)

    hash of properties to update

Returns:



186
187
188
189
190
191
# File 'lib/hubspot/deal.rb', line 186

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