hubspot-ruby 0.1.7 → 0.1.8

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 (65) hide show
  1. checksums.yaml +4 -4
  2. data/hubspot-ruby.gemspec +42 -5
  3. data/lib/hubspot-ruby.rb +3 -0
  4. data/lib/hubspot/blog.rb +14 -48
  5. data/lib/hubspot/connection.rb +95 -0
  6. data/lib/hubspot/contact.rb +74 -91
  7. data/lib/hubspot/contact_list.rb +127 -0
  8. data/lib/hubspot/contact_properties.rb +12 -0
  9. data/lib/hubspot/deal.rb +29 -25
  10. data/lib/hubspot/exceptions.rb +2 -0
  11. data/lib/hubspot/form.rb +88 -4
  12. data/lib/hubspot/topic.rb +8 -24
  13. data/lib/hubspot/utils.rb +0 -51
  14. data/lib/hubspot/version.rb +1 -1
  15. data/spec/fixtures/vcr_cassettes/add_contacts_to_lists.yml +281 -0
  16. data/spec/fixtures/vcr_cassettes/contact_create.yml +34 -2
  17. data/spec/fixtures/vcr_cassettes/contact_create_existing_email.yml +34 -2
  18. data/spec/fixtures/vcr_cassettes/contact_create_invalid_email.yml +35 -3
  19. data/spec/fixtures/vcr_cassettes/contact_create_with_params.yml +34 -2
  20. data/spec/fixtures/vcr_cassettes/contact_destroy.yml +36 -4
  21. data/spec/fixtures/vcr_cassettes/contact_find_by_email_batch_mode.yml +509 -0
  22. data/spec/fixtures/vcr_cassettes/contact_find_by_id_batch_mode.yml +33 -0
  23. data/spec/fixtures/vcr_cassettes/contact_find_by_utk_batch_mode.yml +33 -0
  24. data/spec/fixtures/vcr_cassettes/contact_list_batch_find.yml +65 -0
  25. data/spec/fixtures/vcr_cassettes/contact_list_destroy.yml +63 -0
  26. data/spec/fixtures/vcr_cassettes/contact_list_example.yml +33 -0
  27. data/spec/fixtures/vcr_cassettes/contact_list_find.yml +96 -0
  28. data/spec/fixtures/vcr_cassettes/contact_list_refresh.yml +33 -0
  29. data/spec/fixtures/vcr_cassettes/contact_list_update.yml +36 -0
  30. data/spec/fixtures/vcr_cassettes/contacts_among_list.yml +189 -0
  31. data/spec/fixtures/vcr_cassettes/create_form.yml +39 -0
  32. data/spec/fixtures/vcr_cassettes/create_list.yml +36 -0
  33. data/spec/fixtures/vcr_cassettes/create_list_with_filters.yml +36 -0
  34. data/spec/fixtures/vcr_cassettes/deal_create.yml +29 -0
  35. data/spec/fixtures/vcr_cassettes/deal_find.yml +56 -0
  36. data/spec/fixtures/vcr_cassettes/destroy_deal.yml +110 -0
  37. data/spec/fixtures/vcr_cassettes/fail_to_create_form.yml +35 -0
  38. data/spec/fixtures/vcr_cassettes/fail_to_create_list.yml +35 -0
  39. data/spec/fixtures/vcr_cassettes/field_among_form.yml +34 -0
  40. data/spec/fixtures/vcr_cassettes/fields_among_form.yml +35 -0
  41. data/spec/fixtures/vcr_cassettes/find_all_contacts.yml +39 -0
  42. data/spec/fixtures/vcr_cassettes/find_all_dynamic_lists.yml +104 -0
  43. data/spec/fixtures/vcr_cassettes/find_all_forms.yml +15378 -0
  44. data/spec/fixtures/vcr_cassettes/find_all_lists.yml +138 -0
  45. data/spec/fixtures/vcr_cassettes/find_all_recent_contacts.yml +33 -0
  46. data/spec/fixtures/vcr_cassettes/find_all_stastic_lists.yml +21876 -0
  47. data/spec/fixtures/vcr_cassettes/form_destroy.yml +64 -0
  48. data/spec/fixtures/vcr_cassettes/form_example.yml +39 -0
  49. data/spec/fixtures/vcr_cassettes/form_find.yml +69 -0
  50. data/spec/fixtures/vcr_cassettes/form_submit_data.yml +130 -0
  51. data/spec/fixtures/vcr_cassettes/form_update.yml +77 -0
  52. data/spec/fixtures/vcr_cassettes/one_month_blog_posts_filter_state.yml +5168 -0
  53. data/spec/fixtures/vcr_cassettes/remove_contacts_from_lists.yml +315 -0
  54. data/spec/lib/hubspot/blog_spec.rb +4 -3
  55. data/spec/lib/hubspot/connection_spec.rb +112 -0
  56. data/spec/lib/hubspot/contact_list_spec.rb +249 -0
  57. data/spec/lib/hubspot/contact_properties_spec.rb +8 -0
  58. data/spec/lib/hubspot/contact_spec.rb +110 -54
  59. data/spec/lib/hubspot/deal_spec.rb +11 -0
  60. data/spec/lib/hubspot/form_spec.rb +157 -10
  61. data/spec/lib/hubspot/utils_spec.rb +0 -67
  62. data/spec/live/deals_integration_spec.rb +35 -0
  63. data/spec/spec_helper.rb +2 -0
  64. data/spec/support/tests_helper.rb +17 -0
  65. metadata +70 -33
