Class: Hubspot::Deal
- Inherits:
-
Object
- Object
- Hubspot::Deal
- Defined in:
- lib/hubspot/deal.rb
Overview
HubSpot Deals API
http://developers.hubspot.com/docs/methods/deals/deals_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'
- ASSOCIATE_DEAL_PATH =
'/deals/v1/deal/:deal_id/associations/:OBJECTTYPE?id=:objectId'
- ASSOCIATED_DEAL_PATH =
"/deals/v1/deal/associated/:objectType/:objectId"
Instance Attribute Summary collapse
-
#company_ids ⇒ Object
readonly
Returns the value of attribute company_ids.
-
#deal_id ⇒ Object
readonly
Returns the value of attribute deal_id.
-
#portal_id ⇒ Object
readonly
Returns the value of attribute portal_id.
-
#properties ⇒ Object
readonly
Returns the value of attribute properties.
-
#vids ⇒ Object
readonly
Returns the value of attribute vids.
Class Method Summary collapse
- .all(opts = {}) ⇒ Object
-
.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, [], [52]).
- .create!(portal_id, company_ids, vids, params = {}) ⇒ Object
- .find(deal_id) ⇒ Object
-
.find_by_association(object) ⇒ Array
Find all deals associated to a contact or company http://developers.hubspot.com/docs/methods/deals/get-associated-deals.
-
.find_by_company(company) ⇒ Array
Find all deals associated to a company http://developers.hubspot.com/docs/methods/deals/get-associated-deals.
-
.find_by_contact(contact) ⇒ Array
Find all deals associated to a contact http://developers.hubspot.com/docs/methods/deals/get-associated-deals.
-
.recent(opts = {}) ⇒ Object
Find recent updated deals.
Instance Method Summary collapse
- #[](property) ⇒ Object
-
#destroy! ⇒ TrueClass
Archives the contact in hubspot https://developers.hubspot.com/docs/methods/contacts/delete_contact.
- #destroyed? ⇒ Boolean
-
#initialize(response_hash) ⇒ Deal
constructor
A new instance of Deal.
-
#update!(params) ⇒ Hubspot::Deal
Updates the properties of a deal https://developers.hubspot.com/docs/methods/deals/update_deal.
Constructor Details
#initialize(response_hash) ⇒ Deal
Returns a new instance of Deal.
24 25 26 27 28 29 30 31 |
# File 'lib/hubspot/deal.rb', line 24 def initialize(response_hash) props = response_hash['properties'] || {} @properties = Hubspot::Utils.properties_to_hash(props) @portal_id = response_hash["portalId"] @deal_id = response_hash["dealId"] @company_ids = response_hash["associations"]["associatedCompanyIds"] @vids = response_hash["associations"]["associatedVids"] end |
Instance Attribute Details
#company_ids ⇒ Object (readonly)
Returns the value of attribute company_ids.
21 22 23 |
# File 'lib/hubspot/deal.rb', line 21 def company_ids @company_ids end |
#deal_id ⇒ Object (readonly)
Returns the value of attribute deal_id.
20 21 22 |
# File 'lib/hubspot/deal.rb', line 20 def deal_id @deal_id end |
#portal_id ⇒ Object (readonly)
Returns the value of attribute portal_id.
19 20 21 |
# File 'lib/hubspot/deal.rb', line 19 def portal_id @portal_id end |
#properties ⇒ Object (readonly)
Returns the value of attribute properties.
18 19 20 |
# File 'lib/hubspot/deal.rb', line 18 def properties @properties end |
#vids ⇒ Object (readonly)
Returns the value of attribute vids.
22 23 24 |
# File 'lib/hubspot/deal.rb', line 22 def vids @vids end |
Class Method Details
.all(opts = {}) ⇒ Object
59 60 61 62 63 64 65 66 67 68 69 70 |
# File 'lib/hubspot/deal.rb', line 59 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, [], [52])
47 48 49 50 51 |
# File 'lib/hubspot/deal.rb', line 47 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 |
.create!(portal_id, company_ids, vids, params = {}) ⇒ Object
34 35 36 37 38 39 40 41 |
# File 'lib/hubspot/deal.rb', line 34 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 |
.find(deal_id) ⇒ Object
54 55 56 57 |
# File 'lib/hubspot/deal.rb', line 54 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 http://developers.hubspot.com/docs/methods/deals/get-associated-deals
101 102 103 104 105 106 107 108 109 110 111 112 |
# File 'lib/hubspot/deal.rb', line 101 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.vid } response = Hubspot::Connection.get_json(path, params) response["results"].map { |deal_id| find(deal_id) } end |
.find_by_company(company) ⇒ Array
Find all deals associated to a company http://developers.hubspot.com/docs/methods/deals/get-associated-deals
85 86 87 |
# File 'lib/hubspot/deal.rb', line 85 def find_by_company(company) find_by_association company end |
.find_by_contact(contact) ⇒ Array
Find all deals associated to a contact http://developers.hubspot.com/docs/methods/deals/get-associated-deals
93 94 95 |
# File 'lib/hubspot/deal.rb', line 93 def find_by_contact(contact) find_by_association contact end |
.recent(opts = {}) ⇒ Object
Find recent updated deals. http://developers.hubspot.com/docs/methods/deals/get_deals_modified
76 77 78 79 |
# File 'lib/hubspot/deal.rb', line 76 def recent(opts = {}) response = Hubspot::Connection.get_json(RECENT_UPDATED_PATH, opts) response['results'].map { |d| new(d) } end |
Instance Method Details
#[](property) ⇒ Object
127 128 129 |
# File 'lib/hubspot/deal.rb', line 127 def [](property) @properties[property] end |
#destroy! ⇒ TrueClass
Archives the contact in hubspot https://developers.hubspot.com/docs/methods/contacts/delete_contact
118 119 120 121 |
# File 'lib/hubspot/deal.rb', line 118 def destroy! Hubspot::Connection.delete_json(DEAL_PATH, {deal_id: deal_id}) @destroyed = true end |
#destroyed? ⇒ Boolean
123 124 125 |
# File 'lib/hubspot/deal.rb', line 123 def destroyed? !!@destroyed end |
#update!(params) ⇒ Hubspot::Deal
Updates the properties of a deal https://developers.hubspot.com/docs/methods/deals/update_deal
135 136 137 138 139 140 |
# File 'lib/hubspot/deal.rb', line 135 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 |