amplitude-api 0.1.1 → 0.3.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -1,10 +1,12 @@
1
- require 'spec_helper'
1
+ # frozen_string_literal: true
2
+
3
+ require "spec_helper"
2
4
 
3
5
  describe AmplitudeAPI::Identification do
4
6
  user = Struct.new(:id)
5
7
 
6
- context 'with a user object' do
7
- describe '#body' do
8
+ context "with a user object" do
9
+ describe "#body" do
8
10
  it "populates with the user's id" do
9
11
  identification = described_class.new(user_id: user.new(123))
10
12
  expect(identification.to_hash[:user_id]).to eq(123)
@@ -12,8 +14,8 @@ describe AmplitudeAPI::Identification do
12
14
  end
13
15
  end
14
16
 
15
- context 'with a user id' do
16
- describe '#body' do
17
+ context "with a user id" do
18
+ describe "#body" do
17
19
  it "populates with the user's id" do
18
20
  identification = described_class.new(user_id: 123)
19
21
  expect(identification.to_hash[:user_id]).to eq(123)
@@ -21,35 +23,35 @@ describe AmplitudeAPI::Identification do
21
23
  end
22
24
  end
23
25
 
24
- context 'without a user' do
25
- describe '#body' do
26
- it 'populates with the unknown user' do
26
+ context "without a user" do
27
+ describe "#body" do
28
+ it "populates with the unknown user" do
27
29
  identification = described_class.new(user_id: nil)
28
30
  expect(identification.to_hash[:user_id]).to eq(AmplitudeAPI::USER_WITH_NO_ACCOUNT)
29
31
  end
30
32
  end
31
33
  end
32
34
 
33
- describe '#body' do
34
- it 'includes the user id' do
35
+ describe "#body" do
36
+ it "includes the user id" do
35
37
  identification = described_class.new(user_id: 123)
36
38
  expect(identification.to_hash[:user_id]).to eq(123)
37
39
  end
38
40
 
39
- it 'includes the device id' do
40
- identification = described_class.new(user_id: 123, device_id: 'abc')
41
- expect(identification.to_hash[:device_id]).to eq('abc')
41
+ it "includes the device id" do
42
+ identification = described_class.new(user_id: 123, device_id: "abc")
43
+ expect(identification.to_hash[:device_id]).to eq("abc")
42
44
  end
43
45
 
44
- it 'includes arbitrary user properties' do
46
+ it "includes arbitrary user properties" do
45
47
  identification = described_class.new(
46
48
  user_id: 123,
47
49
  user_properties: {
48
- first_name: 'John',
49
- last_name: 'Doe'
50
+ first_name: "John",
51
+ last_name: "Doe"
50
52
  }
51
53
  )
52
- expect(identification.to_hash[:user_properties]).to eq(first_name: 'John', last_name: 'Doe')
54
+ expect(identification.to_hash[:user_properties]).to eq(first_name: "John", last_name: "Doe")
53
55
  end
54
56
  end
55
57
  end
@@ -1,95 +1,101 @@
1
- require 'spec_helper'
1
+ # frozen_string_literal: true
2
+
3
+ require "spec_helper"
2
4
 
3
5
  describe AmplitudeAPI do
4
6
  let(:user) { Struct.new(:id).new(123) }
5
- let(:device_id) { 'abcdef' }
7
+ let(:device_id) { "abcdef" }
6
8
 
7
- describe '.track' do
8
- context 'with a single event' do
9
- context 'with only user_id' do
10
- it 'sends the event to Amplitude' do
9
+ describe ".track" do
10
+ context "with a single event" do
11
+ context "with only user_id" do
12
+ it "sends the event to Amplitude" do
11
13
  event = AmplitudeAPI::Event.new(
12
14
  user_id: 123,
13
- event_type: 'clicked on sign up'
15
+ event_type: "clicked on sign up"
14
16
  )
15
- body = {
17
+ body = JSON.generate(
16
18
  api_key: described_class.api_key,
17
- event: JSON.generate([event.to_hash])
18
- }
19
+ events: [event.to_hash]
20
+ )
21
+ headers = { "Content-Type" => "application/json" }
19
22
 
20
- expect(Typhoeus).to receive(:post).with(AmplitudeAPI::TRACK_URI_STRING, body: body)
23
+ expect(Faraday).to receive(:post).with(AmplitudeAPI::TRACK_URI_STRING, body, headers)
21
24
 
