ablab 0.2.4 → 0.2.5

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: d558894e13e626caa2bd6375a6eea7b30f32769f
4
- data.tar.gz: 095902b47a9baf06fd1a98c61fd5c4e3aa4540b3
3
+ metadata.gz: 2a5e004cc3855d75784fb6725f3593f7aef46bae
4
+ data.tar.gz: 884592f7d393d1a5c0609b72ce2b3d43d6fb514a
5
5
  SHA512:
6
- metadata.gz: 4b9fc6b731810bfc862ec838cc823e06672da11ad25972a2af3fce833314f49ecd15f5b7806b5f56c8bf2ad61bc1ad1a1fe79cb8d4a4db7d20e092165319971e
7
- data.tar.gz: e443c648d9bbb7b9e5674ef69c8a9557edc07c3e911fbc5a43ead35cafff2de87d19f27e7738211b6f2ef5a2673ceba4e563ed20248da939e6a0d13f8bc362fe
6
+ metadata.gz: 5b35e42099d1eea2f4e92f5a295dedcf04dcb1a3e49e0b923b02fe321569266fc86703a9c23d0a5d5cf454db1d15e968be650aa1c390cbabfb74f420a0eca567
7
+ data.tar.gz: d2ccb3dcec40eecc47889db8ed08277b7b1fda33fafa45c1dc332045e874406a5b368a2cb72e9e408487c41f76e73660137b08d8650cfeed27f69db1a0f77301
data/lib/ablab.rb CHANGED
@@ -39,6 +39,14 @@ module Ablab
39
39
  end
40
40
  @dashboard_credentials
41
41
  end
42
+
43
+ def on_track(&block)
44
+ (@callbacks ||= []) << block
45
+ end
46
+
47
+ def callbacks
48
+ @callbacks || []
49
+ end
42
50
  end
43
51
 
44
52
  class InvalidCredentials < ArgumentError; end
@@ -85,16 +93,16 @@ module Ablab
85
93
  @result.data
86
94
  end
87
95
 
88
- def run(session_id)
89
- Run.new(self, session_id)
96
+ def run(session_id, request)
97
+ Run.new(self, session_id, request)
90
98
  end
91
99
  end
92
100
 
93
101
  class Run
94
- attr_reader :experiment, :session_id
102
+ attr_reader :experiment, :session_id, :request
95
103
 
96
- def initialize(experiment, session_id)
97
- @experiment, @session_id = experiment, session_id
104
+ def initialize(experiment, session_id, request)
105
+ @experiment, @session_id, @request = experiment, session_id, request
98
106
  end
99
107
 
100
108
  def in_group?(name)
@@ -126,7 +134,10 @@ module Ablab
126
134
 
127
135
  def perform_callbacks!(event)
128
136
  experiment.callbacks.each do |cbk|
129
- cbk.call(event, experiment.name, group, session_id)
137
+ cbk.call(event, experiment.name, group, session_id, request)
138
+ end
139
+ Ablab.callbacks.each do |cbk|
140
+ cbk.call(event, experiment.name, group, session_id, request)
130
141
  end
131
142
  end
132
143
  end
data/lib/ablab/helper.rb CHANGED
@@ -17,7 +17,7 @@ module Ablab
17
17
  raise "No experiment with name #{name}"
18
18
  end
19
19
  @experiments[name] ||=
20
- Ablab.experiments[name].run(ablab_session_id)
20
+ Ablab.experiments[name].run(ablab_session_id, request)
21
21
  end
22
22
 
23
23
  def ablab_session_id
data/lib/ablab/version.rb CHANGED
@@ -1,3 +1,3 @@
1
1
  module Ablab
2
- VERSION = "0.2.4"
2
+ VERSION = "0.2.5"
3
3
  end
@@ -4,6 +4,8 @@ require 'ostruct'
4
4
  describe Ablab::Helper do
5
5
  let(:cookies) { Hash.new }
6
6
 
7
+ let(:request) { double(:request) }
8
+
7
9
  let(:controller) do
8
10
  Class.new do
9
11
  include Ablab::Helper
@@ -23,6 +25,10 @@ describe Ablab::Helper do
23
25
  end
24
26
  end
25
27
 
28
+ before do
29
+ allow(controller).to receive(:request).and_return(request)
30
+ end
31
+
26
32
  it 'calls helper_method if the including class implements it' do
27
33
  klass = Class.new do
28
34
  def self.helper_method(_); end
data/spec/ablab_spec.rb CHANGED
@@ -53,6 +53,14 @@ describe Ablab do
53
53
  end
54
54
  end
55
55
 
56
+ describe ".on_track" do
57
+ it "adds a tracking callback" do
58
+ block = Proc.new {}
59
+ ab.on_track(&block)
60
+ expect(ab.callbacks).to eq([block])
61
+ end
62
+ end
63
+
56
64
  describe Ablab::Experiment do
57
65
  let(:experiment) do
58
66
  Ablab::Experiment.new('foo') do; end
@@ -150,62 +158,64 @@ describe Ablab do
150
158
  end
151
159
  end
152
160
 
161
+ let(:request) { double(:request) }
162
+
153
163
  let(:run) do
154
- Ablab::Run.new(experiment, '86wfd8w6df')
164
+ Ablab::Run.new(experiment, '86wfd8w6df', request)
155
165
  end
156
166
 
157
167
  it 'gets assigned to the right group' do
158
- run = Ablab::Run.new(experiment, 0)
168
+ run = Ablab::Run.new(experiment, 'abc', request)
159
169
  allow(run).to receive(:draw).and_return 0
160
170
  expect(run).to be_in_group(:control)