@@ -0,0 +1,249 @@
1
+ describe Hubspot::ContactList do
2
+ let(:example_contact_list_hash) do
3
+ VCR.use_cassette("contact_list_example", record: :none) do
4
+ HTTParty.get("https://api.hubapi.com/contacts/v1/lists/1?hapikey=demo").parsed_response
5
+ end
6
+ end
7
+
8
+ let(:static_list) { Hubspot::ContactList.all(static: true, count: 3).last }
9
+ let(:dynamic_list) { Hubspot::ContactList.all(dynamic: true, count: 1).first }
10
+
11
+ let(:example_contact_hash) do
12
+ VCR.use_cassette("contact_example", record: :none) do
13
+ HTTParty.get("https://api.hubapi.com/contacts/v1/contact/email/testingapis@hubspot.com/profile?hapikey=demo").parsed_response
14
+ end
15
+ end
16
+
17
+ describe '#initialize' do
18
+ subject { Hubspot::ContactList.new(example_contact_list_hash) }
19
+
20
+ it { should be_an_instance_of Hubspot::ContactList }
21
+ its(:id) { should be_an(Integer) }
22
+ its(:portal_id) { should be_a(Integer) }
23
+ its(:name) { should_not be_empty }
24
+ its(:dynamic) { should be true }
25
+ its(:properties) { should be_a(Hash) }
26
+ end
27
+
28
+ before { Hubspot.configure(hapikey: "demo") }
29
+
30
+ describe '#contacts' do
31
+ cassette 'contacts_among_list'
32
+
33
+ let(:list) { Hubspot::ContactList.new(example_contact_list_hash) }
34
+
35
+ it 'returns by defaut 20 contact lists' do
36
+ expect(list.contacts.count).to eql 20
37
+ contact = list.contacts.first
38
+ expect(contact).to be_a(Hubspot::Contact)
39
+ end
40
+
41
+ it 'add default properties to the contacts returned' do
42
+ contact = list.contacts.first
43
+ expect(contact.email).to_not be_empty
44
+ end
45
+
46
+ expect_count_and_offset do |params|
47
+ Hubspot::ContactList.find(1).contacts(params)
48
+ end
49
+ end
50
+
51
+ describe '.create' do
52
+ subject{ Hubspot::ContactList.create!({ name: name }) }
53
+
54
+ context 'with all required parameters' do
55
+ cassette 'create_list'
56
+
57
+ let(:name) { 'testing list' }
58
+ it { should be_an_instance_of Hubspot::ContactList }
59
+ its(:id) { should be_an(Integer) }
60
+ its(:portal_id) { should be_an(Integer) }
61
+ its(:dynamic) { should be false }
62
+
63
+ context 'adding filters parameters' do
64
+ cassette 'create_list_with_filters'
65
+
66
+ it 'returns a ContactList object with filters set' do
67
+ name = 'list with filters'
68
+ filters_param = [[{ operator: "EQ", value: "@hubspot", property: "twitterhandle", type: "string"}]]
69
+ list_with_filters = Hubspot::ContactList.create!({ name: name, filters: filters_param })
70
+ expect(list_with_filters).to be_a(Hubspot::ContactList)
71
+ expect(list_with_filters.properties['filters']).to_not be_empty
72
+ end
73
+ end
74
+ end
75
+
76
+ context 'without all required parameters' do
77
+ cassette 'fail_to_create_list'
78
+
79
+ it 'raises an error' do
80
+ expect { Hubspot::ContactList.create!({ name: nil }) }.to raise_error(Hubspot::RequestError)
81
+ end
82
+ end
83
+ end
84
+
85
+ describe '.all' do
86
+ context 'all list types' do
87
+ cassette 'find_all_lists'
88
+
89
+ it 'returns by defaut 20 contact lists' do
90
+ lists = Hubspot::ContactList.all
91
+ expect(lists.count).to eql 20
92
+
93
+ list = lists.first
94
+ expect(list).to be_a(Hubspot::ContactList)
95
+ expect(list.id).to be_an(Integer)
96
+ end
97
+
98
+ expect_count_and_offset { |params| Hubspot::ContactList.all(params) }
99
+ end
100
+
101
+ context 'static lists' do
102
+ cassette 'find_all_stastic_lists'
103
+
104
+ it 'returns by defaut all the static contact lists' do
105
+ lists = Hubspot::ContactList.all(static: true)
106
+ expect(lists.count).to be > 20
107
+
108
+ list = lists.first
109
+ expect(list).to be_a(Hubspot::ContactList)
110
+ expect(list.dynamic).to be false
111
+ end
112
+ end
113
+
114
+ context 'dynamic lists' do
115
+ cassette 'find_all_dynamic_lists'
116
+
117
+ it 'returns by defaut all the static contact lists' do
118
+ lists = Hubspot::ContactList.all(dynamic: true)
119
+ expect(lists.count).to be > 20
120
+
121
+ list = lists.first
122
+ expect(list).to be_a(Hubspot::ContactList)
123
+ expect(list.dynamic).to be true
124
+ end
125
+ end
126
+ end
127
+
128
+ describe '.find' do
129
+ context 'given an id' do
130
+ cassette "contact_list_find"
131
+ subject { Hubspot::ContactList.find(id) }
132
+
133
+ context 'when the contact list is found' do
134
+ let(:id) { 1 }
135
+ it { should be_an_instance_of Hubspot::ContactList }
136
+ its(:name) { should == 'twitterers' }
137
+
138
+ let(:id) { '1' }
139
+ it { should be_an_instance_of Hubspot::ContactList }
140
+ end
141
+
142
+ context 'Wrong parameter type given' do
143
+ it 'raises an error' do
144
+ expect { Hubspot::ContactList.find(static_list) }.to raise_error(Hubspot::InvalidParams)
145
+ end
146
+ end
147
+
148
+ context 'when the contact list is not found' do
149
+ it 'raises an error' do
150
+ expect { Hubspot::ContactList.find(-1) }.to raise_error(Hubspot::RequestError)
151
+ end
152
+ end
153
+ end
154
+
155
+ context 'given a list of ids' do
156
+ cassette "contact_list_batch_find"
157
+
158
+ it 'find lists of contacts' do
159
+ lists = Hubspot::ContactList.find([2,3,4])
160
+ list = lists.first
161
+ expect(list).to be_a(Hubspot::ContactList)
162
+ expect(list.id).to be == 2
163
+ expect(lists.second.id).to be == 3
164
+ expect(lists.last.id).to be == 4
165
+ end
166
+ end
167
+ end
168
+
169
+ describe '#add' do
170
+ cassette "add_contacts_to_lists"
171
+
172
+ context 'static list' do
173
+ it 'returns true if contacts have been added to the list' do
174
+ contact = Hubspot::Contact.all(count: 1).first
175
+ mock(Hubspot::Connection).post_json("/contacts/v1/lists/:list_id/add", {:params=>{:list_id=>4}, :body=>{:vids=>[contact.vid]}}) { { 'updated' => [contact.vid] } }
176
+
177
+ expect(static_list.add(contact)).to be true
178
+ end
179
+
180
+ it 'returns false if the contact already exists in the list' do
181
+ contact = static_list.contacts(count: 1).first
182
+ expect(static_list.add(contact)).to be false
183
+ end
184
+ end
185
+
186
+ context 'dynamic list' do
187
+ it 'raises error if try to add a contact to a dynamic list' do
188
+ contact = Hubspot::Contact.new(example_contact_hash)
189
+ expect { dynamic_list.add(contact) }.to raise_error(Hubspot::RequestError)
190
+ end
191
+ end
192
+ end
193
+
194
+ describe '#remove' do
195
+ cassette "remove_contacts_from_lists"
196
+
197
+ context 'static list' do
198
+ it 'returns true if removes all contacts in batch mode' do
199
+ contacts = static_list.contacts(count: 2)
200
+ expect(static_list.remove([contacts.first, contacts.last])).to be true
201
+ end
202
+
203
+ it 'returns false if the contact cannot be removed' do
204
+ contact_not_present_in_list = Hubspot::Contact.new(example_contact_hash)
205
+ expect(static_list.remove(contact_not_present_in_list)).to be false
206
+ end
207
+ end
208
+
209
+ context 'dynamic list' do
210
+ it 'raises error if try to remove a contact from a dynamic list' do
211
+ contact = dynamic_list.contacts(recent: true, count: 1).first
212
+ expect { dynamic_list.remove(contact) }.to raise_error(Hubspot::RequestError)
213
+ end
214
+ end
215
+ end
216
+
217
+ describe '#update!' do
218
+ cassette "contact_list_update"
219
+
220
+ let(:contact_list) { Hubspot::ContactList.new(example_contact_list_hash) }
221
+ let(:params) { { name: "update list name" } }
222
+ subject { contact_list.update!(params) }
223
+
224
+ it { should be_an_instance_of Hubspot::ContactList }
225
+ its(:name){ should == "update list name" }
226
+ end
227
+
228
+ describe '#destroy!' do
229
+ cassette "contact_list_destroy"
230
+
231
+ let(:contact_list) { Hubspot::ContactList.create!({ name: "newcontactlist_#{Time.now.to_i}"}) }
232
+ subject{ contact_list.destroy! }
233
+ it { should be_true }
234
+
235
+ it "should be destroyed" do
236
+ subject
237
+ contact_list.destroyed?.should be_true
238
+ end
239
+ end
240
+
241
+ describe '#refresh' do
242
+ cassette "contact_list_refresh"
243
+
244
+ let(:contact_list) { Hubspot::ContactList.new(example_contact_list_hash) }
245
+ subject { contact_list.refresh }
246
+
247
+ it { should be true }
248
+ end
249
+ end
@@ -0,0 +1,8 @@
1
+ describe Hubspot::ContactProperties do
2
+ describe '.add_default_parameters' do
3
+ subject { Hubspot::ContactProperties.add_default_parameters({}) }
4
+ context "default parameters" do
5
+ its([:property]){ should == "email" }
6
+ end
7
+ end
8
+ end
@@ -38,8 +38,8 @@ describe Hubspot::Contact do
38
38
  context "with an existing email" do