22
25
  described_class.track(event)
23
26
  end
24
27
  end
25
28
 
26
- context 'with only device_id' do
27
- it 'sends the event to Amplitude' do
29
+ context "with only device_id" do
30
+ it "sends the event to Amplitude" do
28
31
  event = AmplitudeAPI::Event.new(
29
32
  device_id: device_id,
30
- event_type: 'clicked on sign up'
33
+ event_type: "clicked on sign up"
31
34
  )
32
- body = {
35
+ body = JSON.generate(
33
36
  api_key: described_class.api_key,
34
- event: JSON.generate([event.to_hash])
35
- }
37
+ events: [event.to_hash]
38
+ )
39
+ headers = { "Content-Type" => "application/json" }
36
40
 
37
- expect(Typhoeus).to receive(:post).with(AmplitudeAPI::TRACK_URI_STRING, body: body)
41
+ expect(Faraday).to receive(:post).with(AmplitudeAPI::TRACK_URI_STRING, body, headers)
38
42
 
39
43
  described_class.track(event)
40
44
  end
41
45
  end
42
46
 
43
- context 'with both user_id and device_id' do
44
- it 'sends the event to Amplitude' do
47
+ context "with both user_id and device_id" do
48
+ it "sends the event to Amplitude" do
45
49
  event = AmplitudeAPI::Event.new(
46
50
  user_id: 123,
47
51
  device_id: device_id,
48
- event_type: 'clicked on sign up'
52
+ event_type: "clicked on sign up"
49
53
  )
50
- body = {
54
+ body = JSON.generate(
51
55
  api_key: described_class.api_key,
52
- event: JSON.generate([event.to_hash])
53
- }
56
+ events: [event.to_hash]
57
+ )
58
+ headers = { "Content-Type" => "application/json" }
54
59
 
55
- expect(Typhoeus).to receive(:post).with(AmplitudeAPI::TRACK_URI_STRING, body: body)
60
+ expect(Faraday).to receive(:post).with(AmplitudeAPI::TRACK_URI_STRING, body, headers)
56
61
 
57
62
  described_class.track(event)
58
63
  end
59
64
  end
60
65
  end
61
66
 
62
- context 'with multiple events' do
63
- it 'sends all events in a single request' do
67
+ context "with multiple events" do
68
+ it "sends all events in a single request" do
64
69
  event = AmplitudeAPI::Event.new(
65
70
  user_id: 123,
66
- event_type: 'clicked on sign up'
71
+ event_type: "clicked on sign up"
67
72
  )
68
73
  event2 = AmplitudeAPI::Event.new(
69
74
  user_id: 456,
70
- event_type: 'liked a widget'
75
+ event_type: "liked a widget"
71
76
  )
72
- body = {
77
+ body = JSON.generate(
73
78
  api_key: described_class.api_key,
74
- event: JSON.generate([event.to_hash, event2.to_hash])
75
- }
79
+ events: [event.to_hash, event2.to_hash]
80
+ )
81
+ headers = { "Content-Type" => "application/json" }
76
82
 
77
- expect(Typhoeus).to receive(:post).with(AmplitudeAPI::TRACK_URI_STRING, body: body)
83
+ expect(Faraday).to receive(:post).with(AmplitudeAPI::TRACK_URI_STRING, body, headers)
78
84
 
79
85
  described_class.track([event, event2])
80
86
  end
81
87
  end
82
88
  end
83
89
 
84
- describe '.identify' do
85
- context 'with a single identification' do
86
- context 'with only user_id' do
87
- it 'sends the identification to Amplitude' do
90
+ describe ".identify" do
91
+ context "with a single identification" do
92
+ context "with only user_id" do
93
+ it "sends the identification to Amplitude" do
88
94
  identification = AmplitudeAPI::Identification.new(
89
95
  user_id: 123,
90
96
  user_properties: {
91
- first_name: 'John',
92
- last_name: 'Doe'
97
+ first_name: "John",
98
+ last_name: "Doe"
93
99
  }
94
100
  )
95
101
  body = {
@@ -97,19 +103,19 @@ describe AmplitudeAPI do
97
103
  identification: JSON.generate([identification.to_hash])
98
104
  }
99
105
 
