amplitude-api 0.1.0 → 0.3.3

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