Class: StatsigDriver

Inherits:
Object
  • Object
show all
Defined in:
lib/statsig_driver.rb

Instance Method Summary collapse

Constructor Details

#initialize(secret_key, options = nil) ⇒ StatsigDriver

Returns a new instance of StatsigDriver.



11
12
13
14
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
# File 'lib/statsig_driver.rb', line 11

def initialize(secret_key, options = nil)
  super()
  if !secret_key.is_a?(String) || !secret_key.start_with?('secret-')
    raise 'Invalid secret key provided. Provide your project secret key from the Statsig console'
  end
  if !options.nil? && !options.instance_of?(StatsigOptions)
    raise 'Invalid options provided. Either provide a valid StatsigOptions object or nil'
  end

  @options = options || StatsigOptions.new()
  @shutdown = false
  @secret_key = secret_key
  @net = Network.new(secret_key, @options.api_url_base)
  @statsig_metadata = {
    'sdkType' => 'ruby-server',
    'sdkVersion' => Gem::Specification::load('statsig.gemspec')&.version,
  }
  @logger = StatsigLogger.new(@net, @statsig_metadata)

  downloaded_specs = @net.download_config_specs
  unless downloaded_specs.nil?
    @initialized = true
  end

  @store = SpecStore.new(downloaded_specs)
  @evaluator = Evaluator.new(@store)

  @polling_thread = @net.poll_for_changes(-> (config_specs) { @store.process(config_specs) })
end

Instance Method Details

#check_gate(user, gate_name) ⇒ Object



41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
# File 'lib/statsig_driver.rb', line 41

def check_gate(user, gate_name)
  validate_user(user)
  user = normalize_user(user)
  if !gate_name.is_a?(String) || gate_name.empty?
    raise 'Invalid gate_name provided'
  end
  check_shutdown
  unless @initialized
    return false
  end

  res = @evaluator.check_gate(user, gate_name)
  if res.nil?
    res = ConfigResult.new(gate_name)
  end

  if res == $fetch_from_server
    res = check_gate_fallback(user, gate_name)
  end

  @logger.log_gate_exposure(user, res.name, res.gate_value, res.rule_id)
  res.gate_value
end

#get_config(user, dynamic_config_name) ⇒ Object



65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
# File 'lib/statsig_driver.rb', line 65

def get_config(user, dynamic_config_name)
  validate_user(user)
  user = normalize_user(user)
  if !dynamic_config_name.is_a?(String) || dynamic_config_name.empty?
    raise "Invalid dynamic_config_name provided"
  end
  check_shutdown
  unless @initialized
    return DynamicConfig.new(dynamic_config_name)
  end

  res = @evaluator.get_config(user, dynamic_config_name)
  if res.nil?
    res = ConfigResult.new(dynamic_config_name)
  end

  if res == $fetch_from_server
    res = get_config_fallback(user, dynamic_config_name)
  end

  result_config = DynamicConfig.new(res.name, res.json_value, res.rule_id)
  @logger.log_config_exposure(user, result_config.name, result_config.rule_id)
  result_config
end

#log_event(user, event_name, value = nil, metadata = nil) ⇒ Object



90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
# File 'lib/statsig_driver.rb', line 90

def log_event(user, event_name, value = nil,  = nil)
  if !user.nil? && !user.instance_of?(StatsigUser)
    raise 'Must provide a valid StatsigUser or nil'
  end
  check_shutdown

  user = normalize_user(user)

  event = StatsigEvent.new(event_name)
  event.user = user&.serialize
  event.value = value
  event. = 
  event. = @statsig_metadata
  @logger.log_event(event)
end

#shutdownObject



106
107
108
109
110
# File 'lib/statsig_driver.rb', line 106

def shutdown
  @shutdown = true
  @logger.flush
  @polling_thread&.exit
end