100
- expect(Typhoeus).to receive(:post).with(AmplitudeAPI::IDENTIFY_URI_STRING, body: body)
106
+ expect(Faraday).to receive(:post).with(AmplitudeAPI::IDENTIFY_URI_STRING, body)
101
107
 
102
108
  described_class.identify(identification)
103
109
  end
104
110
  end
105
111
 
106
- context 'with only device_id' do
107
- it 'sends the identification to Amplitude' do
112
+ context "with only device_id" do
113
+ it "sends the identification to Amplitude" do
108
114
  identification = AmplitudeAPI::Identification.new(
109
115
  device_id: device_id,
110
116
  user_properties: {
111
- first_name: 'John',
112
- last_name: 'Doe'
117
+ first_name: "John",
118
+ last_name: "Doe"
113
119
  }
114
120
  )
115
121
  body = {
@@ -117,20 +123,20 @@ describe AmplitudeAPI do
117
123
  identification: JSON.generate([identification.to_hash])
118
124
  }
119
125
 
120
- expect(Typhoeus).to receive(:post).with(AmplitudeAPI::IDENTIFY_URI_STRING, body: body)
126
+ expect(Faraday).to receive(:post).with(AmplitudeAPI::IDENTIFY_URI_STRING, body)
121
127
 
122
128
  described_class.identify(identification)
123
129
  end
124
130
  end
125
131
 
126
- context 'with both user_id and device_id' do
127
- it 'sends the identification to Amplitude' do
132
+ context "with both user_id and device_id" do
133
+ it "sends the identification to Amplitude" do
128
134
  identification = AmplitudeAPI::Identification.new(
129
135
  user_id: 123,
130
136
  device_id: device_id,
131
137
  user_properties: {
132
- first_name: 'John',
133
- last_name: 'Doe'
138
+ first_name: "John",
139
+ last_name: "Doe"
134
140
  }
135
141
  )
136
142
  body = {
@@ -138,27 +144,27 @@ describe AmplitudeAPI do
138
144
  identification: JSON.generate([identification.to_hash])
139
145
  }
140
146
 
141
- expect(Typhoeus).to receive(:post).with(AmplitudeAPI::IDENTIFY_URI_STRING, body: body)
147
+ expect(Faraday).to receive(:post).with(AmplitudeAPI::IDENTIFY_URI_STRING, body)
142
148
 
143
149
  described_class.identify(identification)
144
150
  end
145
151
  end
146
152
  end
147
153
 
148
- context 'with multiple identifications' do
149
- it 'sends all identifications in a single request' do
154
+ context "with multiple identifications" do
155
+ it "sends all identifications in a single request" do
150
156
  identification = AmplitudeAPI::Identification.new(
151
157
  user_id: 123,
152
158
  user_properties: {
153
- first_name: 'Julian',
154
- last_name: 'Ponce'
159
+ first_name: "Julian",
160
+ last_name: "Ponce"
155
161
  }
156
162
  )
157
163
  identification2 = AmplitudeAPI::Identification.new(
158
164
  device_id: 456,
159
165
  user_properties: {
160
- first_name: 'John',
161
- last_name: 'Doe'
166
+ first_name: "John",
167
+ last_name: "Doe"
162
168
  }
163
169
  )
164
170
  body = {
@@ -166,369 +172,415 @@ describe AmplitudeAPI do
166
172
  identification: JSON.generate([identification.to_hash, identification2.to_hash])
167
173
  }
168
174
 
169
- expect(Typhoeus).to receive(:post).with(AmplitudeAPI::IDENTIFY_URI_STRING, body: body)
175
+ expect(Faraday).to receive(:post).with(AmplitudeAPI::IDENTIFY_URI_STRING, body)
170
176
 
171
177
  described_class.identify([identification, identification2])
172
178
  end
173
179
  end
174
180
  end
175
181
 
176
- describe '.initializer' do
182
+ describe ".initializer" do
177
183
  let(:attributes) do
178
184
  {
179
185
  user_id: 123,
180
- event_type: 'test_event',
186
+ event_type: "test_event",
181
187
  event_properties: {
182
188
  test_property: 1
183
189
  },
184
190
  user_properties: {},
185
- ip: '8.8.8.8'
191
+ ip: "8.8.8.8"
186
192
  }
187
193
  end
188
194
 
