hubspot-api-ruby 0.8.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/.rspec +3 -0
- data/Gemfile +3 -0
- data/Guardfile +9 -0
- data/LICENSE.txt +18 -0
- data/README.md +295 -0
- data/RELEASING.md +6 -0
- data/Rakefile +32 -0
- data/hubspot-api-ruby.gemspec +42 -0
- data/lib/hubspot-api-ruby.rb +39 -0
- data/lib/hubspot/blog.rb +98 -0
- data/lib/hubspot/collection.rb +41 -0
- data/lib/hubspot/company.rb +160 -0
- data/lib/hubspot/company_properties.rb +59 -0
- data/lib/hubspot/config.rb +63 -0
- data/lib/hubspot/connection.rb +152 -0
- data/lib/hubspot/contact.rb +110 -0
- data/lib/hubspot/contact_list.rb +129 -0
- data/lib/hubspot/contact_properties.rb +59 -0
- data/lib/hubspot/deal.rb +173 -0
- data/lib/hubspot/deal_pipeline.rb +58 -0
- data/lib/hubspot/deal_properties.rb +59 -0
- data/lib/hubspot/deprecator.rb +7 -0
- data/lib/hubspot/engagement.rb +222 -0
- data/lib/hubspot/event.rb +21 -0
- data/lib/hubspot/exceptions.rb +18 -0
- data/lib/hubspot/file.rb +38 -0
- data/lib/hubspot/form.rb +95 -0
- data/lib/hubspot/oauth.rb +50 -0
- data/lib/hubspot/owner.rb +57 -0
- data/lib/hubspot/paged_collection.rb +35 -0
- data/lib/hubspot/properties.rb +123 -0
- data/lib/hubspot/railtie.rb +10 -0
- data/lib/hubspot/resource.rb +270 -0
- data/lib/hubspot/subscription.rb +37 -0
- data/lib/hubspot/topic.rb +37 -0
- data/lib/hubspot/utils.rb +127 -0
- data/lib/tasks/hubspot.rake +53 -0
- data/spec/factories/companies.rb +9 -0
- data/spec/factories/contacts.rb +10 -0
- data/spec/lib/hubspot-ruby_spec.rb +12 -0
- data/spec/lib/hubspot/blog_spec.rb +150 -0
- data/spec/lib/hubspot/company_properties_spec.rb +410 -0
- data/spec/lib/hubspot/company_spec.rb +340 -0
- data/spec/lib/hubspot/config_spec.rb +87 -0
- data/spec/lib/hubspot/connection_spec.rb +214 -0
- data/spec/lib/hubspot/contact_list_spec.rb +301 -0
- data/spec/lib/hubspot/contact_properties_spec.rb +245 -0
- data/spec/lib/hubspot/contact_spec.rb +223 -0
- data/spec/lib/hubspot/deal_pipeline_spec.rb +85 -0
- data/spec/lib/hubspot/deal_properties_spec.rb +262 -0
- data/spec/lib/hubspot/deal_spec.rb +185 -0
- data/spec/lib/hubspot/deprecator_spec.rb +15 -0
- data/spec/lib/hubspot/engagement_spec.rb +177 -0
- data/spec/lib/hubspot/event_spec.rb +33 -0
- data/spec/lib/hubspot/file_spec.rb +38 -0
- data/spec/lib/hubspot/form_spec.rb +189 -0
- data/spec/lib/hubspot/owner_spec.rb +56 -0
- data/spec/lib/hubspot/properties_spec.rb +45 -0
- data/spec/lib/hubspot/resource_spec.rb +54 -0
- data/spec/lib/hubspot/topic_spec.rb +23 -0
- data/spec/lib/hubspot/utils_spec.rb +164 -0
- data/spec/lib/tasks/hubspot_spec.rb +119 -0
- data/spec/shared_examples/saveable_resource.rb +45 -0
- data/spec/shared_examples/updateable_resource.rb +87 -0
- data/spec/spec_helper.rb +44 -0
- data/spec/support/capture_output.rb +21 -0
- data/spec/support/cassette_helper.rb +19 -0
- data/spec/support/hubspot_api_helpers.rb +13 -0
- data/spec/support/rake.rb +46 -0
- data/spec/support/tests_helper.rb +17 -0
- data/spec/support/vcr.rb +16 -0
- metadata +369 -0
@@ -0,0 +1,340 @@
|
|
1
|
+
RSpec.describe Hubspot::Company do
|
2
|
+
# let(:example_company_hash) do
|
3
|
+
# VCR.use_cassette("company_example") do
|
4
|
+
# HTTParty.get("https://api.hubapi.com/companies/v2/companies/21827084?hapikey=demo").parsed_response
|
5
|
+
# end
|
6
|
+
# end
|
7
|
+
# let(:company_with_contacts_hash) do
|
8
|
+
# VCR.use_cassette("company_with_contacts") do
|
9
|
+
# HTTParty.get("https://api.hubapi.com/companies/v2/companies/115200636?hapikey=demo").parsed_response
|
10
|
+
# end
|
11
|
+
# end
|
12
|
+
|
13
|
+
before{ Hubspot.configure(hapikey: "demo") }
|
14
|
+
|
15
|
+
it_behaves_like "a saveable resource", :company do
|
16
|
+
def set_property(company)
|
17
|
+
company.name = "Foobar"
|
18
|
+
end
|
19
|
+
end
|
20
|
+
|
21
|
+
it_behaves_like "an updateable resource", :company do
|
22
|
+
let(:changed_properties) { { name: "Foobar" } }
|
23
|
+
let(:overlapping_properties) { { name: "My Corp", description: "My Corporation" } }
|
24
|
+
end
|
25
|
+
|
26
|
+
describe '.find' do
|
27
|
+
context 'with a valid ID' do
|
28
|
+
cassette
|
29
|
+
let(:company) { create :company }
|
30
|
+
subject { described_class.find company.id }
|
31
|
+
|
32
|
+
it 'finds the company' do
|
33
|
+
expect(subject).to be_a(described_class)
|
34
|
+
expect(subject.id).to eq(company.id)
|
35
|
+
end
|
36
|
+
end
|
37
|
+
|
38
|
+
context 'with an invalid ID' do
|
39
|
+
cassette
|
40
|
+
subject { described_class.find 0 }
|
41
|
+
|
42
|
+
it 'raises an error' do
|
43
|
+
expect {
|
44
|
+
subject
|
45
|
+
}.to raise_error(Hubspot::RequestError, /resource not found/)
|
46
|
+
end
|
47
|
+
end
|
48
|
+
end
|
49
|
+
|
50
|
+
describe '.create' do
|
51
|
+
context 'with no properties' do
|
52
|
+
cassette
|
53
|
+
subject { described_class.create }
|
54
|
+
|
55
|
+
it 'creates a new company' do
|
56
|
+
expect(subject).to be_a(described_class)
|
57
|
+
expect(subject.id).not_to be_nil
|
58
|
+
end
|
59
|
+
end
|
60
|
+
|
61
|
+
context 'with properties' do
|
62
|
+
cassette
|
63
|
+
|
64
|
+
let(:name) { "Foo Bar Inc." }
|
65
|
+
let(:properties) { { name: name } }
|
66
|
+
subject { described_class.create properties }
|
67
|
+
|
68
|
+
it 'creates a new company' do
|
69
|
+
expect(subject).to be_a(described_class)
|
70
|
+
expect(subject.id).not_to be_nil
|
71
|
+
end
|
72
|
+
|
73
|
+
it 'has the property set' do
|
74
|
+
expect(subject.name).to eq name
|
75
|
+
end
|
76
|
+
|
77
|
+
it 'is persisted' do
|
78
|
+
expect(subject).to be_persisted
|
79
|
+
end
|
80
|
+
end
|
81
|
+
end
|
82
|
+
|
83
|
+
describe '.new' do
|
84
|
+
context 'with no properties' do
|
85
|
+
it 'returns a company' do
|
86
|
+
expect(subject).to be_a(described_class)
|
87
|
+
end
|
88
|
+
|
89
|
+
it 'has no changes' do
|
90
|
+
expect(subject.changes).to be_empty
|
91
|
+
end
|
92
|
+
|
93
|
+
it 'is not persisted' do
|
94
|
+
expect(subject).not_to be_persisted
|
95
|
+
end
|
96
|
+
end
|
97
|
+
|
98
|
+
context 'with properties' do
|
99
|
+
subject { described_class.new name: Faker::Company.name }
|
100
|
+
|
101
|
+
it 'has changes' do
|
102
|
+
expect(subject.changed?).to be_truthy
|
103
|
+
end
|
104
|
+
|
105
|
+
it 'is not persisted' do
|
106
|
+
expect(subject).not_to be_persisted
|
107
|
+
end
|
108
|
+
end
|
109
|
+
|
110
|
+
context 'with an ID property' do
|
111
|
+
subject { described_class.new id: 1 }
|
112
|
+
|
113
|
+
it 'has changes' do
|
114
|
+
pending "tracking ID property changes"
|
115
|
+
expect(subject.changed?).to be_truthy
|
116
|
+
end
|
117
|
+
|
118
|
+
it 'is not persisted' do
|
119
|
+
pending "persisted flag rework"
|
120
|
+
expect(subject).not_to be_persisted
|
121
|
+
end
|
122
|
+
end
|
123
|
+
end
|
124
|
+
|
125
|
+
describe '#reload' do
|
126
|
+
context 'with a valid ID' do
|
127
|
+
cassette
|
128
|
+
|
129
|
+
let(:company) { create :company }
|
130
|
+
subject { inst = described_class.new(company.id); inst.reload }
|
131
|
+
|
132
|
+
it 'loads the company details' do
|
133
|
+
expect(subject.id).to eq(company.id)
|
134
|
+
expect(subject.name).to eq(company.name)
|
135
|
+
end
|
136
|
+
end
|
137
|
+
|
138
|
+
context 'without an ID' do
|
139
|
+
cassette
|
140
|
+
|
141
|
+
it 'raises an error' do
|
142
|
+
expect {
|
143
|
+
subject.reload
|
144
|
+
}.to raise_error(Hubspot::InvalidParams)
|
145
|
+
end
|
146
|
+
end
|
147
|
+
end
|
148
|
+
|
149
|
+
describe '#delete' do
|
150
|
+
context 'when not persisted' do
|
151
|
+
cassette
|
152
|
+
|
153
|
+
subject { build :company }
|
154
|
+
|
155
|
+
it 'raises an error' do
|
156
|
+
expect {
|
157
|
+
subject.delete
|
158
|
+
}.to raise_error(Hubspot::InvalidParams)
|
159
|
+
end
|
160
|
+
end
|
161
|
+
|
162
|
+
context 'when persisted' do
|
163
|
+
cassette
|
164
|
+
|
165
|
+
subject { create :company }
|
166
|
+
|
167
|
+
it 'sets the deleted flag' do
|
168
|
+
expect {
|
169
|
+
subject.delete
|
170
|
+
}.to change { subject.deleted? }.from(false).to(true)
|
171
|
+
end
|
172
|
+
|
173
|
+
it 'deletes the resource' do
|
174
|
+
subject.delete
|
175
|
+
|
176
|
+
expect {
|
177
|
+
described_class.find subject.id
|
178
|
+
}.to raise_error(Hubspot::RequestError)
|
179
|
+
end
|
180
|
+
end
|
181
|
+
end
|
182
|
+
|
183
|
+
describe '.all' do
|
184
|
+
context 'with no options' do
|
185
|
+
cassette
|
186
|
+
|
187
|
+
subject { described_class.all }
|
188
|
+
|
189
|
+
it 'returns a collection' do
|
190
|
+
expect(subject).to be_a(Hubspot::PagedCollection)
|
191
|
+
expect(subject.first).to be_a(Hubspot::Company)
|
192
|
+
end
|
193
|
+
|
194
|
+
it 'has an offset' do
|
195
|
+
expect(subject.next_offset).not_to be_blank
|
196
|
+
end
|
197
|
+
|
198
|
+
it 'identifies if there are more resources' do
|
199
|
+
expect(subject.more?).not_to be_nil
|
200
|
+
end
|
201
|
+
end
|
202
|
+
|
203
|
+
context 'with an offset' do
|
204
|
+
cassette
|
205
|
+
|
206
|
+
let!(:company) { create :company }
|
207
|
+
subject { described_class.all offset: company.id }
|
208
|
+
|
209
|
+
it 'returns a collection' do
|
210
|
+
expect(subject).to be_a(Hubspot::PagedCollection)
|
211
|
+
end
|
212
|
+
|
213
|
+
it 'has an offset' do
|
214
|
+
expect(subject.next_offset).not_to be_blank
|
215
|
+
end
|
216
|
+
|
217
|
+
it 'identifies if there are more resources' do
|
218
|
+
expect(subject.more?).not_to be_nil
|
219
|
+
end
|
220
|
+
end
|
221
|
+
|
222
|
+
context 'with a limit' do
|
223
|
+
cassette
|
224
|
+
|
225
|
+
let(:limit) { 1 }
|
226
|
+
subject { described_class.all limit: limit }
|
227
|
+
|
228
|
+
it 'returns a collection' do
|
229
|
+
expect(subject).to be_a(Hubspot::PagedCollection)
|
230
|
+
expect(subject.first).to be_a(Hubspot::Company)
|
231
|
+
end
|
232
|
+
|
233
|
+
it 'respects the limit' do
|
234
|
+
expect(subject.size).to eq(limit)
|
235
|
+
end
|
236
|
+
end
|
237
|
+
end
|
238
|
+
|
239
|
+
describe '.search_domain' do
|
240
|
+
cassette
|
241
|
+
|
242
|
+
let!(:company) { create :company }
|
243
|
+
|
244
|
+
subject { described_class.search_domain company.domain }
|
245
|
+
|
246
|
+
it 'returns a collection' do
|
247
|
+
expect(subject).to be_a(Hubspot::PagedCollection)
|
248
|
+
expect(subject.first).to be_a(Hubspot::Company)
|
249
|
+
end
|
250
|
+
end
|
251
|
+
|
252
|
+
describe '.recently_created' do
|
253
|
+
cassette
|
254
|
+
|
255
|
+
subject { described_class.recently_created }
|
256
|
+
|
257
|
+
it 'returns a collection' do
|
258
|
+
expect(subject).to be_a(Hubspot::PagedCollection)
|
259
|
+
end
|
260
|
+
end
|
261
|
+
|
262
|
+
describe '.recently_modified' do
|
263
|
+
cassette
|
264
|
+
|
265
|
+
subject { described_class.recently_modified }
|
266
|
+
|
267
|
+
it 'returns a collection' do
|
268
|
+
expect(subject).to be_a(Hubspot::PagedCollection)
|
269
|
+
end
|
270
|
+
end
|
271
|
+
|
272
|
+
describe '.add_contact' do
|
273
|
+
context 'with a valid company ID and contact ID' do
|
274
|
+
cassette
|
275
|
+
|
276
|
+
let(:company) { create :company }
|
277
|
+
let(:contact) { create :contact }
|
278
|
+
|
279
|
+
subject { described_class.add_contact company.id, contact.id }
|
280
|
+
|
281
|
+
it 'returns success' do
|
282
|
+
expect(subject).to be_truthy
|
283
|
+
end
|
284
|
+
|
285
|
+
it 'adds the contact to the company' do
|
286
|
+
expect {
|
287
|
+
subject
|
288
|
+
}.to change { company.contact_ids }.by([contact.id])
|
289
|
+
end
|
290
|
+
end
|
291
|
+
|
292
|
+
context 'with a valid company ID and invalid contact ID' do
|
293
|
+
cassette
|
294
|
+
|
295
|
+
let(:company) { create :company }
|
296
|
+
|
297
|
+
subject { described_class.add_contact company.id, 1 }
|
298
|
+
|
299
|
+
it 'raises an error' do
|
300
|
+
expect {
|
301
|
+
subject
|
302
|
+
}.to raise_error(Hubspot::RequestError, /Contact with the vid/)
|
303
|
+
end
|
304
|
+
end
|
305
|
+
|
306
|
+
context 'with an invalid company ID' do
|
307
|
+
cassette
|
308
|
+
|
309
|
+
subject { described_class.add_contact 1, 1 }
|
310
|
+
|
311
|
+
it 'raises an error' do
|
312
|
+
expect {
|
313
|
+
subject
|
314
|
+
}.to raise_error(Hubspot::RequestError, /company with the ID/)
|
315
|
+
end
|
316
|
+
end
|
317
|
+
end
|
318
|
+
|
319
|
+
describe '.remove_contact' do
|
320
|
+
context 'with a valid company ID and contact ID' do
|
321
|
+
cassette allow_playback_repeats: true
|
322
|
+
|
323
|
+
let!(:company) { create :company }
|
324
|
+
let!(:contact) { create :contact, associatedCompanyId: company.id }
|
325
|
+
|
326
|
+
subject { described_class.remove_contact company.id, contact.id }
|
327
|
+
|
328
|
+
it 'returns success' do
|
329
|
+
expect(subject).to be_truthy
|
330
|
+
end
|
331
|
+
|
332
|
+
# Testing this turns out to be hard since using associatedCompanyId doesn't immediately add
|
333
|
+
# the contact to the company but triggers some background job to perform the update. Since
|
334
|
+
# we're testing the gem interface and not the API (that's Hubspot's job) this should be OK to
|
335
|
+
# leave out.
|
336
|
+
#
|
337
|
+
# it 'removes the contact from the company'
|
338
|
+
end
|
339
|
+
end
|
340
|
+
end
|
@@ -0,0 +1,87 @@
|
|
1
|
+
describe Hubspot::Config do
|
2
|
+
describe ".configure" do
|
3
|
+
it "sets the hapikey config" do
|
4
|
+
hapikey = "demo"
|
5
|
+
|
6
|
+
config = Hubspot::Config.configure(hapikey: hapikey)
|
7
|
+
|
8
|
+
expect(config.hapikey).to eq(hapikey)
|
9
|
+
end
|
10
|
+
|
11
|
+
it "changes the base_url" do
|
12
|
+
base_url = "https://api.hubapi.com/v2"
|
13
|
+
|
14
|
+
config = Hubspot::Config.configure(
|
15
|
+
hapikey: "123abc",
|
16
|
+
base_url: base_url
|
17
|
+
)
|
18
|
+
|
19
|
+
expect(config.base_url).to eq(base_url)
|
20
|
+
end
|
21
|
+
|
22
|
+
it "sets a default value for base_url" do
|
23
|
+
config = Hubspot::Config.configure(hapikey: "123abc")
|
24
|
+
|
25
|
+
expect(config.base_url).to eq("https://api.hubapi.com")
|
26
|
+
end
|
27
|
+
|
28
|
+
it "sets a value for portal_id" do
|
29
|
+
portal_id = "62515"
|
30
|
+
|
31
|
+
config = Hubspot::Config.configure(
|
32
|
+
hapikey: "123abc",
|
33
|
+
portal_id: portal_id
|
34
|
+
)
|
35
|
+
|
36
|
+
expect(config.portal_id).to eq(portal_id)
|
37
|
+
end
|
38
|
+
|
39
|
+
it "raises when an authentication approach is not provided" do
|
40
|
+
expect {
|
41
|
+
Hubspot::Config.configure({})
|
42
|
+
}.to raise_error(Hubspot::ConfigurationError)
|
43
|
+
end
|
44
|
+
|
45
|
+
it "raises when two authentication approaches are provided" do
|
46
|
+
expect {
|
47
|
+
Hubspot::Config.configure({
|
48
|
+
hapikey: "123abc",
|
49
|
+
access_token: "456def",
|
50
|
+
})
|
51
|
+
}.to raise_error(Hubspot::ConfigurationError)
|
52
|
+
end
|
53
|
+
end
|
54
|
+
|
55
|
+
describe ".reset!" do
|
56
|
+
it "resets the config values" do
|
57
|
+
Hubspot::Config.configure(hapikey: "123abc", portal_id: "456def")
|
58
|
+
|
59
|
+
Hubspot::Config.reset!
|
60
|
+
|
61
|
+
expect(Hubspot::Config.hapikey).to be nil
|
62
|
+
expect(Hubspot::Config.portal_id).to be nil
|
63
|
+
end
|
64
|
+
end
|
65
|
+
|
66
|
+
describe ".ensure!" do
|
67
|
+
context "when a specified parameter is missing" do
|
68
|
+
it "raises an error" do
|
69
|
+
Hubspot::Config.configure(hapikey: "123abc")
|
70
|
+
|
71
|
+
expect {
|
72
|
+
Hubspot::Config.ensure!(:portal_id)
|
73
|
+
}.to raise_error(Hubspot::ConfigurationError)
|
74
|
+
end
|
75
|
+
end
|
76
|
+
|
77
|
+
context "when all specified parameters are present" do
|
78
|
+
it "does not raise an error" do
|
79
|
+
Hubspot::Config.configure(hapikey: "123abc", portal_id: "456def")
|
80
|
+
|
81
|
+
expect {
|
82
|
+
Hubspot::Config.ensure!(:portal_id)
|
83
|
+
}.not_to raise_error
|
84
|
+
end
|
85
|
+
end
|
86
|
+
end
|
87
|
+
end
|
@@ -0,0 +1,214 @@
|
|
1
|
+
describe Hubspot::Connection do
|
2
|
+
before do
|
3
|
+
Hubspot.configure hapikey: 'fake'
|
4
|
+
end
|
5
|
+
|
6
|
+
describe ".get_json" do
|
7
|
+
it "returns the parsed response from the GET request" do
|
8
|
+
path = "/some/path"
|
9
|
+
body = { key: "value" }
|
10
|
+
|
11
|
+
stub_request(:get, "https://api.hubapi.com/some/path?hapikey=fake").
|
12
|
+
to_return(status: 200, body: JSON.generate(body))
|
13
|
+
|
14
|
+
result = Hubspot::Connection.get_json(path, {})
|
15
|
+
|
16
|
+
expect(result).to eq({ "key" => "value" })
|
17
|
+
end
|
18
|
+
end
|
19
|
+
|
20
|
+
describe ".post_json" do
|
21
|
+
it "returns the parsed response from the POST request" do
|
22
|
+
path = "/some/path"
|
23
|
+
body = { id: 1, name: "ABC" }
|
24
|
+
|
25
|
+
stub_request(:post, "https://api.hubapi.com/some/path?hapikey=fake&name=ABC").
|
26
|
+
to_return(status: 200, body: JSON.generate(body))
|
27
|
+
|
28
|
+
result = Hubspot::Connection.post_json(path, params: { name: "ABC" })
|
29
|
+
|
30
|
+
expect(result).to eq({ "id" => 1, "name" => "ABC" })
|
31
|
+
end
|
32
|
+
end
|
33
|
+
|
34
|
+
describe ".delete_json" do
|
35
|
+
it "returns the response from the DELETE request" do
|
36
|
+
path = "/some/path"
|
37
|
+
|
38
|
+
stub_request(:delete, "https://api.hubapi.com/some/path?hapikey=fake").
|
39
|
+
to_return(status: 204, body: JSON.generate({}))
|
40
|
+
|
41
|
+
result = Hubspot::Connection.delete_json(path, {})
|
42
|
+
|
43
|
+
expect(result.code).to eq(204)
|
44
|
+
end
|
45
|
+
end
|
46
|
+
|
47
|
+
describe ".put_json" do
|
48
|
+
it "issues a PUT request and returns the parsed body" do
|
49
|
+
path = "/some/path"
|
50
|
+
update_options = { params: {}, body: {} }
|
51
|
+
|
52
|
+
stub_request(:put, "https://api.hubapi.com/some/path?hapikey=fake").
|
53
|
+
to_return(status: 200, body: JSON.generate(vid: 123))
|
54
|
+
|
55
|
+
response = Hubspot::Connection.put_json(path, update_options)
|
56
|
+
|
57
|
+
assert_requested(
|
58
|
+
:put,
|
59
|
+
"https://api.hubapi.com/some/path?hapikey=fake",
|
60
|
+
{
|
61
|
+
body: "{}",
|
62
|
+
headers: { "Content-Type" => "application/json" },
|
63
|
+
}
|
64
|
+
)
|
65
|
+
|
66
|
+
expect(response).to eq({ "vid" => 123 })
|
67
|
+
end
|
68
|
+
|
69
|
+
it "logs information about the request and response" do
|
70
|
+
path = "/some/path"
|
71
|
+
update_options = { params: {}, body: {} }
|
72
|
+
|
73
|
+
logger = stub_logger
|
74
|
+
|
75
|
+
stub_request(:put, "https://api.hubapi.com/some/path?hapikey=fake").
|
76
|
+
to_return(status: 200, body: JSON.generate("response body"))
|
77
|
+
|
78
|
+
Hubspot::Connection.put_json(path, update_options)
|
79
|
+
|
80
|
+
expect(logger).to have_received(:info).with(<<~MSG)
|
81
|
+
Hubspot: https://api.hubapi.com/some/path?hapikey=fake.
|
82
|
+
Body: {}.
|
83
|
+
Response: 200 "response body"
|
84
|
+
MSG
|
85
|
+
end
|
86
|
+
|
87
|
+
it "raises when the request fails" do
|
88
|
+
path = "/some/path"
|
89
|
+
update_options = { params: {}, body: {} }
|
90
|
+
|
91
|
+
stub_request(:put, "https://api.hubapi.com/some/path?hapikey=fake").
|
92
|
+
to_return(status: 401)
|
93
|
+
|
94
|
+
expect {
|
95
|
+
Hubspot::Connection.put_json(path, update_options)
|
96
|
+
}.to raise_error(Hubspot::RequestError)
|
97
|
+
end
|
98
|
+
end
|
99
|
+
|
100
|
+
context 'private methods' do
|
101
|
+
describe ".generate_url" do
|
102
|
+
let(:path){ "/test/:email/profile" }
|
103
|
+
let(:params){{email: "test"}}
|
104
|
+
let(:options){{}}
|
105
|
+
subject{ Hubspot::Connection.send(:generate_url, path, params, options) }
|
106
|
+
before{ Hubspot.configure(hapikey: "demo", portal_id: "62515") }
|
107
|
+
|
108
|
+
it "doesn't modify params" do
|
109
|
+
expect{ subject }.to_not change{params}
|
110
|
+
end
|
111
|
+
|
112
|
+
context "with a portal_id param" do
|
113
|
+
let(:path){ "/test/:portal_id/profile" }
|
114
|
+
let(:params){{}}
|
115
|
+
it{ should == "https://api.hubapi.com/test/62515/profile?hapikey=demo" }
|
116
|
+
end
|
117
|
+
|
118
|
+
context "when configure hasn't been called" do
|
119
|
+
before{ Hubspot::Config.reset! }
|
120
|
+
it "raises a config exception" do
|
121
|
+
expect{ subject }.to raise_error Hubspot::ConfigurationError
|
122
|
+
end
|
123
|
+
end
|
124
|
+
|
125
|
+
context "with interpolations but no params" do
|
126
|
+
let(:params){{}}
|
127
|
+
it "raises an interpolation exception" do
|
128
|
+
expect{ subject }.to raise_error Hubspot::MissingInterpolation
|
129
|
+
end
|
130
|
+
end
|
131
|
+
|
132
|
+
context "with an interpolated param" do
|
133
|
+
let(:params){ {email: "email@address.com"} }
|
134
|
+
it{ should == "https://api.hubapi.com/test/email%40address.com/profile?hapikey=demo" }
|
135
|
+
end
|
136
|
+
|
137
|
+
context "with multiple interpolated params" do
|
138
|
+
let(:path){ "/test/:email/:id/profile" }
|
139
|
+
let(:params){{email: "email@address.com", id: 1234}}
|
140
|
+
it{ should == "https://api.hubapi.com/test/email%40address.com/1234/profile?hapikey=demo" }
|
141
|
+
end
|
142
|
+
|
143
|
+
context "with query params" do
|
144
|
+
let(:params){{email: "email@address.com", id: 1234}}
|
145
|
+
it{ should == "https://api.hubapi.com/test/email%40address.com/profile?id=1234&hapikey=demo" }
|
146
|
+
|
147
|
+
context "containing a time" do
|
148
|
+
let(:start_time) { Time.now }
|
149
|
+
let(:params){{email: "email@address.com", id: 1234, start: start_time}}
|
150
|
+
it{ should == "https://api.hubapi.com/test/email%40address.com/profile?id=1234&start=#{start_time.to_i * 1000}&hapikey=demo" }
|
151
|
+
end
|
152
|
+
|
153
|
+
context "containing a range" do
|
154
|
+
let(:start_time) { Time.now }
|
155
|
+
let(:end_time) { Time.now + 1.year }
|
156
|
+
let(:params){{email: "email@address.com", id: 1234, created__range: start_time..end_time }}
|
157
|
+
it{ should == "https://api.hubapi.com/test/email%40address.com/profile?id=1234&created__range=#{start_time.to_i * 1000}&created__range=#{end_time.to_i * 1000}&hapikey=demo" }
|
158
|
+
end
|
159
|
+
|
160
|
+
context "containing an array of strings" do
|
161
|
+
let(:path){ "/test/emails" }
|
162
|
+
let(:params){{batch_email: %w(email1@example.com email2@example.com)}}
|
163
|
+
it{ should == "https://api.hubapi.com/test/emails?email=email1%40example.com&email=email2%40example.com&hapikey=demo" }
|
164
|
+
end
|
165
|
+
end
|
166
|
+
|
167
|
+
context "with options" do
|
168
|
+
let(:options){ {base_url: "https://cool.com", hapikey: false} }
|
169
|
+
it{ should == "https://cool.com/test/test/profile"}
|
170
|
+
end
|
171
|
+
|
172
|
+
context "passing Array as parameters for batch mode, key is prefixed with batch_" do
|
173
|
+
let(:path) { Hubspot::ContactList::LIST_BATCH_PATH }
|
174
|
+
let(:params) { { batch_list_id: [1,2,3] } }
|
175
|
+
it{ should == "https://api.hubapi.com/contacts/v1/lists/batch?listId=1&listId=2&listId=3&hapikey=demo" }
|
176
|
+
end
|
177
|
+
end
|
178
|
+
end
|
179
|
+
|
180
|
+
def stub_logger
|
181
|
+
instance_double(Logger, info: true).tap do |logger|
|
182
|
+
allow(Hubspot::Config).to receive(:logger).and_return(logger)
|
183
|
+
end
|
184
|
+
end
|
185
|
+
end
|
186
|
+
|
187
|
+
describe Hubspot::EventConnection do
|
188
|
+
describe '.trigger' do
|
189
|
+
let(:path) { '/path' }
|
190
|
+
let(:options) { { params: {} } }
|
191
|
+
let(:headers) { nil }
|
192
|
+
|
193
|
+
subject { described_class.trigger(path, options) }
|
194
|
+
before do
|
195
|
+
Hubspot.configure(hapikey: 'demo', portal_id: '62515')
|
196
|
+
allow(described_class).to receive(:get).and_return(true)
|
197
|
+
end
|
198
|
+
|
199
|
+
it 'calls get with a custom url' do
|
200
|
+
subject
|
201
|
+
expect(described_class).to have_received(:get).with('https://track.hubspot.com/path', body: nil, headers: nil)
|
202
|
+
end
|
203
|
+
|
204
|
+
context 'with more options' do
|
205
|
+
let(:headers) { { 'User-Agent' => 'something' } }
|
206
|
+
let(:options) { { params: {}, headers: headers } }
|
207
|
+
|
208
|
+
it 'supports headers' do
|
209
|
+
subject
|
210
|
+
expect(described_class).to have_received(:get).with('https://track.hubspot.com/path', body: nil, headers: headers)
|
211
|
+
end
|
212
|
+
end
|
213
|
+
end
|
214
|
+
end
|