Skip to content
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
2 changes: 2 additions & 0 deletions lib/hubspot-ruby.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,15 @@
require 'hubspot/collection'
require 'hubspot/paged_collection'
require 'hubspot/properties'
require 'hubspot/campaign'
require 'hubspot/company'
require 'hubspot/company_properties'
require 'hubspot/config'
require 'hubspot/connection'
require 'hubspot/contact'
require 'hubspot/contact_properties'
require 'hubspot/contact_list'
require 'hubspot/email_event'
require 'hubspot/form'
require 'hubspot/blog'
require 'hubspot/topic'
Expand Down
55 changes: 55 additions & 0 deletions lib/hubspot/campaign.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
module Hubspot
class Campaign
CAMPAIGNS_PATH = "/email/public/v1/campaigns" #https://api.hubapi.com/email/public/v1/campaigns?hapikey=demo&limit=3
CAMPAIGNS_BY_ID_PATH = "/email/public/v1/campaigns/by-id" #example: https://api.hubapi.com/email/public/v1/campaigns/by-id?hapikey=demo&limit=3
CAMPAIGN_PATH = "/email/public/v1/campaigns/:campaign_id"

attr_reader :id, :name, :app_id, :app_name, :last_updated_time, :content_id, :counters, :num_included, :num_queued,
:sub_type, :subject, :type

def initialize(response_hash)
@id = response_hash["id"]
@name = response_hash["name"]
@app_id = response_hash["appId"]
@app_name = response_hash["appName"]
@content_id = response_hash["contentId"]
@counters = response_hash["counters"]
@num_included = response_hash["numIncluded"]
@num_queued = response_hash["numQueued"]
@sub_type = response_hash["subType"]
@subject = response_hash["subject"]
@type = response_hash["type"]
@last_updated_time = response["lastUpdatedTime"]
end

class << self
def all(opts = {})
Hubspot::PagedCollection.new(opts) do |options, offset, limit|
response = Hubspot::Connection.get_json(
CAMPAIGNS_PATH,
options.merge("limit" => limit, "offset" => offset)
)
campaigns = response["campaigns"].map { |result| new(result) }
[campaigns, response["offset"], response["hasMore"]]
end
end

def all_by_id(opts = {})
Hubspot::PagedCollection.new(opts) do |options, offset, limit|
response = Hubspot::Connection.get_json(
CAMPAIGNS_BY_ID_PATH,
options.merge("limit" => limit, "offset" => offset)
)
campaigns = response["campaigns"].map { |result| new(result) }
[campaigns, response["offset"], response["hasMore"]]
end
end

def find(id)
response = Hubspot::Connection.get_json(CAMPAIGN_PATH, { campaign_id: id })
new(response)
end

end
end
end
17 changes: 17 additions & 0 deletions lib/hubspot/company.rb
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ class Hubspot::Company < Hubspot::Resource
REMOVE_CONTACT_PATH = '/companies/v2/companies/:id/contacts/:contact_id'
SEARCH_DOMAIN_PATH = '/companies/v2/domains/:domain/companies'
UPDATE_PATH = '/companies/v2/companies/:id'
MERGE_PATH = '/crm/v3/objects/companies/merge'

class << self
def all(opts = {})
Expand Down Expand Up @@ -121,6 +122,18 @@ def batch_update(companies, opts = {})

true
end

def merge(merge_into_id, merged_id)
Hubspot::Connection.post_json(
MERGE_PATH,
params: {},
body: {
"primaryObjectId" => merge_into_id,
"objectIdToMerge" => merged_id
}
)
end

end

def contacts(opts = {})
Expand Down Expand Up @@ -157,4 +170,8 @@ def add_contact(contact)
def remove_contact(contact)
self.class.remove_contact(@id, contact.to_i)
end

def merge(company)
self.class.merge(@id, company.to_i)
end
end
6 changes: 5 additions & 1 deletion lib/hubspot/contact.rb
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,11 @@ def find_by_user_token(token)
alias_method :find_by_utk, :find_by_user_token

def create(email, properties = {})
super(properties.merge("email" => email))
if email
super(properties.merge("email" => email))
else
super(properties)
end
end

def create_or_update(email, properties = {})
Expand Down
47 changes: 47 additions & 0 deletions lib/hubspot/email_event.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
module Hubspot
class EmailEvent
EVENTS_PATH = "/email/public/v1/events"
EVENT_PATH = "/email/public/v1/events/:created/:id"

attr_reader :app_id, :app_name, :created, :email_campaign_id, :hmid, :id, :location, :portal_id, :recipient, :type,
:user_agent, :browser, :location, :filtered_event


def initialize(response_hash)
@app_id = response_hash["appId"]
@app_name = response_hash["appName"]
@created = response_hash["created"]
@email_campaign_id = response_hash["emailCampaignId"]
@hmid = response_hash["hmid"]
@id = response_hash["id"]
@location = response_hash["location"]
@portal_id = response_hash["portalId"]
@recipient = response_hash["recipient"]
@type = response_hash["type"]

# User engagement properties
@user_agent = response_hash["userAgent"]
@browser = response_hash["browser"]
@location = response_hash["location"]
@filtered_event = response_hash["filteredEvent"]
end