161
- run = Ablab::Run.new(experiment, 0)
171
+ run = Ablab::Run.new(experiment, 'abc', request)
162
172
  allow(run).to receive(:draw).and_return 334
163
173
  expect(run).to be_in_group(:a)
164
- run = Ablab::Run.new(experiment, 0)
174
+ run = Ablab::Run.new(experiment, 'abc', request)
165
175
  allow(run).to receive(:draw).and_return 999
166
176
  expect(run).to be_in_group(:b)
167
177
  end
168
178
 
169
179
  it 'assigns the same session ID to the same group' do
170
- run1 = Ablab::Run.new(experiment, 'foobar')
171
- run2 = Ablab::Run.new(experiment, 'foobar')
180
+ run1 = Ablab::Run.new(experiment, 'abc', request)
181
+ run2 = Ablab::Run.new(experiment, 'abc', request)
172
182
  expect(run1.group).to eq(run2.group)
173
183
  end
174
184
 
175
185
  it 'selects only the given percentage of users' do
176
186
  experiment.percentage_of_visitors 30
177
- run = Ablab::Run.new(experiment, 0)
187
+ run = Ablab::Run.new(experiment, 'abc', request)
178
188
  allow(run).to receive(:draw).and_return 0
179
189
  expect(run).to be_in_group(:control)
180
- run = Ablab::Run.new(experiment, 0)
190
+ run = Ablab::Run.new(experiment, 'abc', request)
181
191
  allow(run).to receive(:draw).and_return 100
182
192
  expect(run).to be_in_group(:a)
183
- run = Ablab::Run.new(experiment, 0)
193
+ run = Ablab::Run.new(experiment, 'abc', request)
184
194
  allow(run).to receive(:draw).and_return 200
185
195
  expect(run).to be_in_group(:b)
186
- run = Ablab::Run.new(experiment, 0)
196
+ run = Ablab::Run.new(experiment, 'abc', request)
187
197
  allow(run).to receive(:draw).and_return 300
188
198
  expect(run.group).to be_nil
189
199
  end
190
200
 
191
201
  describe "#draw" do
192
202
  it "is stable across ruby processes" do
193
- d1 = Ablab::Run.new(experiment, '8asd7f8asf7').draw
203
+ d1 = Ablab::Run.new(experiment, '8asd7f8asf7', request).draw
194
204
  dir = File.expand_path(File.dirname(__FILE__), '../lib')
195
- d2 = `bundle exec ruby -I#{dir} -e "require 'ablab'; require 'ostruct'; puts Ablab::Run.new(OpenStruct.new(name: :foo), '8asd7f8asf7').draw"`
205
+ d2 = `bundle exec ruby -I#{dir} -e "require 'ablab'; require 'ostruct'; puts Ablab::Run.new(OpenStruct.new(name: :foo), '8asd7f8asf7', nil).draw"`
196
206
  expect(d1).to eq(d2.to_i)
197
207
  end
198
208
 
199
209
  it "returns an integer number < 1000" do
200
210
  expect(
201
- (0..100).map { |i| Ablab::Run.new(experiment, "#{i}").draw }.all? { |x| x.is_a?(Integer) && x < 1000 }
211
+ (0..100).map { |i| Ablab::Run.new(experiment, "#{i}", request).draw }.all? { |x| x.is_a?(Integer) && x < 1000 }
202
212
  ).to be(true)
203
213
  end
204
214
  end
205
215
 
206
216
  describe "#group" do
207
217
  it "returns one of the groups" do
208
- expect(Ablab::Run.new(experiment, rand(12345).to_s).group).to be_in([:a, :b, :control])
218
+ expect(Ablab::Run.new(experiment, rand(12345).to_s, request).group).to be_in([:a, :b, :control])
209
219
  end
210
220
  end
211
221
 
@@ -218,11 +228,16 @@ describe Ablab do
218
228
 
219
229
  it "performs callbacks" do
220
230
  x = nil
221
- experiment.on_track do |event, experiment, group, session|
222
- x = [event, experiment, group, session]
231
+ y = nil
232
+ allow(Ablab).to receive(:callbacks) {
233
+ [ -> (*args) { y = args } ]
234
+ }
235
+ experiment.on_track do |event, experiment, group, session, request|
236
+ x = [event, experiment, group, session, request]
223
237
  end
224
238
  run.track_view!
225
- expect(x).to eq([:view, :foo, run.group, run.session_id])
239
+ expect(x).to eq([:view, :foo, run.group, run.session_id, request])
240
+ expect(y).to eq([:view, :foo, run.group, run.session_id, request])
226
241
  end
227
242
  end
228
243
 
@@ -235,11 +250,16 @@ describe Ablab do
235
250
 
236
251
  it "performs callbacks" do
237
252
  x = nil
238
- experiment.on_track do |event, experiment, group, session|
239
- x = [event, experiment, group, session]
253
+ y = nil
254
+ allow(Ablab).to receive(:callbacks) {
255
+ [ -> (*args) { y = args } ]
256
+ }
257
+ experiment.on_track do |event, experiment, group, session, request|
258
+ x = [event, experiment, group, session, request]
240
259
  end
241
260
  run.track_success!
242
- expect(x).to eq([:success, :foo, run.group, run.session_id])
261
+ expect(x).to eq([:success, :foo, run.group, run.session_id, request])
262
+ expect(y).to eq([:success, :foo, run.group, run.session_id, request])
243
263
  end
244
264
  end
245
265
  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
4
+ version: 0.2.5
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-03 00:00:00.000000000 Z
11
+ date: 2016-02-04 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rails