189
- it 'requires event type' do
195
+ it "requires event type" do
190
196
  attributes.delete(:event_type)
191
197
  expect { AmplitudeAPI::Event.new(attributes) }.to raise_error(ArgumentError)
192
198
  end
193
199
 
194
- it 'requires user id or device id' do
200
+ it "requires user id or device id" do
195
201
  expect(AmplitudeAPI::Event.new(attributes).to_h).to eq(attributes)
196
- attributes.merge!(device_id: 'abc').delete(:user_id)
202
+ attributes.merge!(device_id: "abc").delete(:user_id)
197
203
  expect(AmplitudeAPI::Event.new(attributes).to_h).to eq(attributes)
198
204
  attributes.delete(:device_id)
199
205
  expect { AmplitudeAPI::Event.new(attributes) }.to raise_error(ArgumentError)
200
206
  end
201
207
 
202
- it 'initializes event with parameter' do
208
+ it "initializes event with parameter" do
203
209
  expect(AmplitudeAPI::Event.new(attributes)).to eq(attributes)
204
210
  end
205
211
  end
206
212
 
207
- describe '.send_event' do
208
- context 'with only user_id' do
209
- it 'sends an event to AmplitudeAPI' do
213
+ describe ".send_event" do
214
+ context "with only user_id" do
215
+ it "sends an event to AmplitudeAPI" do
210
216
  event = AmplitudeAPI::Event.new(
211
217
  user_id: user,
212
- event_type: 'test_event',
218
+ event_type: "test_event",
213
219
  event_properties: { test_property: 1 }
214
220
  )
215
221
  expect(described_class).to receive(:track).with(event)
216
222
 
217
- described_class.send_event('test_event', user, nil, event_properties: { test_property: 1 })
223
+ described_class.send_event("test_event", user, nil, event_properties: { test_property: 1 })
218
224
  end
219
225
 
220
- context 'the user is nil' do
221
- it 'sends an event with the no account user' do
226
+ context "the user is nil" do
227
+ it "sends an event with the no account user" do
222
228
  event = AmplitudeAPI::Event.new(
223
229
  user_id: nil,
224
- event_type: 'test_event',
230
+ event_type: "test_event",
225
231
  event_properties: { test_property: 1 }
226
232
  )
227
233
  expect(described_class).to receive(:track).with(event)
228
234
 
229
- described_class.send_event('test_event', nil, nil, event_properties: { test_property: 1 })
235
+ described_class.send_event("test_event", nil, nil, event_properties: { test_property: 1 })
230
236
  end
231
237
  end
232
238
 
233
- context 'the user is a user_id' do
234
- it 'sends an event to AmplitudeAPI' do
239
+ context "the user is a user_id" do
240
+ it "sends an event to AmplitudeAPI" do
235
241
  event = AmplitudeAPI::Event.new(
236
242
  user_id: 123,
237
- event_type: 'test_event',
243
+ event_type: "test_event",
238
244
  event_properties: { test_property: 1 }
239
245
  )
240
246
  expect(described_class).to receive(:track).with(event)
241
247
 
242
- described_class.send_event('test_event', user.id, nil, event_properties: { test_property: 1 })
248
+ described_class.send_event("test_event", user.id, nil, event_properties: { test_property: 1 })
243
249
  end
244
250
 
245
- it 'sends arbitrary user_properties to AmplitudeAPI' do
251
+ it "sends arbitrary user_properties to AmplitudeAPI" do
246
252
  event = AmplitudeAPI::Event.new(
247
253
  user_id: 123,
248
- event_type: 'test_event',
254
+ event_type: "test_event",
249
255
  event_properties: { test_property: 1 },
250
- user_properties: { test_user_property: 'abc' }
256
+ user_properties: { test_user_property: "abc" }
251
257
  )
252
258
  expect(described_class).to receive(:track).with(event)
253
259
 
254
260
  described_class.send_event(
255
- 'test_event',
261
+ "test_event",
256
262
  user.id,
257
263
  nil,
258
264
  event_properties: { test_property: 1 },
259
- user_properties: { test_user_property: 'abc' }
265
+ user_properties: { test_user_property: "abc" }
260
266
  )
261
267
  end
262
268
  end
263
269
  end
264
270
 