class << self
def all(opts = {})
Hubspot::PagedCollection.new(opts) do |options, offset, limit|
response = Hubspot::Connection.get_json(
EVENTS_PATH,
options.merge("limit" => limit, "offset" => offset)
)
events = response["events"].map { |result| new(result) }
[events, response["offset"], response["hasMore"]]
end
end

def find(created, id)
response = Hubspot::Connection.get_json(EVENT_PATH, { created: created, id: id })
new(response)
end
end
end
end
12 changes: 5 additions & 7 deletions lib/hubspot/owner.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,8 @@ module Hubspot
# TODO: Update an Owner
# TODO: Delete an Owner
class Owner
GET_OWNER_PATH = '/owners/v2/owners/:owner_id' # GET
GET_OWNERS_PATH = '/owners/v2/owners' # GET
CREATE_OWNER_PATH = '/owners/v2/owners' # POST
UPDATE_OWNER_PATH = '/owners/v2/owners/:owner_id' # PUT
DELETE_OWNER_PATH = '/owners/v2/owners/:owner_id' # DELETE
GET_OWNER_PATH = '/crm/v3/owners/:owner_id' # GET
GET_OWNERS_PATH = '/crm/v3/owners' # GET


attr_reader :properties, :owner_id, :email
Expand All @@ -36,14 +33,15 @@ 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)
archived: include_inactive)
new(response)
end

def find_by_email(email, include_inactive=false)
path = GET_OWNERS_PATH
params = { email: email, includeInactive: include_inactive }
params = { email: email, archived: include_inactive }
response = Hubspot::Connection.get_json(path, params)
response.blank? ? nil : new(response.first)
end
Expand Down
107 changes: 107 additions & 0 deletions spec/lib/hubspot/event_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,107 @@
describe Hubspot::Event do
let(:portal_id) { 62515 }
let(:company_id) { 8954037 }
let(:vid) { 27136 }

let(:example_deal_hash) do
VCR.use_cassette("event_example") do
HTTParty.get("https://api.hubapi.com/events/v1/event/3?hapikey=demo&portalId=#{portal_id}").parsed_response
end
end

before{ Hubspot.configure(hapikey: "demo") }

describe "#initialize" do
subject{ Hubspot::Deal.new(example_deal_hash) }
it { should be_an_instance_of Hubspot::Deal }
its (:portal_id) { should == portal_id }
its (:deal_id) { should == 3 }
end

describe ".create!" do
cassette "deal_create"
subject { Hubspot::Deal.create!(portal_id, [company_id], [vid], {}) }
its(:deal_id) { should_not be_nil }
its(:portal_id) { should eql portal_id }
its(:company_ids) { should eql [company_id]}
its(:vids) { should eql [vid]}
end

describe ".find" do
cassette "deal_find"
let(:deal) {Hubspot::Deal.create!(portal_id, [company_id], [vid], { amount: amount})}

it 'must find by the deal id' do
find_deal = Hubspot::Deal.find(deal.deal_id)
find_deal.deal_id.should eql deal.deal_id
find_deal.properties["amount"].should eql amount
end
end

describe '.find_by_company' do
cassette 'deal_find_by_company'
let(:company) { Hubspot::Company.create(name: 'Test Company') }
let(:deal) { Hubspot::Deal.create!(portal_id, [company.id], [vid], { amount: amount }) }

it 'returns company deals' do
deals = Hubspot::Deal.find_by_company(company)
deals.first.deal_id.should eql deal.deal_id
deals.first.properties['amount'].should eql amount
end
end

describe '.recent' do
cassette 'find_all_recent_updated_deals'

it 'must get the recents updated deals' do
deals = Hubspot::Deal.recent

first = deals.first
last = deals.last

expect(first).to be_a Hubspot::Deal
expect(first.properties['amount']).to eql '0'
expect(first.properties['dealname']).to eql '1420787916-gou2rzdgjzx2@u2rzdgjzx2.com'
expect(first.properties['dealstage']).to eql 'closedwon'

expect(last).to be_a Hubspot::Deal
expect(last.properties['amount']).to eql '250'
expect(last.properties['dealname']).to eql '1420511993-U9862RD9XR@U9862RD9XR.com'
expect(last.properties['dealstage']).to eql 'closedwon'
end

it 'must filter only 2 deals' do
deals = Hubspot::Deal.recent(count: 2)
expect(deals.size).to eql 2
end

it 'it must offset the deals' do
deal = Hubspot::Deal.recent(count: 1, offset: 1).first
expect(deal.properties['dealname']).to eql '1420704406-goy6v83a97nr@y6v83a97nr.com' # the third deal
end
end

describe "#destroy!" do
it "should remove from hubspot" do
VCR.use_cassette("destroy_deal") do
deal = Hubspot::Deal.create!(portal_id, [company_id], [vid], {amount: amount})

result = deal.destroy!

assert_requested :delete, hubspot_api_url("/deals/v1/deal/#{deal.deal_id}?hapikey=demo")

expect(result).to be true
end
end
end

describe '#[]' do
subject{ Hubspot::Deal.new(example_deal_hash) }

it 'should get a property' do
subject.properties.each do |property, value|
expect(subject[property]).to eql value
end
end
end
end