Skip to content

Commit b806d92

Browse files
authored
Merge branch 'master' into use_crm_associations_endpoints
2 parents 9f29322 + bc68c85 commit b806d92

23 files changed

+3503
-5
lines changed

lib/hubspot-ruby.rb

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
require 'hubspot/contact'
1414
require 'hubspot/contact_properties'
1515
require 'hubspot/contact_list'
16+
require 'hubspot/event'
1617
require 'hubspot/form'
1718
require 'hubspot/blog'
1819
require 'hubspot/topic'
@@ -25,6 +26,7 @@
2526
require 'hubspot/engagement'
2627
require 'hubspot/subscription'
2728
require 'hubspot/oauth'
29+
require 'hubspot/file'
2830

2931
module Hubspot
3032
def self.configure(config={})

lib/hubspot/connection.rb

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -142,4 +142,11 @@ def self.submit(path, opts)
142142
post(url, body: opts[:body], headers: { 'Content-Type' => 'application/x-www-form-urlencoded' })
143143
end
144144
end
145+
146+
class EventConnection < Connection
147+
def self.trigger(path, opts)
148+
url = generate_url(path, opts[:params], { base_url: 'https://track.hubspot.com', hapikey: false })
149+
get(url, body: opts[:body], headers: opts[:headers])
150+
end
151+
end
145152
end

lib/hubspot/contact.rb

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@ class Hubspot::Contact < Hubspot::Resource
1212
MERGE_PATH = '/contacts/v1/contact/merge-vids/:id/'
1313
SEARCH_PATH = '/contacts/v1/search/query'
1414
UPDATE_PATH = '/contacts/v1/contact/vid/:id/profile'
15+
UPDATE_PATH = '/contacts/v1/contact/vid/:id/profile'
16+
BATCH_UPDATE_PATH = '/contacts/v1/contact/batch'
1517

1618
class << self
1719
def all(opts = {})
@@ -70,6 +72,32 @@ def merge(primary, secondary)
7072

7173
true
7274
end
75+
76+
def batch_update(contacts, opts = {})
77+
request = contacts.map do |contact|
78+
# Use the specified options or update with the changes
79+
changes = opts.empty? ? contact.changes : opts
80+
81+
unless changes.empty?
82+
{
83+
"vid" => contact.id,
84+
"properties" => changes.map { |k, v| { "property" => k, "value" => v } }
85+
}
86+
end
87+
end
88+
89+
# Remove any objects without changes and return if there is nothing to update
90+
request.compact!
91+
return true if request.empty?
92+
93+
Hubspot::Connection.post_json(
94+
BATCH_UPDATE_PATH,
95+
params: {},
96+
body: request
97+
)
98+
99+
true
100+
end
73101
end
74102

75103
def name

lib/hubspot/deal.rb

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,28 @@ def create!(portal_id, company_ids, vids, params={})
3737
new(response)
3838
end
3939

40+
# Updates the properties of a deal
41+
# {http://developers.hubspot.com/docs/methods/deals/update_deal}
42+
# @param deal_id [Integer] hubspot deal_id
43+
# @param params [Hash] hash of properties to update
44+
# @return [boolean] success
45+
def update(id, properties = {})
46+
update!(id, properties)
47+
rescue Hubspot::RequestError => e
48+
false
49+
end
50+
51+
# Updates the properties of a deal
52+
# {http://developers.hubspot.com/docs/methods/deals/update_deal}
53+
# @param deal_id [Integer] hubspot deal_id
54+
# @param params [Hash] hash of properties to update
55+
# @return [Hubspot::Deal] Deal record
56+
def update!(id, properties = {})
57+
request = { properties: Hubspot::Utils.hash_to_properties(properties.stringify_keys, key_name: 'name') }
58+
response = Hubspot::Connection.put_json(UPDATE_DEAL_PATH, params: { deal_id: id, no_parse: true }, body: request)
59+
response.success?
60+
end
61+
4062
# Associate a deal with a contact or company
4163
# {http://developers.hubspot.com/docs/methods/deals/associate_deal}
4264
# Usage
@@ -128,10 +150,11 @@ def [](property)
128150
# @param params [Hash] hash of properties to update
129151
# @return [Hubspot::Deal] self
130152
def update!(params)
131-
query = {"properties" => Hubspot::Utils.hash_to_properties(params.stringify_keys!, key_name: 'name')}
153+
query = { 'properties' => Hubspot::Utils.hash_to_properties(params.stringify_keys!, key_name: 'name') }
132154
Hubspot::Connection.put_json(UPDATE_DEAL_PATH, params: { deal_id: deal_id }, body: query)
133155
@properties.merge!(params)
134156
self
135157
end
158+
alias_method :update, :update!
136159
end
137160
end