265
- context 'with device_id' do
266
- context 'the user is not nil' do
267
- it 'sends an event to AmplitudeAPI' do
271
+ context "with device_id" do
272
+ context "the user is not nil" do
273
+ it "sends an event to AmplitudeAPI" do
268
274
  event = AmplitudeAPI::Event.new(
269
275
  user_id: user,
270
276
  device_id: device_id,
271
- event_type: 'test_event',
277
+ event_type: "test_event",
272
278
  event_properties: { test_property: 1 }
273
279
  )
274
280
  expect(described_class).to receive(:track).with(event)
275
281
 
276
- described_class.send_event('test_event', user, device_id, event_properties: { test_property: 1 })
282
+ described_class.send_event("test_event", user, device_id, event_properties: { test_property: 1 })
277
283
  end
278
284
  end
279
285
 
280
- context 'the user is nil' do
281
- it 'sends an event with the no account user' do
286
+ context "the user is nil" do
287
+ it "sends an event with the no account user" do
282
288
  event = AmplitudeAPI::Event.new(
283
289
  user_id: nil,
284
290
  device_id: device_id,
285
- event_type: 'test_event',
291
+ event_type: "test_event",
286
292
  event_properties: { test_property: 1 }
287
293
  )
288
294
  expect(described_class).to receive(:track).with(event)
289
295
 
290
- described_class.send_event('test_event', nil, device_id, event_properties: { test_property: 1 })
296
+ described_class.send_event("test_event", nil, device_id, event_properties: { test_property: 1 })
291
297
  end
292
298
  end
293
299
  end
294
300
  end
295
301
 
296
- describe '.send_identify' do
297
- context 'with no device_id' do
298
- it 'sends an identify to AmplitudeAPI' do
302
+ describe ".send_identify" do
303
+ context "with no device_id" do
304
+ it "sends an identify to AmplitudeAPI" do
299
305
  identification = AmplitudeAPI::Identification.new(
300
306
  user_id: user,
301
307
  user_properties: {
302
- first_name: 'John',
303
- last_name: 'Doe'
308
+ first_name: "John",
309
+ last_name: "Doe"
304
310
  }
305
311
  )
306
312
  expect(described_class).to receive(:identify).with(identification)
307
313
 
308
- described_class.send_identify(user, nil, first_name: 'John', last_name: 'Doe')
314
+ described_class.send_identify(user, nil, first_name: "John", last_name: "Doe")
309
315
  end
310
316
 
311
- context 'the user is nil' do
312
- it 'sends an identify with the no account user' do
317
+ context "the user is nil" do
318
+ it "sends an identify with the no account user" do
313
319
  identification = AmplitudeAPI::Identification.new(
314
320
  user_id: nil,
315
321
  user_properties: {
316
- first_name: 'John',
317
- last_name: 'Doe'
322
+ first_name: "John",
323
+ last_name: "Doe"
318
324
  }
319
325
  )
320
326
  expect(described_class).to receive(:identify).with(identification)
321
327
 
322
- described_class.send_identify(nil, nil, first_name: 'John', last_name: 'Doe')
328
+ described_class.send_identify(nil, nil, first_name: "John", last_name: "Doe")
323
329
  end
324
330
  end
325
331
 
326
- context 'the user is a user_id' do
327
- it 'sends an identify to AmplitudeAPI' do
332
+ context "the user is a user_id" do
333
+ it "sends an identify to AmplitudeAPI" do
328
334
  identification = AmplitudeAPI::Identification.new(
329
335
  user_id: 123,
330
336
  user_properties: {
331
- first_name: 'John',
332
- last_name: 'Doe'
337
+ first_name: "John",
338
+ last_name: "Doe"
333
339
  }
334
340
  )
335
341
  expect(described_class).to receive(:identify).with(identification)
336
342
 
337
- described_class.send_identify(user.id, nil, first_name: 'John', last_name: 'Doe')
343
+ described_class.send_identify(user.id, nil, first_name: "John", last_name: "Doe")
338
344
  end
339
345
  end
340
346
  end
341
347
 
342
- context 'with a device_id' do
343
- it 'sends an identify to AmplitudeAPI' do
348
+ context "with a device_id" do
349
+ it "sends an identify to AmplitudeAPI" do
344
350
  identification = AmplitudeAPI::Identification.new(
345
351
  user_id: user,
346
- device_id: 'abc',
352
+ device_id: "abc",
347
353
  user_properties: {
348
- first_name: 'John',
349
- last_name: 'Doe'
354
+ first_name: "John",
355
+ last_name: "Doe"
350
356
  }
351
357
  )
