Skip to content

Commit

Permalink
Merge branch 'master' into use_crm_associations_endpoints
Browse files Browse the repository at this point in the history
  • Loading branch information
fonji authored Dec 12, 2019
2 parents 9f29322 + bc68c85 commit b806d92
Show file tree
Hide file tree
Showing 23 changed files with 3,503 additions and 5 deletions.
2 changes: 2 additions & 0 deletions lib/hubspot-ruby.rb
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
require 'hubspot/contact'
require 'hubspot/contact_properties'
require 'hubspot/contact_list'
require 'hubspot/event'
require 'hubspot/form'
require 'hubspot/blog'
require 'hubspot/topic'
Expand All @@ -25,6 +26,7 @@
require 'hubspot/engagement'
require 'hubspot/subscription'
require 'hubspot/oauth'
require 'hubspot/file'

module Hubspot
def self.configure(config={})
Expand Down
7 changes: 7 additions & 0 deletions lib/hubspot/connection.rb
Original file line number Diff line number Diff line change
Expand Up @@ -142,4 +142,11 @@ def self.submit(path, opts)
post(url, body: opts[:body], headers: { 'Content-Type' => 'application/x-www-form-urlencoded' })
end
end

class EventConnection < Connection
def self.trigger(path, opts)
url = generate_url(path, opts[:params], { base_url: 'https://track.hubspot.com', hapikey: false })
get(url, body: opts[:body], headers: opts[:headers])
end
end
end
28 changes: 28 additions & 0 deletions lib/hubspot/contact.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@ class Hubspot::Contact < Hubspot::Resource
MERGE_PATH = '/contacts/v1/contact/merge-vids/:id/'
SEARCH_PATH = '/contacts/v1/search/query'
UPDATE_PATH = '/contacts/v1/contact/vid/:id/profile'
UPDATE_PATH = '/contacts/v1/contact/vid/:id/profile'
BATCH_UPDATE_PATH = '/contacts/v1/contact/batch'

class << self
def all(opts = {})
Expand Down Expand Up @@ -70,6 +72,32 @@ def merge(primary, secondary)

true
end

def batch_update(contacts, opts = {})
request = contacts.map do |contact|
# Use the specified options or update with the changes
changes = opts.empty? ? contact.changes : opts

unless changes.empty?
{
"vid" => contact.id,
"properties" => changes.map { |k, v| { "property" => k, "value" => v } }
}
end
end

# Remove any objects without changes and return if there is nothing to update
request.compact!
return true if request.empty?

Hubspot::Connection.post_json(
BATCH_UPDATE_PATH,
params: {},
body: request
)

true
end
end

def name
Expand Down
25 changes: 24 additions & 1 deletion lib/hubspot/deal.rb
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,28 @@ def create!(portal_id, company_ids, vids, params={})
new(response)
end

# Updates the properties of a deal
# {http://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
def update(id, properties = {})
update!(id, properties)
rescue Hubspot::RequestError => e
false
end

# Updates the properties of a deal
# {http://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
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

# Associate a deal with a contact or company
# {http://developers.hubspot.com/docs/methods/deals/associate_deal}
# Usage
Expand Down Expand Up @@ -128,10 +150,11 @@ def [](property)
# @param params [Hash] hash of properties to update
# @return [Hubspot::Deal] self
def update!(params)
query = {"properties" => Hubspot::Utils.hash_to_properties(params.stringify_keys!, key_name: 'name')}
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
alias_method :update, :update!
end
end
26 changes: 26 additions & 0 deletions lib/hubspot/engagement.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ module Hubspot
# {http://developers.hubspot.com/docs/methods/engagements/create_engagement}
#
class Engagement
ALL_ENGAGEMENTS_PATH = '/engagements/v1/engagements/paged'
RECENT_ENGAGEMENT_PATH = '/engagements/v1/engagements/recent/modified'
CREATE_ENGAGMEMENT_PATH = '/engagements/v1/engagements'
ENGAGEMENT_PATH = '/engagements/v1/engagements/:engagement_id'
ASSOCIATE_ENGAGEMENT_PATH = '/engagements/v1/engagements/:engagement_id/associations/:object_type/:object_vid'
Expand Down Expand Up @@ -46,6 +48,30 @@ def find(engagement_id)
end
end

def all(opts = {})
path = ALL_ENGAGEMENTS_PATH

response = Hubspot::Connection.get_json(path, opts)

result = {}
result['engagements'] = response['results'].map { |d| new(d) }
result['offset'] = response['offset']
result['hasMore'] = response['hasMore']
return result
end

def recent(opts = {})
path = RECENT_ENGAGEMENT_PATH

response = Hubspot::Connection.get_json(path, opts)

result = {}
result['engagements'] = response['results'].map { |d| new(d) }
result['offset'] = response['offset']
result['hasMore'] = response['hasMore']
result
end

def find_by_company(company_id)
find_by_association company_id, 'COMPANY'
end
Expand Down
21 changes: 21 additions & 0 deletions lib/hubspot/event.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
require 'hubspot/utils'

module Hubspot
#
# HubSpot Events HTTP API
#
# {https://developers.hubspot.com/docs/methods/enterprise_events/http_api}
#
class Event
POST_EVENT_PATH = '/v1/event'

class << self
def trigger(event_id, email, options = {})
default_params = { _n: event_id, _a: Hubspot::Config.portal_id, email: email }
options[:params] = default_params.merge(options[:params] || {})

Hubspot::EventConnection.trigger(POST_EVENT_PATH, options).success?
end
end
end
end
38 changes: 38 additions & 0 deletions lib/hubspot/file.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
require 'hubspot/utils'
require 'base64'
require 'pp'

module Hubspot
#
# HubSpot Files API
#
# {https://developers.hubspot.com/docs/methods/files/post_files}
#
class File
GET_FILE_PATH = "/filemanager/api/v2/files/:file_id"
DELETE_FILE_PATH = "/filemanager/api/v2/files/:file_id/full-delete"
LIST_FILE_PATH = "/filemanager/api/v2/files"

attr_reader :id
attr_reader :properties

def initialize(response_hash)
@id = response_hash["id"]
@properties = response_hash
end

class << self
def find_by_id(file_id)
response = Hubspot::Connection.get_json(GET_FILE_PATH, { file_id: file_id })
new(response)
end
end

# Permanently delete a file and all related data and thumbnails from file manager.
# {https://developers.hubspot.com/docs/methods/files/hard_delete_file_and_associated_objects}
def destroy!
Hubspot::Connection.post_json(DELETE_FILE_PATH, params: {file_id: id})
end

end
end
1 change: 1 addition & 0 deletions lib/hubspot/owner.rb
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ def all(include_inactive=false)
end

def find(id, include_inactive=false)
path = GET_OWNER_PATH
response = Hubspot::Connection.get_json(path, owner_id: id,
include_inactive: include_inactive)
new(response)
Expand Down
Loading

0 comments on commit b806d92

Please sign in to comment.