39
39
  cassette "contact_create_existing_email"
40
40
  let(:email){ "testingapis@hubspot.com" }
41
- it "raises a ContactExistsError" do
42
- expect{ subject }.to raise_error Hubspot::ContactExistsError
41
+ it "raises a RequestError" do
42
+ expect{ subject }.to raise_error Hubspot::RequestError
43
43
  end
44
44
  end
45
45
  context "with an invalid email" do
@@ -52,89 +52,145 @@ describe Hubspot::Contact do
52
52
  end
53
53
 
54
54
  describe ".find_by_email" do
55
- cassette "contact_find_by_email"
56
- subject{ Hubspot::Contact.find_by_email(email) }
55
+ context 'given an uniq email' do
56
+ cassette "contact_find_by_email"
57
+ subject{ Hubspot::Contact.find_by_email(email) }
58
+
59
+ context "when the contact is found" do
60
+ let(:email){ "testingapis@hubspot.com" }
61
+ it{ should be_an_instance_of Hubspot::Contact }
62
+ its(:vid){ should == 82325 }
63
+ end
57
64
 
58
- context "when the contact is found" do
59
- let(:email){ "testingapis@hubspot.com" }
60
- it{ should be_an_instance_of Hubspot::Contact }
61
- its(:vid){ should == 82325 }
65
+ context "when the contact cannot be found" do
66
+ it 'raises an error' do
67
+ expect { Hubspot::Contact.find_by_email('notacontact@test.com') }.to raise_error(Hubspot::RequestError)
68
+ end
69
+ end
62
70
  end
