trailguide 0.1.21 → 0.1.22

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: ec80228061f031c01c0eb4d6dbef24d52ca205ccf2e6137ebe3a18886b095258
4
- data.tar.gz: 6fa47e1dd04511cbb35d39a737589a34658fcc9d1f0dde64298908a9ca539f27
3
+ metadata.gz: 7dadd0f6c7091b171c4e9034e9e4b31b9c7300121f6ef8b75fc577dc5913cf28
4
+ data.tar.gz: 1f84fdce8ea0d73bb189732fcb5099d1e3e41e48a0fa286d7e68854cb8d038f8
5
5
  SHA512:
6
- metadata.gz: ca6d1439e8632ec16946dcfc50e5201f24714a568a2b99c234d0ed666db6a6149724aeea7190be7669fe06dc232619cce6ead9211f175c1a518d2b6458dff7d4
7
- data.tar.gz: 7ab9d62c95a8c3b0a63fd1b09f0b51bea8d021b98a6797acff0658fbe8a718d5b8665458df8b92b31e89c2e606788d1aa677e96a1ce21dfeb2516e780e8bfacb
6
+ metadata.gz: d53f939b8c3773cbb08f0d8d36cdec3ead6d7aa0f904d6cc7f31339bab41f388ff0ad2ae48c054429c9152dc42e049a0e6ed98bdef085f28d780bc02e0d112ed
7
+ data.tar.gz: 8a18ec65a94353dfbe557c07295bb698a5486cdfd0c2071a5ba2a5adc51fea0c3e0b671b2a5d0d108808c814c2846850d6a0d5d839f2576d64d0a8135fd47f9d
@@ -6,8 +6,8 @@ module TrailGuide
6
6
 
7
7
  class << self
8
8
  alias_method :configure, :new
9
- def new(context, &block)
10
- configure(&block).new(context)
9
+ def new(_context, &block)
10
+ configure(&block).new(_context)
11
11
  end
12
12
  end
13
13
 
@@ -18,15 +18,14 @@ module TrailGuide
18
18
  end
19
19
 
20
20
  # instance method, creates a new adapter and passes through config
21
- def new(context)
22
- Adapter.new(context, configuration)
21
+ def new(_context)
22
+ Adapter.new(configuration)
23
23
  end
24
24
 
25
25
  class Adapter
26
- attr_reader :context, :config
26
+ attr_reader :config
27
27
 
28
- def initialize(context, config)
29
- @context = context
28
+ def initialize(config)
30
29
  @config = config
31
30
  end
32
31
 
@@ -27,10 +27,9 @@ module TrailGuide
27
27
  end
28
28
 
29
29
  class Adapter
30
- attr_reader :context, :config, :storage_key
30
+ attr_reader :config, :storage_key
31
31
 
32
32
  def initialize(context, config, key: nil)
33
- @context = context
34
33
  @config = config
35
34
 
36
35
  if key
@@ -9,7 +9,7 @@ module TrailGuide
9
9
  end
10
10
 
11
11
  def participating?
12
- @participating ||= variant.present?#participant.participating?(experiment)
12
+ @participating ||= variant.present?
13
13
  end
14
14
 
15
15
  def converted?(checkpoint=nil)
@@ -1,9 +1,48 @@
1
1
  module TrailGuide
2
2
  module Helper
3
3
  def trailguide(metric=nil, **opts, &block)