352
358
  expect(described_class).to receive(:identify).with(identification)
353
359
 
354
- described_class.send_identify(user, 'abc', first_name: 'John', last_name: 'Doe')
360
+ described_class.send_identify(user, "abc", first_name: "John", last_name: "Doe")
355
361
  end
356
362
  end
357
363
  end
358
364
 
359
- describe '.segmentation' do
365
+ describe ".segmentation" do
360
366
  let(:end_time) { Time.now }
361
367
  let(:start_time) { end_time - 60 * 60 * 24 } # -1 day
362
368
 
363
- it 'sends request to Amplitude' do
364
- expect(Typhoeus).to receive(:get).with(AmplitudeAPI::SEGMENTATION_URI_STRING,
365
- userpwd: "#{described_class.api_key}:#{described_class.secret_key}",
366
- params: {
367
- e: { event_type: 'my event' }.to_json,
368
- start: start_time.strftime('%Y%m%d'),
369
- end: end_time.strftime('%Y%m%d'),
370
- s: [{ prop: 'foo', op: 'is', values: %w[bar] }.to_json]
371
- })
372
-
373
- described_class.segmentation({ event_type: 'my event' }, start_time, end_time,
374
- s: [{ prop: 'foo', op: 'is', values: %w[bar] }])
369
+ it "sends request to Amplitude" do
370
+ expect(Faraday).to receive(:get).with(AmplitudeAPI::SEGMENTATION_URI_STRING,
371
+ userpwd: "#{described_class.api_key}:#{described_class.secret_key}",
372
+ params: {
373
+ e: { event_type: "my event" }.to_json,
374
+ start: start_time.strftime("%Y%m%d"),
375
+ end: end_time.strftime("%Y%m%d"),
376
+ s: [{ prop: "foo", op: "is", values: %w[bar] }.to_json]
377
+ })
378
+
379
+ described_class.segmentation({ event_type: "my event" }, start_time, end_time,
380
+ s: [{ prop: "foo", op: "is", values: %w[bar] }])
375
381
  end
376
382
  end
377
383
 
378
- describe '.delete' do
379
- context 'a single user_id' do
380
- it 'correctly formats the response' do
384
+ describe ".delete" do
385
+ let(:connection) { instance_double("Faraday::Connection", post: nil, basic_auth: nil) }
386
+
387
+ before do
388
+ allow(Faraday).to receive(:new).and_yield(connection).and_return(connection)
389
+ allow(Faraday).to receive(:post)
390
+ end
391
+
392
+ it "sends the authentification" do
393
+ api_key = described_class.config.api_key
394
+ secret_key = described_class.config.secret_key
395
+
396
+ described_class.delete(user_ids: "123")
397
+
398
+ expect(connection).to have_received(:basic_auth).with(api_key, secret_key)
399
+ end
400
+
401
+ it "sends the requester" do
402
+ requester = "privacy@gethopscotch.com"
403
+ body = {
404
+ amplitude_ids: ["123"],
405
+ requester: requester
406
+ }
407
+
408
+ described_class.delete(amplitude_ids: "123", requester: requester)
409
+
410
+ expect(connection).to have_received(:post).with(
411
+ AmplitudeAPI::DELETION_URI_STRING,
412
+ JSON.generate(body),
413
+ "Content-Type" => "application/json"
414
+ )
415
+ end
416
+
417
+ it "sends the ignore_invalid_id flag" do
418
+ body = {
419
+ user_ids: ["123"],
420
+ ignore_invalid_id: "true"
421
+ }
422
+
423
+ described_class.delete(user_ids: "123", ignore_invalid_id: true)
424
+
425
+ expect(connection).to have_received(:post).with(
426
+ AmplitudeAPI::DELETION_URI_STRING,
427
+ JSON.generate(body),
428
+ "Content-Type" => "application/json"
429
+ )
430
+ end
431
+
432
+ it "sends the delete_from_org flag" do
433
+ body = {
434
+ user_ids: ["123"],
435
+ delete_from_org: "true"
436
+ }
437
+
438
+ described_class.delete(user_ids: "123", delete_from_org: true)
439
+
440
+ expect(connection).to have_received(:post).with(
441
+ AmplitudeAPI::DELETION_URI_STRING,
442
+ JSON.generate(body),
443
+ "Content-Type" => "application/json"
444
+ )
445
+ end
446
+
447
+ context "with a single user" do
448
+ it "sends the user_id to Amplitude" do
381
449
  body = {
382
- user_ids: ['123']
450
+ user_ids: ["123"]
383
451
  }