lib/hubspot/engagement.rb

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@ module Hubspot
77
# {http://developers.hubspot.com/docs/methods/engagements/create_engagement}
88
#
99
class Engagement
10+
ALL_ENGAGEMENTS_PATH = '/engagements/v1/engagements/paged'
11+
RECENT_ENGAGEMENT_PATH = '/engagements/v1/engagements/recent/modified'
1012
CREATE_ENGAGMEMENT_PATH = '/engagements/v1/engagements'
1113
ENGAGEMENT_PATH = '/engagements/v1/engagements/:engagement_id'
1214
ASSOCIATE_ENGAGEMENT_PATH = '/engagements/v1/engagements/:engagement_id/associations/:object_type/:object_vid'
@@ -46,6 +48,30 @@ def find(engagement_id)
4648
end
4749
end
4850

51+
def all(opts = {})
52+
path = ALL_ENGAGEMENTS_PATH
53+
54+
response = Hubspot::Connection.get_json(path, opts)
55+
56+
result = {}
57+
result['engagements'] = response['results'].map { |d| new(d) }
58+
result['offset'] = response['offset']
59+
result['hasMore'] = response['hasMore']
60+
return result
61+
end
62+
63+
def recent(opts = {})
64+
path = RECENT_ENGAGEMENT_PATH
65+
66+
response = Hubspot::Connection.get_json(path, opts)
67+
68+
result = {}
69+
result['engagements'] = response['results'].map { |d| new(d) }
70+
result['offset'] = response['offset']
71+
result['hasMore'] = response['hasMore']
72+
result
73+
end
74+
4975
def find_by_company(company_id)
5076
find_by_association company_id, 'COMPANY'
5177
end

lib/hubspot/event.rb

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
require 'hubspot/utils'
2+
3+
module Hubspot
4+
#
5+
# HubSpot Events HTTP API
6+
#
7+
# {https://developers.hubspot.com/docs/methods/enterprise_events/http_api}
8+
#
9+
class Event
10+
POST_EVENT_PATH = '/v1/event'
11+
12+
class << self
13+
def trigger(event_id, email, options = {})
14+
default_params = { _n: event_id, _a: Hubspot::Config.portal_id, email: email }
15+
options[:params] = default_params.merge(options[:params] || {})
16+
17+
Hubspot::EventConnection.trigger(POST_EVENT_PATH, options).success?
18+
end
19+
end
20+
end
21+
end

lib/hubspot/file.rb

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
require 'hubspot/utils'
2+
require 'base64'
3+
require 'pp'
4+
5+
module Hubspot
6+
#
7+
# HubSpot Files API
8+
#
9+
# {https://developers.hubspot.com/docs/methods/files/post_files}
10+
#
11+
class File
12+
GET_FILE_PATH = "/filemanager/api/v2/files/:file_id"
13+
DELETE_FILE_PATH = "/filemanager/api/v2/files/:file_id/full-delete"
14+
LIST_FILE_PATH = "/filemanager/api/v2/files"
15+
16+
attr_reader :id
17+
attr_reader :properties
18+
19+
def initialize(response_hash)
20+
@id = response_hash["id"]
21+
@properties = response_hash
22+
end
23+
24+
class << self
25+
def find_by_id(file_id)
26+
response = Hubspot::Connection.get_json(GET_FILE_PATH, { file_id: file_id })
27+
new(response)
28+
end
29+
end
30+
31+
# Permanently delete a file and all related data and thumbnails from file manager.
32+
# {https://developers.hubspot.com/docs/methods/files/hard_delete_file_and_associated_objects}
33+
def destroy!
34+
Hubspot::Connection.post_json(DELETE_FILE_PATH, params: {file_id: id})
35+
end
36+
37+
end
38+
end

lib/hubspot/owner.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ def all(include_inactive=false)
3636
end
3737

3838
def find(id, include_inactive=false)
39+
path = GET_OWNER_PATH
3940
response = Hubspot::Connection.get_json(path, owner_id: id,
4041
include_inactive: include_inactive)
4142
new(response)

0 commit comments

Comments
 (0)