63
71
 
64
- context "when the contact cannot be found" do
65
- let(:email){ "notacontact@test.com" }
66
- it{ should be_nil }
72
+ context 'batch mode' do
73
+ cassette "contact_find_by_email_batch_mode"
74
+
75
+ it 'find lists of contacts' do
76
+ emails = ['testingapis@hubspot.com', 'testingapisawesomeandstuff@hubspot.com']
77
+ contacts = Hubspot::Contact.find_by_email(emails)
78
+ pending
79
+ end
67
80
  end
68
81
  end
69
82
 
70
83
  describe ".find_by_id" do
71
- cassette "contact_find_by_id"
72
- subject{ Hubspot::Contact.find_by_id(vid) }
84
+ context 'given an uniq id' do
85
+ cassette "contact_find_by_id"
86
+ subject{ Hubspot::Contact.find_by_id(vid) }
87
+
88
+ context "when the contact is found" do
89
+ let(:vid){ 82325 }
90
+ it{ should be_an_instance_of Hubspot::Contact }
91
+ its(:email){ should == "testingapis@hubspot.com" }
92
+ end
73
93
 
74
- context "when the contact is found" do
75
- let(:vid){ 82325 }
76
- it{ should be_an_instance_of Hubspot::Contact }
77
- its(:email){ should == "testingapis@hubspot.com" }
94
+ context "when the contact cannot be found" do
95
+ it 'raises an error' do
96
+ expect { Hubspot::Contact.find_by_id(9999999) }.to raise_error(Hubspot::RequestError)
97
+ end
98
+ end
78
99
  end
