hubspot-api-ruby 0.8.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (73) hide show
  1. checksums.yaml +7 -0
  2. data/.rspec +3 -0
  3. data/Gemfile +3 -0
  4. data/Guardfile +9 -0
  5. data/LICENSE.txt +18 -0
  6. data/README.md +295 -0
  7. data/RELEASING.md +6 -0
  8. data/Rakefile +32 -0
  9. data/hubspot-api-ruby.gemspec +42 -0
  10. data/lib/hubspot-api-ruby.rb +39 -0
  11. data/lib/hubspot/blog.rb +98 -0
  12. data/lib/hubspot/collection.rb +41 -0
  13. data/lib/hubspot/company.rb +160 -0
  14. data/lib/hubspot/company_properties.rb +59 -0
  15. data/lib/hubspot/config.rb +63 -0
  16. data/lib/hubspot/connection.rb +152 -0
  17. data/lib/hubspot/contact.rb +110 -0
  18. data/lib/hubspot/contact_list.rb +129 -0
  19. data/lib/hubspot/contact_properties.rb +59 -0
  20. data/lib/hubspot/deal.rb +173 -0
  21. data/lib/hubspot/deal_pipeline.rb +58 -0
  22. data/lib/hubspot/deal_properties.rb +59 -0
  23. data/lib/hubspot/deprecator.rb +7 -0
  24. data/lib/hubspot/engagement.rb +222 -0
  25. data/lib/hubspot/event.rb +21 -0
  26. data/lib/hubspot/exceptions.rb +18 -0
  27. data/lib/hubspot/file.rb +38 -0
  28. data/lib/hubspot/form.rb +95 -0
  29. data/lib/hubspot/oauth.rb +50 -0
  30. data/lib/hubspot/owner.rb +57 -0
  31. data/lib/hubspot/paged_collection.rb +35 -0
  32. data/lib/hubspot/properties.rb +123 -0
  33. data/lib/hubspot/railtie.rb +10 -0
  34. data/lib/hubspot/resource.rb +270 -0
  35. data/lib/hubspot/subscription.rb +37 -0
  36. data/lib/hubspot/topic.rb +37 -0
  37. data/lib/hubspot/utils.rb +127 -0
  38. data/lib/tasks/hubspot.rake +53 -0
  39. data/spec/factories/companies.rb +9 -0
  40. data/spec/factories/contacts.rb +10 -0
  41. data/spec/lib/hubspot-ruby_spec.rb +12 -0
  42. data/spec/lib/hubspot/blog_spec.rb +150 -0
  43. data/spec/lib/hubspot/company_properties_spec.rb +410 -0
  44. data/spec/lib/hubspot/company_spec.rb +340 -0
  45. data/spec/lib/hubspot/config_spec.rb +87 -0
  46. data/spec/lib/hubspot/connection_spec.rb +214 -0
  47. data/spec/lib/hubspot/contact_list_spec.rb +301 -0
  48. data/spec/lib/hubspot/contact_properties_spec.rb +245 -0
  49. data/spec/lib/hubspot/contact_spec.rb +223 -0
  50. data/spec/lib/hubspot/deal_pipeline_spec.rb +85 -0
  51. data/spec/lib/hubspot/deal_properties_spec.rb +262 -0
  52. data/spec/lib/hubspot/deal_spec.rb +185 -0
  53. data/spec/lib/hubspot/deprecator_spec.rb +15 -0
  54. data/spec/lib/hubspot/engagement_spec.rb +177 -0
  55. data/spec/lib/hubspot/event_spec.rb +33 -0
  56. data/spec/lib/hubspot/file_spec.rb +38 -0
  57. data/spec/lib/hubspot/form_spec.rb +189 -0
  58. data/spec/lib/hubspot/owner_spec.rb +56 -0
  59. data/spec/lib/hubspot/properties_spec.rb +45 -0
  60. data/spec/lib/hubspot/resource_spec.rb +54 -0
  61. data/spec/lib/hubspot/topic_spec.rb +23 -0
  62. data/spec/lib/hubspot/utils_spec.rb +164 -0
  63. data/spec/lib/tasks/hubspot_spec.rb +119 -0
  64. data/spec/shared_examples/saveable_resource.rb +45 -0
  65. data/spec/shared_examples/updateable_resource.rb +87 -0
  66. data/spec/spec_helper.rb +44 -0
  67. data/spec/support/capture_output.rb +21 -0
  68. data/spec/support/cassette_helper.rb +19 -0
  69. data/spec/support/hubspot_api_helpers.rb +13 -0
  70. data/spec/support/rake.rb +46 -0
  71. data/spec/support/tests_helper.rb +17 -0
  72. data/spec/support/vcr.rb +16 -0
  73. metadata +369 -0
