ablab 0.2.8 → 0.2.9

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 58f3558e8df0966c774c936d9abb1ea3ef30cb40
4
- data.tar.gz: 5296df570693f9db5e472cf6b18a1be20fd5fec0
3
+ metadata.gz: 6b000e4eb969554a264a4abfc4293be995c35e1c
4
+ data.tar.gz: 6ef436d7216b99eb8b7b31b8181cbeeccf53ed8c
5
5
  SHA512:
6
- metadata.gz: 9fddf7df11963b05e36359ff02659070e1211c6c1f06ecd712725dd2ac5f936a7b05e8134bb6c8981236247226b32d4598b64545fd85156f27a815f758ec27a8
7
- data.tar.gz: 427443b6a68e2333f898fb916ed2ddb9f895f2f9d1ee0530f120bb5ef5e0945c363e7c68d4b947823176f5a82bdda1f1f3e730c88c55e7bdddd92f761a1a3d81
6
+ metadata.gz: ade19662889d8bef5926285e67a1715f586adb92ab10de8988ba367d2bc54a2145ec36f6946187b8a1f4acefdf25d4109f8beeeebc400449f9cc5b8b11fa692c
7
+ data.tar.gz: 56e137554415c1dfa674e6aec64cd2cd3e102fa078b5445a5c2471d9680ffa1b6c5ea5a059436cc6f69fffddc399c741716d44c24b1d5b3f4eee7a47fdc5f376
@@ -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 || Proc.new { |e| raise e }
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
- Ablab.tracker.track_view!(experiment.name, group, session_id)
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
- Ablab.tracker.track_success!(experiment.name, group, session_id)
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
@@ -1,3 +1,3 @@
1
1
  module Ablab
2
- VERSION = "0.2.8"
2
+ VERSION = "0.2.9"
3
3
  end
@@ -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 "call exception handler if given" do
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 "call exception handler if given" do
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.8
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-10 00:00:00.000000000 Z
11
+ date: 2016-02-11 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rails