79
100
 
80
- context "when the contact cannot be found" do
81
- let(:vid){ 9999999 }
82
- it{ should be_nil }
101
+ context 'batch mode' do
102
+ cassette "contact_find_by_id_batch_mode"
103
+
104
+ # NOTE: error currently appends on API endpoint
105
+ it 'find lists of contacts' do
106
+ expect { Hubspot::Contact.find_by_id([82325]) }.to raise_error(Hubspot::ApiError)
107
+ end
83
108
  end
84
109
  end
85
110
 
86
111
  describe ".find_by_utk" do
87
- cassette "contact_find_by_utk"
88
- subject{ Hubspot::Contact.find_by_utk(utk) }
112
+ context 'given an uniq utk' do
113
+ cassette "contact_find_by_utk"
114
+ subject{ Hubspot::Contact.find_by_utk(utk) }
115
+
116
+ context "when the contact is found" do
117
+ let(:utk){ "f844d2217850188692f2610c717c2e9b" }
118
+ it{ should be_an_instance_of Hubspot::Contact }
119
+ its(:utk){ should == "f844d2217850188692f2610c717c2e9b" }
120
+ end
89
121
 
90
- context "when the contact is found" do
91
- let(:utk){ "f844d2217850188692f2610c717c2e9b" }
92
- it{ should be_an_instance_of Hubspot::Contact }
93
- its(:utk){ should == "f844d2217850188692f2610c717c2e9b" }
122
+ context "when the contact cannot be found" do
123
+ it 'raises an error' do
124
+ expect { Hubspot::Contact.find_by_utk("invalid") }.to raise_error(Hubspot::RequestError)
125
+ end
126
+ end
94
127
  end
