Top Level Namespace

Defined Under Namespace

Modules: TestlabSdkRuby Classes: Client, Config

Instance Method Summary collapse

Instance Method Details

#get_variant(features, name, user_id) ⇒ Object



50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
# File 'lib/testlab_sdk_ruby/testlab_feature_logic.rb', line 50

def get_variant(features, name, user_id)
  hashed_id = hash_message(user_id)
  puts "uuid, hashed #{user_id}, #{hashed_id}"

  feature =
    features["experiments"]
      .concat(features["toggles"], features["rollouts"])
      .find { |f| f["name"] == name }
  return false unless feature

  variants = feature["variant_arr"]
  blocks = features["userblocks"]
  block_id = (hashed_id * blocks.length).ceil

  target_block =
    blocks
      .filter { |b| b["id"] == block_id && b["feature_id"] == feature["id"] }
      .first

  return false unless target_block

  segment_end = target_block["id"].to_f / blocks.length
  segment_start = segment_end - 1.0 / blocks.length

  running_total = segment_start
  variants.each do |variant|
    running_total += variant["weight"].to_f * (1.0 / blocks.length)
    if hashed_id <= running_total
      return { id: variant["id"], value: variant["value"] }
    end
  end

  false
end

#hash_message(message) ⇒ Object



1
2
3
4
5
6
7
8
# File 'lib/testlab_sdk_ruby/testlab_feature_logic.rb', line 1

def hash_message(message)
  hash = 0
  message.each_char do |c|
    hash = (hash << 5) - hash + c.ord
    hash &= 0xFFFFFFFF # Convert to 32-bit integer
  end
  (hash.to_f / 2**32).abs # Scale to [0, 1]
end

#is_active?(start_date, end_date) ⇒ Boolean

Returns:

  • (Boolean)


10
11
12
13
# File 'lib/testlab_sdk_ruby/testlab_feature_logic.rb', line 10

def is_active?(start_date, end_date)
  current_date = DateTime.now
  current_date >= start_date && current_date <= end_date
end

#is_enabled(features, name, user_id) ⇒ Object



15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
# File 'lib/testlab_sdk_ruby/testlab_feature_logic.rb', line 15

def is_enabled(features, name, user_id)
  # Find target feature based on name
  feature =
    features["experiments"]
      .concat(features["toggles"], features["rollouts"])
      .find { |f| f["name"] == name }
  # feature = features.find { |f| f["name"] == name }
  return false unless feature

  # Return false if current date is outside of date range for feature
  start_date = DateTime.parse(feature["start_date"])
  end_date = DateTime.parse(feature["end_date"])

  return false unless is_active?(start_date, end_date) && feature["is_running"]

  # Return false if feature is not running (toggled off) or if the hashed ID is outside of the target user_percentage range
  # For Type 3 (features), users can only be assigned to one feature (total percentage of users enrolled in features can not exceed 100%)

  case feature["type_id"]
  when 2
    hashed_id = hash_message(user_id + name)
    feature["is_running"] && hashed_id < feature["user_percentage"]
  when 3
    hashed_id = hash_message(user_id)
    blocks = features["userblocks"]
    block_id = (hashed_id * blocks.length).ceil

    !blocks
      .filter { |b| b["id"] == block_id && b["feature_id"] == feature["id"] }
      .empty?
  else
    false
  end
end