Class: SplitIoClient::SplitClient

Inherits:
Object
  • Object
show all
Defined in:
lib/splitclient-rb/clients/split_client.rb

Instance Method Summary collapse

Constructor Details

#initialize(sdk_key, repositories, status_manager, config, impressions_manager, telemetry_evaluation_producer, evaluator, split_validator, fallback_treatment_calculator, events_manager) ⇒ SplitIoClient

Creates a new split client instance that connects to split.io API.

Parameters:

  • sdk_key (String)

    the SDK key for your split account



21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
# File 'lib/splitclient-rb/clients/split_client.rb', line 21

def initialize(sdk_key, repositories, status_manager, config, impressions_manager, telemetry_evaluation_producer, evaluator, split_validator, fallback_treatment_calculator, events_manager)
  @api_key = sdk_key
  @splits_repository = repositories[:splits]
  @segments_repository = repositories[:segments]
  @rule_based_segments_repository = repositories[:rule_based_segments]
  @impressions_repository = repositories[:impressions]
  @events_repository = repositories[:events]
  @status_manager = status_manager
  @destroyed = false
  @config = config
  @impressions_manager = impressions_manager
  @telemetry_evaluation_producer = telemetry_evaluation_producer
  @split_validator = split_validator
  @evaluator = evaluator
  @fallback_treatment_calculator = fallback_treatment_calculator
  @events_manager = events_manager
end

Instance Method Details

#block_until_ready(time = nil) ⇒ Object



176
177
178
# File 'lib/splitclient-rb/clients/split_client.rb', line 176

def block_until_ready(time = nil)
  @status_manager.wait_until_ready(time) if @status_manager
end

#destroyObject



117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
# File 'lib/splitclient-rb/clients/split_client.rb', line 117

def destroy
  @config.logger.info('Split client shutdown started...') if @config.debug_enabled
  if !@config.cache_adapter.is_a?(SplitIoClient::Cache::Adapters::RedisAdapter) && @config.impressions_mode != :none &&
      (!@impressions_repository.empty? || !@events_repository.empty?)
    @config.logger.debug("Impressions and/or Events cache is not empty") if @config.debug_enabled
    # Adding small delay to ensure sender threads are fully running
    sleep(0.1)
    if !@config.threads.key?(:impressions_sender) || !@config.threads.key?(:events_sender)
      @config.logger.debug("Periodic data recording thread has not started yet, waiting for service startup.") if @config.debug_enabled
      @config.threads[:start_sdk].join(5) if @config.threads.key?(:start_sdk)
    end
  end
  @config.threads.select { |name, thread| name.to_s.end_with? 'sender' }.values.each do |thread|
    thread.raise(SplitIoClient::SDKShutdownException)
    thread.join
  end

  @config.threads.values.each { |thread| Thread.kill(thread) }

  @splits_repository.clear
  @segments_repository.clear
  @rule_based_segments_repository.clear

  SplitIoClient.load_factory_registry
  SplitIoClient.split_factory_registry.remove(@api_key)

  @config.logger.info('Split client shutdown complete') if @config.debug_enabled
  @config.valid_mode = false
  @destroyed = true
end

#get_treatment(key, split_name, attributes = {}, evaluation_options = nil, split_data = nil, store_impressions = nil, multiple = false, evaluator = nil) ⇒ Object



39
40
41
42
43
44
45
46
47
48
# File 'lib/splitclient-rb/clients/split_client.rb', line 39

def get_treatment(
    key, split_name, attributes = {}, evaluation_options = nil, split_data = nil, store_impressions = nil,
    multiple = false, evaluator = nil
  )
  log_deprecated_warning(GET_TREATMENT, evaluator, 'evaluator')

  result = treatment(key, split_name, attributes, split_data, store_impressions, GET_TREATMENT, multiple, evaluation_options)
  return result.tap { |t| t.delete(:config) } if multiple
  result[:treatment]
end

#get_treatment_with_config(key, split_name, attributes = {}, evaluation_options = nil, split_data = nil, store_impressions = nil, multiple = false, evaluator = nil) ⇒ Object



50
51
52
53
54
55
56
57
58
# File 'lib/splitclient-rb/clients/split_client.rb', line 50

def get_treatment_with_config(
    key, split_name, attributes = {}, evaluation_options = nil, split_data = nil, store_impressions = nil,
    multiple = false, evaluator = nil
  )
  log_deprecated_warning(GET_TREATMENT, evaluator, 'evaluator')
  result = treatment(key, split_name, attributes, split_data, store_impressions, GET_TREATMENT_WITH_CONFIG, multiple, evaluation_options)

  { :config => result[:config], :treatment => result[:treatment] }
end

#get_treatments(key, split_names, attributes = {}, evaluation_options = nil) ⇒ Object



60
61
62
63
64
65
66
67
# File 'lib/splitclient-rb/clients/split_client.rb', line 60

def get_treatments(key, split_names, attributes = {}, evaluation_options = nil)
  treatments = treatments(key, split_names, attributes, evaluation_options)

  return treatments if treatments.nil?
  keys = treatments.keys
  treats = treatments.map { |_,t| t[:treatment]  }
  Hash[keys.zip(treats)]
end

#get_treatments_by_flag_set(key, flag_set, attributes = {}, evaluation_options = nil) ⇒ Object



77
78
79
80
81
82
83
84
85
# File 'lib/splitclient-rb/clients/split_client.rb', line 77