95
128
 
96
- context "when the contact cannot be found" do
97
- let(:utk){ "invalid" }
98
- it{ should be_nil }
129
+ context 'batch mode' do
130
+ cassette "contact_find_by_utk_batch_mode"
131
+
132
+ it 'find lists of contacts' do
133
+ utks = ['f844d2217850188692f2610c717c2e9b', 'j94344d22178501692f2610c717c2e9b']
134
+ expect { Hubspot::Contact.find_by_utk(utks) }.to raise_error(Hubspot::ApiError)
135
+ end
99
136
  end
100
137
  end
101
138
 
102
139
 
103
140
  describe '.all' do
104
- cassette 'find_all_contacts'
141
+ context 'all contacts' do
142
+ cassette 'find_all_contacts'
105
143
 
106
- it 'must get the contacts list' do
107
- contacts = Hubspot::Contact.all
144
+ it 'must get the contacts list' do
145
+ contacts = Hubspot::Contact.all
108
146
 
109
- expect(contacts.size).to eql 20 # defautl page size
147
+ expect(contacts.size).to eql 20 # default page size
110
148
 
111
- first = contacts.first
112
- last = contacts.last
149
+ first = contacts.first
150
+ last = contacts.last
113
151
 
114
- expect(first.vid).to eql 154835
115
- expect(first['firstname']).to eql 'HubSpot'
116
- expect(first['lastname']).to eql 'Test'
152
+ expect(first).to be_a Hubspot::Contact
153
+ expect(first.vid).to eql 154835
154
+ expect(first['firstname']).to eql 'HubSpot'
155
+ expect(first['lastname']).to eql 'Test'
117
156
 
118
- expect(last).to be_a Hubspot::Contact
119
- expect(last.vid).to eql 196199
120
- expect(last['firstname']).to eql 'Eleanor'
121
- expect(last['lastname']).to eql 'Morgan'
122
- end
157
+ expect(last).to be_a Hubspot::Contact
158
+ expect(last.vid).to eql 196199
159
+ expect(last['firstname']).to eql 'Eleanor'
160
+ expect(last['lastname']).to eql 'Morgan'
161
+ end
123
162
 
124
- it 'must filter only 2 contacts' do
125
- contacts = Hubspot::Contact.all(count: 2)
126
- expect(contacts.size).to eql 2
163
+ it 'must filter only 2 contacts' do
164
+ contacts = Hubspot::Contact.all(count: 2)
165
+ expect(contacts.size).to eql 2
166
+ end
167
+
168
+ it 'it must offset the contacts' do
169
+ single_list = Hubspot::Contact.all(count: 1)
170
+ expect(single_list.size).to eql 1
171
+ first = single_list.first
172
+
173
+ second = Hubspot::Contact.all(count: 1, vidOffset: first.vid).first
174
+ expect(second.vid).to eql 196181
175
+ expect(second['firstname']).to eql 'Charles'
176
+ expect(second['lastname']).to eql 'Gowland'
177
+ end
127
178
  end
128
179
 
129
- it 'it must offset the contacts' do
130
- single_list = Hubspot::Contact.all(count: 1)
131
- expect(single_list.size).to eql 1
132
- first = single_list.first
180
+ context 'recent contacts' do
181
+ cassette 'find_all_recent_contacts'
133
182
 
134
- second = Hubspot::Contact.all(count: 1, 'vidOffset': first.vid).first
135
- expect(second.vid).to eql 196181
136
- expect(second['firstname']).to eql 'Charles'
137
- expect(second['lastname']).to eql 'Gowland'
183
+ it 'must get the contacts list' do
184
+ contacts = Hubspot::Contact.all(recent: true)
185
+ expect(contacts.size).to eql 20
186
+
187
+ first, last = contacts.first, contacts.last
188
+ expect(first).to be_a Hubspot::Contact
189
+ expect(first.vid).to eql 263794
190
+
191
+ expect(last).to be_a Hubspot::Contact
192
+ expect(last.vid).to eql 263776
193
+ end
138
194
  end
139
195
  end
140
196