384
452
 
385
- expect(Typhoeus).to receive(:post).with(
453
+ described_class.delete(user_ids: "123")
454
+
455
+ expect(connection).to have_received(:post).with(
386
456
  AmplitudeAPI::DELETION_URI_STRING,
387
- userpwd: "#{described_class.api_key}:#{described_class.config.secret_key}",
388
- body: JSON.generate(body),
389
- headers: { 'Content-Type': 'application/json' }
457
+ JSON.generate(body),
458
+ "Content-Type" => "application/json"
390
459
  )
391
- described_class.delete(user_ids: '123')
392
460
  end
393
- end
394
461
 
395
- context 'with user_ids' do
396
- it 'sends the deletion to Amplitude' do
397
- user_ids = [123, 456, 555]
462
+ it "sends the amplitude_id to Amplitude" do
398
463
  body = {
399
- user_ids: user_ids
464
+ amplitude_ids: ["123"]
400
465
  }
401
466
 
402
- expect(Typhoeus).to receive(:post).with(
467
+ described_class.delete(amplitude_ids: "123")
468
+
469
+ expect(connection).to have_received(:post).with(
403
470
  AmplitudeAPI::DELETION_URI_STRING,
404
- userpwd: "#{described_class.api_key}:#{described_class.config.secret_key}",
405
- body: JSON.generate(body),
406
- headers: { 'Content-Type': 'application/json' }
471
+ JSON.generate(body),
472
+ "Content-Type" => "application/json"
407
473
  )
408
- described_class.delete(user_ids: user_ids)
409
474
  end
410
475
 
411
- context 'with amplitude_ids' do
412
- it 'sends the deletion to Amplitude' do
413
- user_ids = [123, 456, 555]
414
- amplitude_ids = [122, 456]
415
- body = {
416
- amplitude_ids: amplitude_ids,
417
- user_ids: user_ids
418
- }
476
+ it "sends both user_id and amplitude_id to Amplitude" do
477
+ body = {
478
+ amplitude_ids: ["123"],
479
+ user_ids: ["456"]
480
+ }
419
481
 
420
- expect(Typhoeus).to receive(:post).with(
421
- AmplitudeAPI::DELETION_URI_STRING,
422
- userpwd: "#{described_class.api_key}:#{described_class.config.secret_key}",
423
- body: JSON.generate(body),
424
- headers: { 'Content-Type': 'application/json' }
425
- )
426
- described_class.delete(
427
- amplitude_ids: amplitude_ids,
428
- user_ids: user_ids
429
- )
430
- end
482
+ described_class.delete(user_ids: "456", amplitude_ids: "123")
483
+
484
+ expect(connection).to have_received(:post).with(
485
+ AmplitudeAPI::DELETION_URI_STRING,
486
+ JSON.generate(body),
487
+ "Content-Type" => "application/json"
488
+ )
431
489
  end
432
490
  end
433
491
 
434
- context 'with amplitude_ids' do
435
- it 'sends the deletion to Amplitude' do
436
- amplitude_ids = [122, 456]
492
+ context "with multiple user_ids" do
493
+ it "sends the user_ids to Amplitude" do
494
+ user_ids = [123, 456, 555]
437
495
  body = {
438
- amplitude_ids: amplitude_ids
496
+ user_ids: user_ids
439
497
  }
440
498
 
441
- expect(Typhoeus).to receive(:post).with(
499
+ described_class.delete(user_ids: user_ids)
500
+
501
+ expect(connection).to have_received(:post).with(
442
502
  AmplitudeAPI::DELETION_URI_STRING,
443
- userpwd: "#{described_class.api_key}:#{described_class.config.secret_key}",
444
- body: JSON.generate(body),
445
- headers: { 'Content-Type': 'application/json' }
503
+ JSON.generate(body),
504
+ "Content-Type" => "application/json"
446
505
  )
447
- described_class.delete(amplitude_ids: amplitude_ids)
448
506
  end
449
- end
450
507
 
451
- context 'a single amplitude_id' do
452
- it 'correctly formats the response' do
508
+ it "sends the amplitude_ids to Amplitude" do
509
+ amplitude_ids = [122, 456]
453
510
  body = {
454
- amplitude_ids: [122]
511
+ amplitude_ids: amplitude_ids
455
512
  }
456
513
 
457
- expect(Typhoeus).to receive(:post).with(
514
+ described_class.delete(amplitude_ids: amplitude_ids)
515
+
516
+ expect(connection).to have_received(:post).with(
458
517
  AmplitudeAPI::DELETION_URI_STRING,
459
- userpwd: "#{described_class.api_key}:#{described_class.config.secret_key}",
460
- body: JSON.generate(body),
461
- headers: { 'Content-Type': 'application/json' }
518
+ JSON.generate(body),
519
+ "Content-Type" => "application/json"
462
520
  )
463
- described_class.delete(amplitude_ids: 122)
464
521
  end
465
- end
466
522
 
467
- context 'with requester' do
468
- it 'sends the deletion to Amplitude' do
523
+ it "sends both user_ids and amplitude_ids to Amplitude" do
524
+ user_ids = [123, 456, 555]
469
525
  amplitude_ids = [122, 456]
470
-
471
526
  body = {
472
527
  amplitude_ids: amplitude_ids,
473
- requester: 'privacy@gethopscotch.com'
528
+ user_ids: user_ids
474
529
  }
475
- userpwd = "#{described_class.api_key}:#{described_class.config.secret_key}"
476
530
 
477
- expect(Typhoeus).to receive(:post).with(
531
+ described_class.delete(user_ids: user_ids, amplitude_ids: amplitude_ids)
532
+
533
+ expect(connection).to have_received(:post).with(
478
534
  AmplitudeAPI::DELETION_URI_STRING,
479
- userpwd: userpwd,
480
- body: JSON.generate(body),
481
- headers: { 'Content-Type': 'application/json' }
482
- )
483
- described_class.delete(
484
- amplitude_ids: amplitude_ids,
485
- requester: 'privacy@gethopscotch.com'
535
+ JSON.generate(body),
536
+ "Content-Type" => "application/json"
486
537
  )
487
538
  end
488
539
  end
489
540
  end
490
541
 
491
- describe '#body' do
492
- it 'adds an api key' do
542
+ describe "#body" do
543
+ it "adds an api key" do
493
544
  event = AmplitudeAPI::Event.new(
494
545
  user_id: user,
495
- event_type: 'test_event',
546
+ event_type: "test_event",
496
547
  event_properties: {
497
548
  test_property: 1
498
549
  }
499
550
  )
500
- body = described_class.track_body(event)
501
- expect(body[:api_key]).to eq('stub api key')
551
+ json_body = described_class.track_body(event)
552
+ body = JSON.parse(json_body)
553
+ expect(body["api_key"]).to eq("stub api key")
502
554
  end
503
555
 
504
- it 'creates an event' do
556
+ it "creates an event" do
505
557
  event = AmplitudeAPI::Event.new(
506
558
  user_id: 23,
507
- event_type: 'test_event',
559
+ event_type: "test_event",
508
560
  event_properties: {
509
- foo: 'bar'
561
+ foo: "bar"
510
562
  },
511
563
  user_properties: {
512
- abc: '123'
564
+ abc: "123"
513
565
  },
514
- ip: '8.8.8.8'
566
+ ip: "8.8.8.8"
515
567
  )
516
- body = described_class.track_body(event)
517
-
568
+ json_body = described_class.track_body(event)
569
+ body = JSON.parse(json_body, symbolize_names: true)
518
570
  expected = [
519
571
  {
520
- event_type: 'test_event',
572
+ event_type: "test_event",
521
573
  user_id: 23,
522
574
  event_properties: {
523
- foo: 'bar'
575
+ foo: "bar"
524
576
  },
525
577
  user_properties: {
526
- abc: '123'
578
+ abc: "123"
527
579
  },
528
- ip: '8.8.8.8'
580
+ ip: "8.8.8.8"
529
581
  }
530
582
  ]
531
- expect(JSON.parse(body[:event], symbolize_names: true)).to eq(expected)
583
+ expect(body[:events]).to eq(expected)
532
584
  end
533
585
  end
534
586
  end