def get_treatments_by_flag_set(key, flag_set, attributes = {}, evaluation_options = nil)
  valid_flag_set = @split_validator.valid_flag_sets(GET_TREATMENTS_BY_FLAG_SET, [flag_set])
  split_names = @splits_repository.get_feature_flags_by_sets(valid_flag_set)
  treatments = treatments(key, split_names, attributes, evaluation_options, GET_TREATMENTS_BY_FLAG_SET)
  return treatments if treatments.nil?
  keys = treatments.keys
  treats = treatments.map { |_,t| t[:treatment]  }
  Hash[keys.zip(treats)]
end

#get_treatments_by_flag_sets(key, flag_sets, attributes = {}, evaluation_options = nil) ⇒ Object



87
88
89
90
91
92
93
94
95
# File 'lib/splitclient-rb/clients/split_client.rb', line 87

def get_treatments_by_flag_sets(key, flag_sets, attributes = {}, evaluation_options = nil)
  valid_flag_set = @split_validator.valid_flag_sets(GET_TREATMENTS_BY_FLAG_SETS, flag_sets)
  split_names = @splits_repository.get_feature_flags_by_sets(valid_flag_set)
  treatments = treatments(key, split_names, attributes, evaluation_options, GET_TREATMENTS_BY_FLAG_SETS)
  return treatments if treatments.nil?
  keys = treatments.keys
  treats = treatments.map { |_,t| t[:treatment]  }
  Hash[keys.zip(treats)]
end

#get_treatments_with_config(key, split_names, attributes = {}, evaluation_options = nil) ⇒ Object



69
70
71
72
73
74
75
# File 'lib/splitclient-rb/clients/split_client.rb', line 69

def get_treatments_with_config(key, split_names, attributes = {}, evaluation_options = nil)
  results = treatments(key, split_names, attributes, evaluation_options, GET_TREATMENTS_WITH_CONFIG)

  results.map{|key, value|
    [key, { treatment: value[:treatment], config: value[:config] }]
  }.to_h
end

#get_treatments_with_config_by_flag_set(key, flag_set, attributes = {}, evaluation_options = nil) ⇒ Object



97
98
99
100
101
102
103
104
105
# File 'lib/splitclient-rb/clients/split_client.rb', line 97

def get_treatments_with_config_by_flag_set(key, flag_set, attributes = {}, evaluation_options = nil)
  valid_flag_set = @split_validator.valid_flag_sets(GET_TREATMENTS_WITH_CONFIG_BY_FLAG_SET, [flag_set])
  split_names = @splits_repository.get_feature_flags_by_sets(valid_flag_set)
  results = treatments(key, split_names, attributes, evaluation_options, GET_TREATMENTS_WITH_CONFIG_BY_FLAG_SET)

  results.map{|key, value|
    [key, { treatment: value[:treatment], config: value[:config] }]
  }.to_h
end

#get_treatments_with_config_by_flag_sets(key, flag_sets, attributes = {}, evaluation_options = nil) ⇒ Object



107
108
109
110
111
112
113
114
115
# File 'lib/splitclient-rb/clients/split_client.rb', line 107

def get_treatments_with_config_by_flag_sets(key, flag_sets, attributes = {}, evaluation_options = nil)
  valid_flag_set = @split_validator.valid_flag_sets(GET_TREATMENTS_WITH_CONFIG_BY_FLAG_SETS, flag_sets)
  split_names = @splits_repository.get_feature_flags_by_sets(valid_flag_set)
  results = treatments(key, split_names, attributes, evaluation_options, GET_TREATMENTS_WITH_CONFIG_BY_FLAG_SETS)

  results.map{|key, value|
    [key, { treatment: value[:treatment], config: value[:config] }]
  }.to_h
end

#register(sdk_event, handler) ⇒ Object



180
181
182
# File 'lib/splitclient-rb/clients/split_client.rb', line 180

def register(sdk_event, handler)
  @events_manager.register(sdk_event, handler)
end

#track(key, traffic_type_name, event_type, value = nil, properties = nil) ⇒ Object



148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
# File 'lib/splitclient-rb/clients/split_client.rb', line 148

def track(key, traffic_type_name, event_type, value = nil, properties = nil)
  return false unless valid_client && @config.split_validator.valid_track_parameters(key, traffic_type_name, event_type, value, properties)

  start = Time.now
  properties_size = EVENT_AVERAGE_SIZE

  if !properties.nil?
    properties, size = validate_properties(properties)
    properties_size += size
    return false unless check_properties_size(properties_size)
  end

  if ready? && !@config.localhost_mode && !@splits_repository.traffic_type_exists(traffic_type_name)
    @config.logger.warn("track: Traffic Type #{traffic_type_name} " \
      "does not have any corresponding feature flags in this environment, make sure you're tracking " \
      'your events to a valid traffic type defined in the Split user interface')
  end

  @events_repository.add(key.to_s, traffic_type_name.downcase, event_type.to_s, (Time.now.to_f * 1000).to_i, value, properties, properties_size)
  record_latency(TRACK, start)
  true
rescue StandardError => e
  @config.log_found_exception(__method__.to_s, e)
  record_exception(TRACK)

  false
end

#unregister(sdk_event, handler) ⇒ Object



184
185
186
# File 'lib/splitclient-rb/clients/split_client.rb', line 184

def unregister(sdk_event, handler)
  @events_manager.unregister(sdk_event)
end