4
- proxy = HelperProxy.new(self)
5
- return proxy if metric.nil?
6
- proxy.choose!(metric, **opts, &block)
4
+ @trailguide_proxy ||= HelperProxy.new(self)
5
+ return @trailguide_proxy if metric.nil?
6
+ @trailguide_proxy.choose!(metric, **opts, &block)
7
+ end
8
+
9
+ def trailguide_participant
10
+ @trailguide_participant ||= TrailGuide::Participant.new(self)
11
+ end
12
+
13
+ def trailguide_excluded_request?
14
+ @trailguide_excluded_request ||= instance_exec(self, &TrailGuide.configuration.request_filter)
15
+ end
16
+
17
+ def is_preview?
18
+ return false unless respond_to?(:request, true)
19
+ headers = request.try(:headers)
20
+ headers && headers['x-purpose'] == 'preview'
21
+ end
22
+
23
+ def is_filtered_user_agent?
24
+ return false if TrailGuide.configuration.filtered_user_agents.nil? || TrailGuide.configuration.filtered_user_agents.empty?
25
+ return false unless respond_to?(:request, true) && request.user_agent
26
+
27
+ TrailGuide.configuration.filtered_user_agents do |ua|
28
+ return true if ua.class == String && request.user_agent == ua
29
+ return true if ua.class == Regexp && request.user_agent =~ ua
30
+ end
31
+
32
+ return false
33
+ end
34
+
35
+ def is_filtered_ip_address?
36
+ return false if TrailGuide.configuration.filtered_ip_addresses.nil? || TrailGuide.configuration.filtered_ip_addresses.empty?
37
+ return false unless respond_to?(:request, true) && request.ip
38
+
39
+ TrailGuide.configuration.filtered_ip_addresses.each do |ip|
40
+ return true if ip.class == String && request.ip == ip
41
+ return true if ip.class == Regexp && request.ip =~ ip
42
+ return true if ip.class == Range && ip.first.class == IPAddr && ip.include?(IPAddr.new(request.ip))
43
+ end
44
+
45
+ return false
7
46
  end
8
47
 
9
48
  class HelperProxy
@@ -35,7 +74,7 @@ module TrailGuide
35
74
  end
36
75
 
37
76
  def participant
38
- @participant ||= TrailGuide::Participant.new(context)
77
+ @participant ||= context.send(:trailguide_participant)
39
78
  end
40
79
 
41
80
  def context_type
@@ -143,43 +182,7 @@ module TrailGuide
143
182
 
144
183
  def exclude_visitor?
145
184
  return false if experiment.configuration.skip_request_filter?
146
- instance_exec(context, &TrailGuide.configuration.request_filter)
147
- end
148
-
149
- def is_preview?
150
- return false unless context.respond_to?(:request, true)
151
- headers = context.send(:request).try(:headers)
152
- headers && headers['x-purpose'] == 'preview'
153
- end
154
-
155
- def is_filtered_user_agent?
156
- return false if TrailGuide.configuration.filtered_user_agents.nil? || TrailGuide.configuration.filtered_user_agents.empty?
157
- return false unless context.respond_to?(:request, true)
158
- request = context.send(:request)
159
- return false unless request && request.user_agent
160
-
161
- TrailGuide.configuration.filtered_user_agents do |ua|
162
- return true if ua.class == String && request.user_agent == ua
163
- return true if ua.class == Regexp && request.user_agent =~ ua
164
- end
165
-
166
- return false
167
- end
168
-
169
- def is_filtered_ip_address?
170
- return false if TrailGuide.configuration.filtered_ip_addresses.nil? || TrailGuide.configuration.filtered_ip_addresses.empty?
171
-
172
- return false unless context.respond_to?(:request, true)
173
- request = context.send(:request)
174
- return false unless request && request.ip
175
-
176
- TrailGuide.configuration.filtered_ip_addresses.each do |ip|
177
- return true if ip.class == String && request.ip == ip
178
- return true if ip.class == Regexp && request.ip =~ ip
179
- return true if ip.class == Range && ip.first.class == IPAddr && ip.include?(IPAddr.new(request.ip))
180
- end
181
-
182
- return false
185
+ context.send(:trailguide_excluded_request?)
183
186
  end
184
187
  end
185
188
  end
@@ -1,55 +1,53 @@
1
1
  module TrailGuide
2
2
  class Participant
3
- attr_reader :context, :variants
3
+ attr_reader :adapter
4
4
  delegate :key?, :keys, :[], :[]=, :delete, :destroy!, :to_h, to: :adapter
5
5
 
6
6
  def initialize(context, adapter: nil)
7
- @context = context
8
- @adapter = adapter.new(context) unless adapter.nil?
9
- @variants = {}
7
+ @adapter = adapter.present? ? adapter.new(context) : configured_adapter.new(context)
10
8
  #cleanup_inactive_experiments!
11
9
  end
12
10
 
