Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Events support #182

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions lib/hubspot-ruby.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,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 Down
11 changes: 9 additions & 2 deletions lib/hubspot/connection.rb
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ def generate_url(path, params={}, options={})
options[:hapikey] = false
else
Hubspot::Config.ensure! :hapikey
end
end
path = path.clone
params = params.clone
base_url = options[:base_url] || Hubspot::Config.base_url
Expand All @@ -75,7 +75,7 @@ def generate_url(path, params={}, options={})
if path =~ /:portal_id/
Hubspot::Config.ensure! :portal_id
params["portal_id"] = Hubspot::Config.portal_id if path =~ /:portal_id/
end
end

params.each do |k,v|
if path.match(":#{k}")
Expand Down Expand Up @@ -121,4 +121,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
39 changes: 39 additions & 0 deletions lib/hubspot/event.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
require 'hubspot/utils'

module Hubspot
#
# HubSpot Events HTTP API
#
# {https://developers.hubspot.com/docs/methods/enterprise_events/http_api}
#
class Event
GET_EVENTS_PATH = '/reports/v2/events'
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

def all(opts = {})
response = Hubspot::Connection.get_json(GET_EVENTS_PATH, opts)
response.map { |c| new(c) }
end
end

def initialize(response_hash)
@properties = response_hash
@name = response_hash['name']
@id = response_hash['id']
@status = response_hash['status']
end

attr_reader :properties,
:name,
:id,
:status
end
end
25 changes: 25 additions & 0 deletions spec/lib/hubspot/connection_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -177,6 +177,31 @@
end
end

describe Hubspot::EventConnection do
describe '.complete' do
let(:path) { '/path' }
let(:options) { { params: {} } }

subject { described_class.complete(path, options) }
before { Hubspot.configure(hapikey: 'demo', portal_id: '62515') }

it 'calls get with a custom url' do
mock(described_class).get('https://track.hubspot.com/path', body: nil, headers: nil) { true }
subject
end

context 'with more options' do
let(:headers) { { 'User-Agent' => 'something' } }
let(:options) { { params: {}, headers: headers } }

it 'supports headers' do
mock(described_class).get('https://track.hubspot.com/path', body: nil, headers: headers) { true }
subject
end
end
end
end

def stub_logger
instance_double(Logger, info: true).tap do |logger|
allow(Hubspot::Config).to receive(:logger).and_return(logger)
Expand Down
49 changes: 49 additions & 0 deletions spec/lib/hubspot/event_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
describe Hubspot::Event do
let(:portal_id) { '62515' }
let(:sent_portal_id) { portal_id }
before { Hubspot.configure(hapikey: 'fake') }

describe '.trigger' do
let(:event_id) { '000000001625' }
let(:email) { '[email protected]' }
let(:options) { {} }
let(:base_url) { 'https://track.hubspot.com' }
let(:url) { "#{base_url}/v1/event?_n=#{event_id}&_a=#{sent_portal_id}&email=#{CGI.escape email}" }
subject { described_class.trigger(event_id, email, options) }

it 'sends a request to trigger the event' do
stub_request(:get, url)
.to_return(status: 200)

expect(subject).to be true
end

context 'with more options' do
context 'with headers' do
let(:headers) { Hash['User-Agent' => 'something'] }
let(:options) { Hash[headers: headers] }

it 'sends headers' do
stub_request(:get, url)
.with(headers: headers)
.to_return(status: 200)

expect(subject).to be true
end
end

context 'when overriding portal_id' do
let(:sent_portal_id) { '123' }
let(:options) { { params: { _a: sent_portal_id } } }

it 'sends a request to the portal_id in the options' do
stub_request(:get, url)
.with(headers: nil)
.to_return(status: 200)

expect(subject).to be true
end
end
end
end
end