Class: SplitIoClient::SplitClient

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

Instance Method Summary collapse

Constructor Details

#initialize(api_key, metrics, splits_repository, segments_repository, impressions_repository, metrics_repository, events_repository, sdk_blocker, config, impressions_manager) ⇒ SplitIoClient

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

Parameters:

  • api_key (String)

    the API key for your split account



12
13
14
15
16
17
18
19
20
21
22
23
24
# File 'lib/splitclient-rb/clients/split_client.rb', line 12

def initialize(api_key, metrics, splits_repository, segments_repository, impressions_repository, metrics_repository, events_repository, sdk_blocker, config, impressions_manager)
  @api_key = api_key
  @metrics = metrics
  @splits_repository = splits_repository
  @segments_repository = segments_repository
  @impressions_repository = impressions_repository
  @metrics_repository = metrics_repository
  @events_repository = events_repository
  @sdk_blocker = sdk_blocker
  @destroyed = false
  @config = config
  @impressions_manager = impressions_manager
end

Instance Method Details

#block_until_ready(time = nil) ⇒ Object



153
154
155
# File 'lib/splitclient-rb/clients/split_client.rb', line 153

def block_until_ready(time = nil)
  @sdk_blocker.block(time) if @sdk_blocker && !@sdk_blocker.ready?
end

#destroyObject



64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
# File 'lib/splitclient-rb/clients/split_client.rb', line 64

def destroy
  @config.logger.info('Split client shutdown started...') if @config.debug_enabled

  @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

  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 = {}, split_data = nil, store_impressions = true, multiple = false, evaluator = nil) ⇒ Object



26
27
28
29
30
31
32
33
34
35
36
37
38
39
# File 'lib/splitclient-rb/clients/split_client.rb', line 26

def get_treatment(
    key, split_name, attributes = {}, split_data = nil, store_impressions = true,
    multiple = false, evaluator = nil
  )
  impressions = []
  result = treatment(key, split_name, attributes, split_data, store_impressions, multiple, evaluator, 'get_treatment', impressions)
  @impressions_manager.track(impressions)

  if multiple
    result.tap { |t| t.delete(:config) }
  else
    result[:treatment]
  end
end

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



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

def get_treatment_with_config(
    key, split_name, attributes = {}, split_data = nil, store_impressions = true,
    multiple = false, evaluator = nil
  )
  impressions = []
  result = treatment(key, split_name, attributes, split_data, store_impressions, multiple, evaluator, 'get_treatment_with_config', impressions)
  @impressions_manager.track(impressions)

  result
end

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



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

def get_treatments(key, split_names, attributes = {})
  treatments = treatments(key, split_names, attributes)
  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 = {}) ⇒ Object



60
61
62
# File 'lib/splitclient-rb/clients/split_client.rb', line 60

def get_treatments_with_config(key, split_names, attributes = {})
  treatments(key, split_names, attributes,'get_treatments_with_config')
end

#keys_from_key(key) ⇒ Object



114
115
116
117
118
119
120
121
# File 'lib/splitclient-rb/clients/split_client.rb', line 114

def keys_from_key(key)
  case key
  when Hash
    key.values_at(:bucketing_key, :matching_key).map { |k| k.nil? ? nil : k }
  else
    [nil, key].map { |k| k.nil? ? nil : k }
  end
end

#parsed_treatment(multiple, treatment_data) ⇒ Object



123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
# File 'lib/splitclient-rb/clients/split_client.rb', line 123

def parsed_treatment(multiple, treatment_data)
  if multiple
  {
    treatment: treatment_data[:treatment],
    label: treatment_data[:label],
    change_number: treatment_data[:change_number],
    config: treatment_data[:config]
  }
  else
    {
      treatment: treatment_data[:treatment],
      config: treatment_data[:config]
    }
  end
end

#sanitize_split_names(calling_method, split_names) ⇒ Object



139
140
141
142
143
144
145
146
147
148
149
150
151
# File 'lib/splitclient-rb/clients/split_client.rb', line 139

def sanitize_split_names(calling_method, split_names)
  split_names.compact.uniq.select do |split_name|
    if (split_name.is_a?(String) || split_name.is_a?(Symbol)) && !split_name.empty?
      true
    elsif split_name.is_a?(String) && split_name.empty?
      @config.logger.warn("#{calling_method}: you passed an empty split_name, split_name must be a non-empty String or a Symbol")
      false
    else
      @config.logger.warn("#{calling_method}: you passed an invalid split_name, split_name must be a non-empty String or a Symbol")
      false
    end
  end
end

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



85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
# File 'lib/splitclient-rb/clients/split_client.rb', line 85

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)

  properties_size = EVENT_AVERAGE_SIZE

  if !properties.nil?
    properties, size = validate_properties(properties)
    properties_size += size
    if (properties_size > EVENTS_SIZE_THRESHOLD)
      @config.logger.error("The maximum size allowed for the properties is #{EVENTS_SIZE_THRESHOLD}. Current is #{properties_size}. Event not queued")
      return false
    end
  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 Splits in this environment, make sure you're tracking " \
      'your events to a valid traffic type defined in the Split console')
  end

  begin
    @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)
    true
  rescue StandardError => error
    @config.log_found_exception(__method__.to_s, error)
    false
  end
end