diff --git a/lib/hubspot-ruby.rb b/lib/hubspot-ruby.rb index ad2a4fd6..6cf4393c 100644 --- a/lib/hubspot-ruby.rb +++ b/lib/hubspot-ruby.rb @@ -6,6 +6,7 @@ require 'hubspot/collection' require 'hubspot/paged_collection' require 'hubspot/properties' +require 'hubspot/campaign' require 'hubspot/company' require 'hubspot/company_properties' require 'hubspot/config' @@ -13,6 +14,7 @@ require 'hubspot/contact' require 'hubspot/contact_properties' require 'hubspot/contact_list' +require 'hubspot/email_event' require 'hubspot/form' require 'hubspot/blog' require 'hubspot/topic' diff --git a/lib/hubspot/campaign.rb b/lib/hubspot/campaign.rb new file mode 100644 index 00000000..19dc5ac7 --- /dev/null +++ b/lib/hubspot/campaign.rb @@ -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 \ No newline at end of file diff --git a/lib/hubspot/company.rb b/lib/hubspot/company.rb index 8f6e2d77..cb37dc22 100644 --- a/lib/hubspot/company.rb +++ b/lib/hubspot/company.rb @@ -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 = {}) @@ -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 = {}) @@ -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 diff --git a/lib/hubspot/contact.rb b/lib/hubspot/contact.rb index 7101c048..b005a8bb 100644 --- a/lib/hubspot/contact.rb +++ b/lib/hubspot/contact.rb @@ -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 = {}) diff --git a/lib/hubspot/email_event.rb b/lib/hubspot/email_event.rb new file mode 100644 index 00000000..775fbeca --- /dev/null +++ b/lib/hubspot/email_event.rb @@ -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 \ No newline at end of file diff --git a/lib/hubspot/owner.rb b/lib/hubspot/owner.rb index 2ddaedc8..18778148 100644 --- a/lib/hubspot/owner.rb +++ b/lib/hubspot/owner.rb @@ -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) diff --git a/spec/lib/hubspot/event_spec.rb b/spec/lib/hubspot/event_spec.rb new file mode 100644 index 00000000..8aa14858 --- /dev/null +++ b/spec/lib/hubspot/event_spec.rb @@ -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