@@ -0,0 +1,185 @@
1
+ describe Hubspot::Deal do
2
+ let(:portal_id) { 62515 }
3
+ let(:company_id) { 8954037 }
4
+ let(:vid) { 27136 }
5
+ let(:amount) { '30' }
6
+ let(:deal) { Hubspot::Deal.create!(portal_id, [company_id], [vid], { amount: amount}) }
7
+
8
+ let(:example_deal_hash) do
9
+ VCR.use_cassette("deal_example") do
10
+ HTTParty.get("https://api.hubapi.com/deals/v1/deal/3?hapikey=demo&portalId=#{portal_id}").parsed_response
11
+ end
12
+ end
13
+
14
+ before{ Hubspot.configure(hapikey: "demo") }
15
+
16
+ describe "#initialize" do
17
+ subject{ Hubspot::Deal.new(example_deal_hash) }
18
+ it { should be_an_instance_of Hubspot::Deal }
19
+ its (:portal_id) { should == portal_id }
20
+ its (:deal_id) { should == 3 }
21
+ end
22
+
23
+ describe ".create!" do
24
+ cassette "deal_create"
25
+ subject { Hubspot::Deal.create!(portal_id, [company_id], [vid], {}) }
26
+ its(:deal_id) { should_not be_nil }
27
+ its(:portal_id) { should eql portal_id }
28
+ its(:company_ids) { should eql [company_id]}
29
+ its(:vids) { should eql [vid]}
30
+ end
31
+
32
+ describe '.update' do
33
+ let(:changed_properties) { { dealname: 'super deal' } }
34
+
35
+ context 'with an existing resource' do
36
+ cassette
37
+ subject { described_class.update(deal.deal_id, changed_properties) }
38
+
39
+ it 'updates' do
40
+ expect(subject).to be_truthy
41
+ find_deal = Hubspot::Deal.find(deal.deal_id)
42
+ expect(find_deal['dealname']).to eq 'super deal'
43
+ end
44
+ end
45
+
46
+ context 'with an invalid resource' do
47
+ cassette
48
+ subject { described_class.update(0, changed_properties) }
49
+
50
+ it { is_expected.to be false }
51
+ end
52
+ end
53
+
54
+ describe '.update!' do
55
+ let(:changed_properties) { { dealname: 'super deal' } }
56
+
57
+ context 'with an existing resource' do
58
+ cassette
59
+ subject { described_class.update!(deal.deal_id, changed_properties) }
60
+
61
+ it 'updates' do
62
+ expect(subject).to be_truthy
63
+ find_deal = Hubspot::Deal.find(deal.deal_id)
64
+ expect(find_deal['dealname']).to eq 'super deal'
65
+ end
66
+ end
67
+
68
+ context 'with an invalid resource' do
69
+ cassette
70
+ subject { described_class.update!(0, changed_properties) }
71
+
72
+ it 'fails with an error' do
73
+ expect { subject }.to raise_error Hubspot::RequestError
74
+ end
75
+ end
76
+ end
77
+
78
+ describe '#update' do
79
+ let(:changed_properties) { { dealname: 'super deal' }.stringify_keys }
80
+
81
+ context 'without overlapping changes' do
82
+ cassette
83
+ subject { deal.update(changed_properties) }
84
+
85
+ it 'updates the properties' do
86
+ expect(subject).to be_truthy
87
+ changed_properties.each do |property, value|
88
+ expect(deal[property]).to eq value
89
+ end
90
+ end
91
+ end
92
+
93
+ context 'with overlapping changes' do
94
+ cassette
95
+ subject { deal.update(changed_properties) }
96
+ let(:overlapping_properties) { { dealname: 'old deal', amount: 6 }.stringify_keys }
97
+
98
+ before(:each) do
99
+ overlapping_properties.each { |property, value| deal.properties[property] = value }
100
+ end
101
+
102
+ it 'merges and updates the properties' do
103
+ expect(subject).to be_truthy
104
+ overlapping_properties.merge(changed_properties).each do |property, value|
105
+ expect(deal[property]).to eq value
106
+ end
107
+ end
108
+ end
109
+ end
110
+
111
+ describe ".find" do
112
+ cassette "deal_find"
113
+
114
+ it 'must find by the deal id' do
115
+ find_deal = Hubspot::Deal.find(deal.deal_id)
116
+ find_deal.deal_id.should eql deal.deal_id
117
+ find_deal.properties["amount"].should eql amount
118
+ end
119
+ end
120
+
121
+ describe '.find_by_company' do
122
+ cassette 'deal_find_by_company'
123
+ let(:company) { Hubspot::Company.create(name: 'Test Company') }
124
+ let(:company_id) { company.id }
125
+
126
+ it 'returns company deals' do
127
+ deals = Hubspot::Deal.find_by_company(company)
128
+ deals.first.deal_id.should eql deal.deal_id
129
+ deals.first.properties['amount'].should eql amount
130
+ end
131
+ end
132
+
133
+ describe '.recent' do
134
+ cassette 'find_all_recent_updated_deals'
135
+
136
+ it 'must get the recents updated deals' do
137
+ deals = Hubspot::Deal.recent
138
+
139
+ first = deals.first
140
+ last = deals.last
141
+
142
+ expect(first).to be_a Hubspot::Deal
143
+ expect(first.properties['amount']).to eql '0'
144
+ expect(first.properties['dealname']).to eql '1420787916-gou2rzdgjzx2@u2rzdgjzx2.com'
145
+ expect(first.properties['dealstage']).to eql 'closedwon'
146
+
147
+ expect(last).to be_a Hubspot::Deal
148
+ expect(last.properties['amount']).to eql '250'
149
+ expect(last.properties['dealname']).to eql '1420511993-U9862RD9XR@U9862RD9XR.com'
150
+ expect(last.properties['dealstage']).to eql 'closedwon'
151
+ end
152
+
153
+ it 'must filter only 2 deals' do
154
+ deals = Hubspot::Deal.recent(count: 2)
155
+ expect(deals.size).to eql 2
156
+ end
157
+
158
+ it 'it must offset the deals' do
159
+ deal = Hubspot::Deal.recent(count: 1, offset: 1).first
160
+ expect(deal.properties['dealname']).to eql '1420704406-goy6v83a97nr@y6v83a97nr.com' # the third deal
161
+ end
162
+ end
163
+
164
+ describe "#destroy!" do
165
+ it "should remove from hubspot" do
166
+ VCR.use_cassette("destroy_deal") do
167
+ result = deal.destroy!
168
+
169
+ assert_requested :delete, hubspot_api_url("/deals/v1/deal/#{deal.deal_id}?hapikey=demo")
170
+
171
+ expect(result).to be true
172
+ end
173
+ end
174
+ end
175
+
176
+ describe '#[]' do
177
+ subject{ Hubspot::Deal.new(example_deal_hash) }
178
+
179
+ it 'should get a property' do
180
+ subject.properties.each do |property, value|
181
+ expect(subject[property]).to eql value
182
+ end
183
+ end
184
+ end
185
+ end
@@ -0,0 +1,15 @@
1
+ RSpec.describe Hubspot::Deprecator do
2
+ describe ".build" do
3
+ it "returns an instance of ActiveSupport::Deprecation" do
4
+ deprecator = Hubspot::Deprecator.build
5
+
6
+ expect(deprecator).to be_an_instance_of(ActiveSupport::Deprecation)
7
+ end
8
+
9
+ it "uses the correct gem name" do
10
+ deprecator = Hubspot::Deprecator.build
11
+
12
+ expect(deprecator.gem_name).to eq("hubspot-api-ruby")
13
+ end
14
+ end
15
+ end
@@ -0,0 +1,177 @@
1
+ describe Hubspot::Engagement do
2
+ let(:example_engagement_hash) do
3
+ VCR.use_cassette("engagement_example") do
4
+ HTTParty.get("https://api.hubapi.com/engagements/v1/engagements/51484873?hapikey=demo").parsed_response
5
+ end
6
+ end
7
+ let(:example_associated_engagement_hash) do
8
+ VCR.use_cassette("engagement_associated_example") do
9
+ HTTParty.get("https://api.hubapi.com/engagements/v1/engagements/58699206?hapikey=demo").parsed_response
10
+ end
11
+ end
12
+
13
+ # http://developers.hubspot.com/docs/methods/contacts/get_contact
14
+ before{ Hubspot.configure(hapikey: "demo") }
15
+
16
+ describe "#initialize" do
17
+ subject{ Hubspot::Engagement.new(example_engagement_hash) }
18
+ it { should be_an_instance_of Hubspot::Engagement }
19
+ its (:id) { should == 51484873 }
20
+ end
21
+
22
+ describe 'EngagementNote' do
23
+ describe ".create!" do
24
+ cassette "engagement_create"
25
+ body = "Test note"
26
+ subject { Hubspot::EngagementNote.create!(nil, body) }
27
+ its(:id) { should_not be_nil }
28
+ its(:body) { should eql body }
29
+ end
30
+
31
+ describe ".find" do
32
+ cassette "engagement_find"
33
+ let(:engagement) {Hubspot::EngagementNote.new(example_engagement_hash)}
34
+
35
+ it 'must find by the engagement id' do
36
+ find_engagement = Hubspot::EngagementNote.find(engagement.id)
37
+ find_engagement.id.should eql engagement.id
38
+ find_engagement.body.should eql engagement.body
39
+ end
40
+ end
41
+
42
+ describe ".find_by_company" do
43
+ cassette "engagement_find_by_country"
44
+ let(:engagement) {Hubspot::EngagementNote.new(example_associated_engagement_hash)}
45
+
46
+ it 'must find by company id' do
47
+ find_engagements = Hubspot::EngagementNote.find_by_company(engagement.associations["companyIds"].first)
48
+ find_engagements.should_not be_nil
49
+ find_engagements.any?{|engagement| engagement.id == engagement.id and engagement.body == engagement.body}.should be true
50
+ end
51
+ end
52
+
53
+ describe ".find_by_contact" do
54
+ cassette "engagement_find_by_contact"
55
+ let(:engagement) {Hubspot::EngagementNote.new(example_associated_engagement_hash)}
56
+
57
+ it 'must find by contact id' do
58
+ find_engagements = Hubspot::EngagementNote.find_by_contact(engagement.associations["contactIds"].first)
59
+ find_engagements.should_not be_nil
60
+ find_engagements.any?{|engagement| engagement.id == engagement.id and engagement.body == engagement.body}.should be true
61
+ end
62
+ end
63
+
64
+ describe ".find_by_association" do
65
+ cassette "engagement_find_by_association"
66
+
67
+ it 'must raise for fake association type' do
68
+ expect {
69
+ Hubspot::EngagementNote.find_by_association(1, 'FAKE_TYPE')
70
+ }.to raise_error
71
+ end
72
+ end
73
+
74
+ describe ".all" do
75
+ cassette "find_all_engagements"
76
+
77
+ it 'must get the engagements list' do
78
+ engagements = Hubspot::Engagement.all
79
+
80
+ expect(engagements['engagements'].size).to eql 100 # default page size
81
+
82
+ first = engagements['engagements'].first
83
+ last = engagements['engagements'].last
84
+
85
+ expect(first).to be_a Hubspot::Engagement
86
+ expect(first.engagement['id']).to eql 3981023
87
+
88
+ expect(last).to be_a Hubspot::Engagement
89
+ expect(last.engagement['id']).to eql 36579065
90
+ end
91
+
92
+ it 'must filter only 2 engagements' do
93
+ engagements = Hubspot::Engagement.all(limit: 2)
94
+ expect(engagements['engagements'].size).to eql 2
95
+ end
96
+
97
+ it 'it must offset the engagements' do
98
+ single_list = Hubspot::Engagement.all(limit: 5)
99
+ expect(single_list['engagements'].size).to eql 5
100
+
101
+ second = Hubspot::Engagement.all(count: 1, offset: single_list['offset'])['engagements'].first
102
+ expect(second.engagement['id']).to eql 4815722
103
+ end
104
+ end
105
+
106
+ describe ".associate!" do
107
+ cassette "engagement_associate"
108
+
109
+ let(:engagement) { Hubspot::EngagementNote.create!(nil, 'note') }
110
+ let(:contact) { Hubspot::Contact.create(email: "newcontact#{Time.now.to_i}@hsgem.com") }
111
+ subject { Hubspot::Engagement.associate!(engagement.id, 'contact', contact.id) }
112
+
113
+ it 'associate an engagement to a resource' do
114
+ subject
115
+ found_by_contact = Hubspot::Engagement.find_by_contact(contact.id)
116
+ expect(found_by_contact.first.id).to eql engagement.id
117
+ end
118
+ end
119
+
120
+ describe '#destroy!' do
121
+ cassette 'engagement_destroy'
122
+
123
+ let(:engagement) {Hubspot::EngagementNote.create!(nil, 'test note') }
124
+
125
+ it 'should remove from hubspot' do
126
+ expect(Hubspot::Engagement.find(engagement.id)).to_not be_nil
127
+
128
+ expect(engagement.destroy!).to be true
129
+ expect(engagement.destroyed?).to be true
130
+
131
+ expect(Hubspot::Engagement.find(engagement.id)).to be_nil
132
+ end
133
+ end
134
+ end
135
+
136
+ describe 'EngagementCall' do
137
+ let(:example_engagement_hash) do
138
+ VCR.use_cassette("engagement_call_example") do
139
+ HTTParty.get("https://api.hubapi.com/engagements/v1/engagements/4709059?hapikey=demo").parsed_response
140
+ end
141
+ end
142
+
143
+ describe ".create!" do
144
+ cassette "engagement_call_create"
145
+ body = "Test call"
146
+ subject { Hubspot::EngagementCall.create!(nil, body, 0) }
147
+ its(:id) { should_not be_nil }
148
+ its(:body) { should eql body }
149
+ end
150
+
151
+ describe ".find" do
152
+ cassette "engagement_call_find"
153
+ let(:engagement) { Hubspot::EngagementNote.new(example_engagement_hash) }
154
+
155
+ it 'must find by the engagement id' do
156
+ find_engagement = Hubspot::EngagementNote.find(engagement.id)
157
+ find_engagement.id.should eql engagement.id
158
+ find_engagement.body.should eql engagement.body
159
+ end
160
+ end
161
+
162
+ describe '#destroy!' do
163
+ cassette 'engagement_call_destroy'
164
+
165
+ let(:engagement) { Hubspot::EngagementCall.create!(nil, 'test call', 0) }
166
+
167
+ it 'should remove from hubspot' do
168
+ expect(Hubspot::Engagement.find(engagement.id)).to_not be_nil
169
+
170
+ expect(engagement.destroy!).to be true
171
+ expect(engagement.destroyed?).to be true
172
+
173
+ expect(Hubspot::Engagement.find(engagement.id)).to be_nil
174
+ end
175
+ end
176
+ end
177
+ end
@@ -0,0 +1,33 @@
1
+ describe Hubspot::Event do
2
+ let(:portal_id) { '62515' }
3
+ let(:sent_portal_id) { portal_id }
4
+ before { Hubspot.configure(hapikey: 'demo', portal_id: portal_id) }
5
+
6
+ describe '.trigger' do
7
+ let(:event_id) { '000000001625' }
8
+ let(:email) { 'testingapis@hubspot.com' }
9
+ let(:options) { {} }
10
+ let(:base_url) { 'https://track.hubspot.com' }
11
+ let(:url) { "#{base_url}/v1/event?_n=#{event_id}&_a=#{sent_portal_id}&email=#{CGI.escape email}" }
12
+
13
+ subject { described_class.trigger(event_id, email, options) }
14
+
15
+ before { stub_request(:get, url).to_return(status: 200, body: JSON.generate({})) }
16
+
17
+ it('sends a request to trigger the event') { is_expected.to be true }
18
+
19
+ context 'with headers' do
20
+ let(:headers) { { 'User-Agent' => 'something' } }
21
+ let(:options) { { headers: headers } }
22
+
23
+ it('sends headers') { is_expected.to be true }
24
+ end
25
+
26
+ context 'when overriding portal_id' do
27
+ let(:sent_portal_id) { '123' }
28
+ let(:options) { { params: { _a: sent_portal_id } } }
29
+
30
+ it('sends a request to the portal_id in the options') { is_expected.to be true }
31
+ end
32
+ end
33
+ end
@@ -0,0 +1,38 @@
1
+ describe Hubspot do
2
+
3
+ let(:example_file_hash) do
4
+ VCR.use_cassette("file_list", record: :none) do
5
+ url = Hubspot::Connection.send(:generate_url, Hubspot::File::LIST_FILE_PATH)
6
+ resp = HTTParty.get(url, format: :json)
7
+ resp.parsed_response["objects"].first
8
+ end
9
+ end
10
+
11
+ before do
12
+ Hubspot.configure(hapikey: "demo")
13
+ end
14
+
15
+ describe Hubspot::File do
16
+
17
+ describe ".find_by_id" do
18
+ it "should fetch specific file" do
19
+ VCR.use_cassette("file_find", record: :none) do
20
+ file = Hubspot::File.find_by_id(example_file_hash["id"])
21
+ file.id.should eq(example_file_hash["id"])
22
+ end
23
+ end
24
+ end
25
+
26
+ describe '#destroy!' do
27
+ it 'should remove from hubspot' do
28
+ VCR.use_cassette("file_delete", record: :none) do
29
+ file = Hubspot::File.find_by_id(example_file_hash["id"])
30
+ res = file.destroy!
31
+ expect(res["succeeded"]).to be true
32
+ end
33
+ end
34
+ end
35
+
36
+ end
37
+
38
+ end