13
- def adapter
14
- @adapter ||= begin
15
- config_adapter = TrailGuide.configuration.adapter
16
- case config_adapter
17
- when :cookie
18
- config_adapter = TrailGuide::Adapters::Participants::Cookie
19
- when :session
20
- config_adapter = TrailGuide::Adapters::Participants::Session
21
- when :redis
22
- config_adapter = TrailGuide::Adapters::Participants::Redis
23
- when :anonymous
24
- config_adapter = TrailGuide::Adapters::Participants::Anonymous
25
- when :multi
26
- config_adapter = TrailGuide::Adapters::Participants::Multi
27
- else
28
- config_adapter = config_adapter.constantize if config_adapter.is_a?(String)
29
- end
30
- config_adapter.new(context)
31
- rescue => e
32
- [TrailGuide.configuration.on_adapter_failover].flatten.compact.each do |callback|
33
- callback.call(config_adapter, e)
34
- end
35
- TrailGuide::Adapters::Participants::Anonymous.new(context)
11
+ def configured_adapter
12
+ config_adapter = TrailGuide.configuration.adapter
13
+ case config_adapter
14
+ when :cookie
15
+ config_adapter = TrailGuide::Adapters::Participants::Cookie
16
+ when :session
17
+ config_adapter = TrailGuide::Adapters::Participants::Session
18
+ when :redis
19
+ config_adapter = TrailGuide::Adapters::Participants::Redis
20
+ when :anonymous
21
+ config_adapter = TrailGuide::Adapters::Participants::Anonymous
22
+ when :multi
23
+ config_adapter = TrailGuide::Adapters::Participants::Multi
24
+ else
25
+ config_adapter = config_adapter.constantize if config_adapter.is_a?(String)
26
+ end
27
+ config_adapter
28
+ rescue => e
29
+ [TrailGuide.configuration.on_adapter_failover].flatten.compact.each do |callback|
30
+ callback.call(config_adapter, e)
36
31
  end
32
+ TrailGuide::Adapters::Participants::Anonymous
37
33
  end
38
34
 
39
- def participating?(experiment, include_control=true)
40
- return variants[experiment.storage_key] if variants.key?(experiment.storage_key)
41
- return false unless experiment.started?
42
- return false unless adapter.key?(experiment.storage_key)
35
+ def variant(experiment)
36
+ return nil unless experiment.started?
37
+ return nil unless adapter.key?(experiment.storage_key)
43
38
  varname = adapter[experiment.storage_key]
44
39
  variant = experiment.variants.find { |var| var == varname }
45
- return false if !include_control && variant.control?
46
- return false unless variant && adapter.key?(variant.storage_key)
40
+ return nil unless variant && adapter.key?(variant.storage_key)
47
41
 
48
42
  chosen_at = Time.at(adapter[variant.storage_key].to_i)
49
- if chosen_at >= experiment.started_at
50
- variants[experiment.storage_key] = variant
51
- return variant
52
- end
43
+ return variant if chosen_at >= experiment.started_at
44
+ end
45
+
46
+ def participating?(experiment, include_control=true)
47
+ var = variant(experiment)
48
+ return false if var.nil?
49
+ return false if !include_control && var.control?
50
+ return true
53
51
  end
54
52
 
55
53
  def converted?(experiment, checkpoint=nil)
@@ -79,10 +77,6 @@ module TrailGuide
79
77
  end
80
78
  end
81
79
 
82
- def variant(experiment)
83
- participating?(experiment) || nil
84
- end
85
-
86
80
  def participating!(variant)
87
81
  adapter[variant.experiment.storage_key] = variant.name
88
82
  adapter[variant.storage_key] = Time.now.to_i
@@ -2,7 +2,7 @@ module TrailGuide
2
2
  module Version
3
3
  MAJOR = 0
4
4
  MINOR = 1
5
- PATCH = 21
5
+ PATCH = 22
6
6
  VERSION = "#{MAJOR}.#{MINOR}.#{PATCH}"
7
7
 
8
8
  class << self
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: trailguide
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.21
4
+ version: 0.1.22
5
5
  platform: ruby
6
6
  authors:
7
7
  - Mark Rebec
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2019-03-13 00:00:00.000000000 Z
11
+ date: 2019-03-14 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rails