ablab 0.2.8 → 0.2.9
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.
- checksums.yaml +4 -4
- data/lib/ablab.rb +30 -13
- data/lib/ablab/version.rb +1 -1
- data/spec/ablab_spec.rb +47 -2
- metadata +2 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 6b000e4eb969554a264a4abfc4293be995c35e1c
|
4
|
+
data.tar.gz: 6ef436d7216b99eb8b7b31b8181cbeeccf53ed8c
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: ade19662889d8bef5926285e67a1715f586adb92ab10de8988ba367d2bc54a2145ec36f6946187b8a1f4acefdf25d4109f8beeeebc400449f9cc5b8b11fa692c
|
7
|
+
data.tar.gz: 56e137554415c1dfa674e6aec64cd2cd3e102fa078b5445a5c2471d9680ffa1b6c5ea5a059436cc6f69fffddc399c741716d44c24b1d5b3f4eee7a47fdc5f376
|
data/lib/ablab.rb
CHANGED
@@ -6,6 +6,9 @@ require "forwardable"
|
|
6
6
|
|
7
7
|
module Ablab
|
8
8
|
module ModuleMethods
|
9
|
+
TRACKING_EXCEPTION_HANDLER = Proc.new { |e| raise e }
|
10
|
+
ALLOW_TRACKING = Proc.new { true }
|
11
|
+
|
9
12
|
attr_reader :experiments
|
10
13
|
|
11
14
|
def setup(&block)
|
@@ -44,12 +47,17 @@ module Ablab
|
|
44
47
|
(@callbacks ||= []) << block
|
45
48
|
end
|
46
49
|
|
50
|
+
def allow_tracking(&block)
|
51
|
+
@allow_tracking = block if block_given?
|
52
|
+
@allow_tracking || ALLOW_TRACKING
|
53
|
+
end
|
54
|
+
|
47
55
|
def on_tracking_exception(&block)
|
48
56
|
@tracking_exception_handler = block
|
49
57
|
end
|
50
58
|
|
51
59
|
def tracking_exception_handler
|
52
|
-
@tracking_exception_handler ||
|
60
|
+
@tracking_exception_handler || TRACKING_EXCEPTION_HANDLER
|
53
61
|
end
|
54
62
|
|
55
63
|
def callbacks
|
@@ -118,21 +126,11 @@ module Ablab
|
|
118
126
|
end
|
119
127
|
|
120
128
|
def track_view!
|
121
|
-
|
122
|
-
Thread.new do
|
123
|
-
perform_callbacks!(:view)
|
124
|
-
end
|
125
|
-
rescue => e
|
126
|
-
Ablab.tracking_exception_handler.call(e)
|
129
|
+
track!(:view)
|
127
130
|
end
|
128
131
|
|
129
132
|
def track_success!
|
130
|
-
|
131
|
-
Thread.new do
|
132
|
-
perform_callbacks!(:success)
|
133
|
-
end
|
134
|
-
rescue => e
|
135
|
-
Ablab.tracking_exception_handler.call(e)
|
133
|
+
track!(:success)
|
136
134
|
end
|
137
135
|
|
138
136
|
def group
|
@@ -158,6 +156,8 @@ module Ablab
|
|
158
156
|
Ablab.callbacks.each do |cbk|
|
159
157
|
cbk.call(event, experiment.name, group, session_id, request)
|
160
158
|
end
|
159
|
+
rescue => e
|
160
|
+
Ablab.tracking_exception_handler.call(e)
|
161
161
|
end
|
162
162
|
|
163
163
|
private def forced_group
|
@@ -170,6 +170,23 @@ module Ablab
|
|
170
170
|
group = hash[experiment.name.to_s]
|
171
171
|
group.to_sym if group && experiment.groups.map { |g| g.name.to_s }.include?(group)
|
172
172
|
end
|
173
|
+
|
174
|
+
private def track!(event)
|
175
|
+
if allowed?(experiment.name, group, session_id, request)
|
176
|
+
method = (event == :view) ? :track_view! : :track_success!
|
177
|
+
Ablab.tracker.send(method, experiment.name, group, session_id)
|
178
|
+
Thread.new do
|
179
|
+
perform_callbacks!(event)
|
180
|
+
end
|
181
|
+
end
|
182
|
+
rescue => e
|
183
|
+
Ablab.tracking_exception_handler.call(e)
|
184
|
+
end
|
185
|
+
|
186
|
+
private def allowed?(experiment_name, group, session_id, request)
|
187
|
+
filter = Ablab.allow_tracking
|
188
|
+
filter.call(experiment_name, group, session_id, request)
|
189
|
+
end
|
173
190
|
end
|
174
191
|
|
175
192
|
class Group
|
data/lib/ablab/version.rb
CHANGED
data/spec/ablab_spec.rb
CHANGED
@@ -69,6 +69,15 @@ describe Ablab do
|
|
69
69
|
end
|
70
70
|
end
|
71
71
|
|
72
|
+
describe ".allow_tracking" do
|
73
|
+
it "sets and return allow_tracking" do
|
74
|
+
block = Proc.new {}
|
75
|
+
filter = ab.allow_tracking(&block)
|
76
|
+
expect(filter).to eq(block)
|
77
|
+
expect(ab.allow_tracking).to eq(block)
|
78
|
+
end
|
79
|
+
end
|
80
|
+
|
72
81
|
describe Ablab::Experiment do
|
73
82
|
let(:experiment) do
|
74
83
|
Ablab::Experiment.new('foo') do; end
|
@@ -255,7 +264,7 @@ describe Ablab do
|
|
255
264
|
expect(y).to eq([:view, :foo, run.group, run.session_id, request])
|
256
265
|
end
|
257
266
|
|
258
|
-
it "
|
267
|
+
it "calls exception handler if given" do
|
259
268
|
exception = nil
|
260
269
|
allow(Ablab.tracker).to receive(:track_view!) { raise "Boom!" }
|
261
270
|
allow(Ablab).to receive(:tracking_exception_handler)
|
@@ -263,6 +272,24 @@ describe Ablab do
|
|
263
272
|
expect { run.track_view! }.to_not raise_error
|
264
273
|
expect(exception).to be_a(StandardError)
|
265
274
|
end
|
275
|
+
|
276
|
+
it "calls exception handler if callback fails" do
|
277
|
+
exception = nil
|
278
|
+
allow(Ablab).to receive(:callbacks) {
|
279
|
+
[ -> (*args) { raise "boom!" } ]
|
280
|
+
}
|
281
|
+
allow(Ablab).to receive(:tracking_exception_handler)
|
282
|
+
.and_return(Proc.new { |e| exception = e })
|
283
|
+
expect { run.track_view!.join }.to_not raise_error
|
284
|
+
expect(exception).to be_a(StandardError)
|
285
|
+
end
|
286
|
+
|
287
|
+
it "does nothing if allow_tracking returns false" do
|
288
|
+
allow(Ablab).to receive(:allow_tracking) { Proc.new { false } }
|
289
|
+
expect(Ablab.tracker).to_not receive(:track_view!)
|
290
|
+
expect(run).to_not receive(:perform_callbacks!)
|
291
|
+
run.track_view!
|
292
|
+
end
|
266
293
|
end
|
267
294
|
|
268
295
|
describe "#track_success!" do
|
@@ -286,7 +313,7 @@ describe Ablab do
|
|
286
313
|
expect(y).to eq([:success, :foo, run.group, run.session_id, request])
|
287
314
|
end
|
288
315
|
|
289
|
-
it "
|
316
|
+
it "calls exception handler if given" do
|
290
317
|
exception = nil
|
291
318
|
allow(Ablab.tracker).to receive(:track_success!) { raise "Boom!" }
|
292
319
|
allow(Ablab).to receive(:tracking_exception_handler)
|
@@ -294,6 +321,24 @@ describe Ablab do
|
|
294
321
|
expect { run.track_success! }.to_not raise_error
|
295
322
|
expect(exception).to be_a(StandardError)
|
296
323
|
end
|
324
|
+
|
325
|
+
it "calls exception handler if callback fails" do
|
326
|
+
exception = nil
|
327
|
+
allow(Ablab).to receive(:callbacks) {
|
328
|
+
[ -> (*args) { raise "boom!" } ]
|
329
|
+
}
|
330
|
+
allow(Ablab).to receive(:tracking_exception_handler)
|
331
|
+
.and_return(Proc.new { |e| exception = e })
|
332
|
+
expect { run.track_success!.join }.to_not raise_error
|
333
|
+
expect(exception).to be_a(StandardError)
|
334
|
+
end
|
335
|
+
|
336
|
+
it "does nothing if allow_tracking returns false" do
|
337
|
+
allow(Ablab).to receive(:allow_tracking) { Proc.new { false } }
|
338
|
+
expect(Ablab.tracker).to_not receive(:track_success!)
|
339
|
+
expect(run).to_not receive(:perform_callbacks!)
|
340
|
+
run.track_success!
|
341
|
+
end
|
297
342
|
end
|
298
343
|
end
|
299
344
|
end
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: ablab
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.2.
|
4
|
+
version: 0.2.9
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Luca Ongaro
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2016-02-
|
11
|
+
date: 2016-02-11 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: rails
|