unleash 5.1.1 → 6.0.1.pre

Sign up to get free protection for your applications and to get access to all the features.
@@ -1,24 +0,0 @@
1
- require 'unleash/strategy/util'
2
-
3
- module Unleash
4
- module Strategy
5
- class GradualRolloutRandom < Base
6
- def name
7
- 'gradualRolloutRandom'
8
- end
9
-
10
- # need: params['percentage']
11
- def is_enabled?(params = {}, _context = nil)
12
- return false unless params.is_a?(Hash) && params.has_key?('percentage')
13
-
14
- begin
15
- percentage = Integer(params['percentage'] || 0)
16
- rescue ArgumentError
17
- return false
18
- end
19
-
20
- (percentage >= Random.rand(1..100))
21
- end
22
- end
23
- end
24
- end
@@ -1,21 +0,0 @@
1
- require 'unleash/strategy/util'
2
-
3
- module Unleash
4
- module Strategy
5
- class GradualRolloutSessionId < Base
6
- def name
7
- 'gradualRolloutSessionId'
8
- end
9
-
10
- # need: params['percentage'], params['groupId'], context.user_id,
11
- def is_enabled?(params = {}, context = nil)
12
- return false unless params.is_a?(Hash) && params.has_key?('percentage')
13
- return false unless context.instance_of?(Unleash::Context)
14
- return false if context.session_id.nil? || context.session_id.empty?
15
-
16
- percentage = Integer(params['percentage'] || 0)
17
- (percentage.positive? && Util.get_normalized_number(context.session_id, params['groupId'] || "", 0) <= percentage)
18
- end
19
- end
20
- end
21
- end
@@ -1,21 +0,0 @@
1
- require 'unleash/strategy/util'
2
-
3
- module Unleash
4
- module Strategy
5
- class GradualRolloutUserId < Base
6
- def name
7
- 'gradualRolloutUserId'
8
- end
9
-
10
- # need: params['percentage'], params['groupId'], context.user_id,
11
- def is_enabled?(params = {}, context = nil, _constraints = [])
12
- return false unless params.is_a?(Hash) && params.has_key?('percentage')
13
- return false unless context.instance_of?(Unleash::Context)
14
- return false if context.user_id.nil? || context.user_id.empty?
15
-
16
- percentage = Integer(params['percentage'] || 0)
17
- (percentage.positive? && Util.get_normalized_number(context.user_id, params['groupId'] || "", 0) <= percentage)
18
- end
19
- end
20
- end
21
- end
@@ -1,36 +0,0 @@
1
- module Unleash
2
- module Strategy
3
- class RemoteAddress < Base
4
- PARAM = 'IPs'.freeze
5
-
6
- def name
7
- 'remoteAddress'
8
- end
9
-
10
- # need: params['IPs'], context.remote_address
11
- def is_enabled?(params = {}, context = nil)
12
- return false unless params.is_a?(Hash) && params.has_key?(PARAM)
13
- return false unless params.fetch(PARAM, nil).is_a? String
14
- return false unless context.instance_of?(Unleash::Context)
15
-
16
- remote_address = ipaddr_or_nil_from_str(context.remote_address)
17
-
18
- params[PARAM]
19
- .split(',')
20
- .map(&:strip)
21
- .map{ |ipblock| ipaddr_or_nil_from_str(ipblock) }
22
- .compact
23
- .map{ |ipb| ipb.include? remote_address }
24
- .any?
25
- end
26
-
27
- private
28
-
29
- def ipaddr_or_nil_from_str(ip)
30
- IPAddr.new(ip)
31
- rescue StandardError
32
- nil
33
- end
34
- end
35
- end
36
- end
@@ -1,20 +0,0 @@
1
- module Unleash
2
- module Strategy
3
- class UserWithId < Base
4
- PARAM = 'userIds'.freeze
5
-
6
- def name
7
- 'userWithId'
8
- end
9
-
10
- # requires: params['userIds'], context.user_id,
11
- def is_enabled?(params = {}, context = nil)
12
- return false unless params.is_a?(Hash) && params.has_key?(PARAM)
13
- return false unless params.fetch(PARAM, nil).is_a? String
14
- return false unless context.instance_of?(Unleash::Context)
15
-
16
- params[PARAM].split(",").map(&:strip).include?(context.user_id)
17
- end
18
- end
19
- end
20
- end
@@ -1,17 +0,0 @@
1
- require 'murmurhash3'
2
-
3
- module Unleash
4
- module Strategy
5
- module Util
6
- module_function
7
-
8
- NORMALIZER = 100
9
- VARIANT_NORMALIZER_SEED = 86_028_157
10
-
11
- # convert the two strings () into a number between 1 and base (100 by default)
12
- def get_normalized_number(identifier, group_id, seed, base = NORMALIZER)
13
- MurmurHash3::V32.str_hash("#{group_id}:#{identifier}", seed) % base + 1
14
- end
15
- end
16
- end
17
- end
@@ -1,26 +0,0 @@
1
- require 'unleash/variant_override'
2
-
3
- module Unleash
4
- class VariantDefinition
5
- attr_accessor :name, :weight, :payload, :overrides, :stickiness
6
-
7
- def initialize(name, weight = 0, payload = nil, stickiness = nil, overrides = []) # rubocop:disable Metrics/ParameterLists
8
- self.name = name
9
- self.weight = weight
10
- self.payload = payload
11
- self.stickiness = stickiness
12
- self.overrides = (overrides || [])
13
- .select{ |v| v.is_a?(Hash) && v.has_key?('contextName') }
14
- .map{ |v| VariantOverride.new(v.fetch('contextName', ''), v.fetch('values', [])) } || []
15
- end
16
-
17
- def override_matches_context?(context)
18
- self.overrides.select{ |o| o.matches_context?(context) }.first
19
- end
20
-
21
- def to_s
22
- "<VariantDefinition: name=#{self.name},weight=#{self.weight},payload=#{self.payload},stickiness=#{self.stickiness}" \
23
- ",overrides=#{self.overrides}>"
24
- end
25
- end
26
- end
@@ -1,44 +0,0 @@
1
- module Unleash
2
- class VariantOverride
3
- attr_accessor :context_name, :values
4
-
5
- def initialize(context_name, values = [])
6
- self.context_name = context_name
7
- self.values = values || []
8
-
9
- validate
10
- end
11
-
12
- def to_s
13
- "<VariantOverride: context_name=#{self.context_name},values=#{self.values}>"
14
- end
15
-
16
- def matches_context?(context)
17
- raise ArgumentError, 'context must be of class Unleash::Context' unless context.instance_of?(Unleash::Context)
18
-
19
- context_value =
20
- case self.context_name
21
- when 'userId'
22
- context.user_id
23
- when 'sessionId'
24
- context.session_id
25
- when 'remoteAddress'
26
- context.remote_address
27
- else
28
- context.properties.fetch(self.context_name, nil)
29
- end
30
-
31
- Unleash.logger.debug "VariantOverride: context_name: #{context_name} context_value: #{context_value}"
32
-
33
- self.values.include? context_value.to_s
34
- end
35
-
36
- private
37
-
38
- def validate
39
- raise ArgumentError, 'context_name must be a String' unless self.context_name.is_a?(String)
40
- raise ArgumentError, 'values must be an Array of strings' unless self.values.is_a?(Array) \
41
- && self.values.reject{ |v| v.is_a?(String) }.empty?
42
- end
43
- end
44
- end