split 4.0.3 → 4.0.4

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
  SHA256:
3
- metadata.gz: 746dd3b526b5464f12e01e2f00f8ff62b71ac6483527632f6f0bca7bc5242e8c
4
- data.tar.gz: bb40ca355a1aa9eec9cfb4067e6e2b3c381072830cffadffe414f8d7a4af043f
3
+ metadata.gz: 80d095f07432d336e773b30c3c5a873b554dd682f65ace33886d1a83697d447a
4
+ data.tar.gz: 9a504a3c9d4c67391528e1640c2c7eac3cfcafe91305cadb901e48541726f04c
5
5
  SHA512:
6
- metadata.gz: 70466ddfe57955a43dda0506c4a23806f6bdab154b344e495879027ae2baebec93d59f34018e78a3e83ef945c776c7a2ddcf2b40cd71998fb0bc9207ed60df61
7
- data.tar.gz: db1d46d7e7e1826aacf80ec1c8634f0502bd07d4b58d488e57f4917a8843954d568b4cd20c9e2db09ac2ff80811b14ec68a3158b870d091eeabf4e426af8172d
6
+ metadata.gz: 68e5919618103f315fa9f4f0d18384392c02c01c680c13d50a77cfa0507bd19a1c01fb5c1db66619329c1ef38013c3ebe90e880f6af5c9bac9ba2d1a76b3963a
7
+ data.tar.gz: 1ae5fe0187e16b4bb3efc14a0e0ded4df98527fbc530dab4a89c544d133b1a7e8cd0adc6579f0f43ea3c84aa6899a2d123e97c5af105ce158e804a5ac8286728
data/CHANGELOG.md CHANGED
@@ -1,4 +1,12 @@
1
- # 4.0.3 (November 15rd, 2023)
1
+ # 4.0.4 (March 3rd, 2024)
2
+
3
+ Bugfixes:
4
+ - Better integration for EncapsulatedHelper when needing params/request info (@henrique-ft, #721 and #723)
5
+
6
+ Misc:
7
+ - Make specs compatible with newer Rack versions (@andrehjr, #722)
8
+
9
+ # 4.0.3 (November 15th, 2023)
2
10
 
3
11
  Bugfixes:
4
12
  - Do not throw error if alternativas have data that can lead to negative numbers for probability calculation (@andrehjr, #703)
@@ -23,6 +23,14 @@ module Split
23
23
  @context = context
24
24
  end
25
25
 
26
+ def params
27
+ request.params if request && request.respond_to?(:params)
28
+ end
29
+
30
+ def request
31
+ @context.request if @context.respond_to?(:request)
32
+ end
33
+
26
34
  def ab_user
27
35
  @ab_user ||= Split::User.new(@context)
28
36
  end
data/lib/split/helper.rb CHANGED
@@ -121,11 +121,11 @@ module Split
121
121
  end
122
122
 
123
123
  def override_alternative_by_params(experiment_name)
124
- defined?(params) && params[OVERRIDE_PARAM_NAME] && params[OVERRIDE_PARAM_NAME][experiment_name]
124
+ params_present? && params[OVERRIDE_PARAM_NAME] && params[OVERRIDE_PARAM_NAME][experiment_name]
125
125
  end
126
126
 
127
127
  def override_alternative_by_cookies(experiment_name)
128
- return unless defined?(request)
128
+ return unless request_present?
129
129
 
130
130
  if request.cookies && request.cookies.key?("split_override")
131
131
  experiments = JSON.parse(request.cookies["split_override"]) rescue {}
@@ -134,7 +134,7 @@ module Split
134
134
  end
135
135
 
136
136
  def split_generically_disabled?
137
- defined?(params) && params["SPLIT_DISABLE"]
137
+ params_present? && params["SPLIT_DISABLE"]
138
138
  end
139
139
 
140
140
  def ab_user
@@ -142,26 +142,34 @@ module Split
142
142
  end
143
143
 
144
144
  def exclude_visitor?
145
- defined?(request) && (instance_exec(request, &Split.configuration.ignore_filter) || is_ignored_ip_address? || is_robot? || is_preview?)
145
+ request_present? && (instance_exec(request, &Split.configuration.ignore_filter) || is_ignored_ip_address? || is_robot? || is_preview?)
146
146
  end
147
147
 
148
148
  def is_robot?
149
- defined?(request) && request.user_agent =~ Split.configuration.robot_regex
149
+ request_present? && request.user_agent =~ Split.configuration.robot_regex
150
150
  end
151
151
 
152
152
  def is_preview?
153
- defined?(request) && defined?(request.headers) && request.headers["x-purpose"] == "preview"
153
+ request_present? && defined?(request.headers) && request.headers["x-purpose"] == "preview"
154
154
  end
155
155
 
156
156
  def is_ignored_ip_address?
157
157
  return false if Split.configuration.ignore_ip_addresses.empty?
158
158
 
159
159
  Split.configuration.ignore_ip_addresses.each do |ip|
160
- return true if defined?(request) && (request.ip == ip || (ip.class == Regexp && request.ip =~ ip))
160
+ return true if request_present? && (request.ip == ip || (ip.class == Regexp && request.ip =~ ip))
161
161
  end
162
162
  false
163
163
  end
164
164
 
165
+ def params_present?
166
+ defined?(params) && params
167
+ end
168
+
169
+ def request_present?
170
+ defined?(request) && request
171
+ end
172
+
165
173
  def active_experiments
166
174
  ab_user.active_experiments
167
175
  end
data/lib/split/version.rb CHANGED
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module Split
4
- VERSION = "4.0.3"
4
+ VERSION = "4.0.4"
5
5
  end
@@ -3,11 +3,7 @@
3
3
  require "spec_helper"
4
4
 
5
5
  describe Split::EncapsulatedHelper do
6
- include Split::EncapsulatedHelper
7
-
8
- def params
9
- raise NoMethodError, "This method is not really defined"
10
- end
6
+ let(:context_shim) { Split::EncapsulatedHelper::ContextShim.new(double(request: request)) }
11
7
 
12
8
  describe "ab_test" do
13
9
  before do
@@ -15,20 +11,15 @@ describe Split::EncapsulatedHelper do
15
11
  .and_return(mock_user)
16
12
  end
17
13
 
18
- it "should not raise an error when params raises an error" do
19
- expect { params }.to raise_error(NoMethodError)
20
- expect { ab_test("link_color", "blue", "red") }.not_to raise_error
21
- end
22
-
23
14
  it "calls the block with selected alternative" do
24
- expect { |block| ab_test("link_color", "red", "red", &block) }.to yield_with_args("red", {})
15
+ expect { |block| context_shim.ab_test("link_color", "red", "red", &block) }.to yield_with_args("red", {})
25
16
  end
26
17
 
27
18
  context "inside a view" do
28
19
  it "works inside ERB" do
29
20
  require "erb"
30
21
  template = ERB.new(<<-ERB.split(/\s+/s).map(&:strip).join(" "), nil, "%")
31
- foo <% ab_test(:foo, '1', '2') do |alt, meta| %>
22
+ foo <% context_shim.ab_test(:foo, '1', '2') do |alt, meta| %>
32
23
  static <%= alt %>
33
24
  <% end %>
34
25
  ERB
@@ -43,8 +34,43 @@ describe Split::EncapsulatedHelper do
43
34
  include Split::EncapsulatedHelper
44
35
  public :session
45
36
  }.new
37
+
46
38
  expect(ctx).to receive(:session) { {} }
47
39
  expect { ctx.ab_test("link_color", "blue", "red") }.not_to raise_error
48
40
  end
41
+
42
+ context "when request is defined in context of ContextShim" do
43
+ context "when overriding by params" do
44
+ it do
45
+ ctx = Class.new {
46
+ public :session
47
+ def request
48
+ build_request(params: {
49
+ "ab_test" => { "link_color" => "blue" }
50
+ })
51
+ end
52
+ }.new
53
+
54
+ context_shim = Split::EncapsulatedHelper::ContextShim.new(ctx)
55
+ expect(context_shim.ab_test("link_color", "blue", "red")).to be("blue")
56
+ end
57
+ end
58
+
59
+ context "when overriding by cookies" do
60
+ it do
61
+ ctx = Class.new {
62
+ public :session
63
+ def request
64
+ build_request(cookies: {
65
+ "split_override" => '{ "link_color": "red" }'
66
+ })
67
+ end
68
+ }.new
69
+
70
+ context_shim = Split::EncapsulatedHelper::ContextShim.new(ctx)
71
+ expect(context_shim.ab_test("link_color", "blue", "red")).to be("red")
72
+ end
73
+ end
74
+ end
49
75
  end
50
76
  end
@@ -67,14 +67,14 @@ describe Split::Persistence::CookieAdapter do
67
67
  it "puts multiple experiments in a single cookie" do
68
68
  subject["foo"] = "FOO"
69
69
  subject["bar"] = "BAR"
70
- expect(context.response.headers["Set-Cookie"]).to match(/\Asplit=%7B%22foo%22%3A%22FOO%22%2C%22bar%22%3A%22BAR%22%7D; path=\/; expires=[a-zA-Z]{3}, \d{2} [a-zA-Z]{3} \d{4} \d{2}:\d{2}:\d{2} [A-Z]{3}\Z/)
70
+ expect(Array(context.response.headers["Set-Cookie"])).to include(/\Asplit=%7B%22foo%22%3A%22FOO%22%2C%22bar%22%3A%22BAR%22%7D; path=\/; expires=[a-zA-Z]{3}, \d{2} [a-zA-Z]{3} \d{4} \d{2}:\d{2}:\d{2} [A-Z]{3}\Z/)
71
71
  end
72
72
 
73
73
  it "ensure other added cookies are not overriden" do
74
74
  context.response.set_cookie "dummy", "wow"
75
75
  subject["foo"] = "FOO"
76
- expect(context.response.headers["Set-Cookie"]).to include("dummy=wow")
77
- expect(context.response.headers["Set-Cookie"]).to include("split=")
76
+ expect(Array(context.response.headers["Set-Cookie"])).to include(/dummy=wow/)
77
+ expect(Array(context.response.headers["Set-Cookie"])).to include(/split=/)
78
78
  end
79
79
  end
80
80
 
data/spec/spec_helper.rb CHANGED
@@ -44,11 +44,20 @@ def params
44
44
  @params ||= {}
45
45
  end
46
46
 
47
- def request(ua = "Mozilla/5.0 (Macintosh; U; Intel Mac OS X 10_6_6; de-de) AppleWebKit/533.20.25 (KHTML, like Gecko) Version/5.0.4 Safari/533.20.27")
48
- @request ||= begin
49
- r = OpenStruct.new
50
- r.user_agent = ua
51
- r.ip = "192.168.1.1"
52
- r
53
- end
47
+ def request
48
+ @request ||= build_request
49
+ end
50
+
51
+ def build_request(
52
+ ua: "Mozilla/5.0 (Macintosh; U; Intel Mac OS X 10_6_6; de-de) AppleWebKit/533.20.25 (KHTML, like Gecko) Version/5.0.4 Safari/533.20.27",
53
+ ip: "192.168.1.1",
54
+ params: {},
55
+ cookies: {}
56
+ )
57
+ r = OpenStruct.new
58
+ r.user_agent = ua
59
+ r.ip = ip
60
+ r.params = params
61
+ r.cookies = cookies
62
+ r
54
63
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: split
3
3
  version: !ruby/object:Gem::Version
4
- version: 4.0.3
4
+ version: 4.0.4
5
5
  platform: ruby
6
6
  authors:
7
7
  - Andrew Nesbitt
8
- autorequire:
8
+ autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2023-11-15 00:00:00.000000000 Z
11
+ date: 2024-03-03 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: redis
@@ -164,7 +164,7 @@ dependencies:
164
164
  - - ">="
165
165
  - !ruby/object:Gem::Version
166
166
  version: '5.0'
167
- description:
167
+ description:
168
168
  email:
169
169
  - andrewnez@gmail.com
170
170
  executables: []
@@ -274,7 +274,7 @@ metadata:
274
274
  bug_tracker_uri: https://github.com/splitrb/split/issues
275
275
  wiki_uri: https://github.com/splitrb/split/wiki
276
276
  mailing_list_uri: https://groups.google.com/d/forum/split-ruby
277
- post_install_message:
277
+ post_install_message:
278
278
  rdoc_options: []
279
279
  require_paths:
280
280
  - lib
@@ -289,8 +289,8 @@ required_rubygems_version: !ruby/object:Gem::Requirement
289
289
  - !ruby/object:Gem::Version
290
290
  version: 2.0.0
291
291
  requirements: []
292
- rubygems_version: 3.1.6
293
- signing_key:
292
+ rubygems_version: 3.5.3
293
+ signing_key:
294
294
  specification_version: 4
295
295
  summary: Rack based split testing framework
296
296
  test_files: