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, adapter = nil, splits_repository, segments_repository, impressions_repository, metrics_repository, events_repository) ⇒ 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
# File 'lib/splitclient-rb/clients/split_client.rb', line 12

def initialize(api_key, adapter = nil, splits_repository, segments_repository, impressions_repository, metrics_repository, events_repository)
  @splits_repository = splits_repository
  @segments_repository = segments_repository
  @impressions_repository = impressions_repository
  @metrics_repository = metrics_repository
  @events_repository = events_repository
  @destroyed = false

  @adapter = adapter
end

Instance Method Details

#destroy ⇒ Object



54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
# File 'lib/splitclient-rb/clients/split_client.rb', line 54

def destroy
  SplitIoClient.configuration.logger.info('Split client shutdown started...') if SplitIoClient.configuration.debug_enabled

  SplitIoClient.configuration.threads.select { |name, thread| name.to_s.end_with? 'sender' }.values.each do |thread|
    thread.raise(SplitIoClient::SDKShutdownException)
    thread.join
  end

  SplitIoClient.configuration.threads.values.each { |thread| Thread.kill(thread) }

  @splits_repository.clear
  @segments_repository.clear

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

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



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

def get_treatment(
    key, split_name, attributes = {}, split_data = nil, store_impressions = true,
    multiple = false, evaluator = nil
  )
  treatment = treatment(key, split_name, attributes, split_data, store_impressions, multiple, evaluator)
  if multiple
     treatment.tap { |t| t.delete(:config) }
  else
    treatment[:treatment]
  end
end

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



35
36
37
38
39
40
# File 'lib/splitclient-rb/clients/split_client.rb', line 35

def get_treatment_with_config(
    key, split_name, attributes = {}, split_data = nil, store_impressions = true,
    multiple = false, evaluator = nil
  )
  treatment(key, split_name, attributes, split_data, store_impressions, multiple, evaluator, 'get_treatment_with_config')
end

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



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

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



50
51
52
# File 'lib/splitclient-rb/clients/split_client.rb', line 50

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

#impression_router ⇒ Object



113
114
115
# File 'lib/splitclient-rb/clients/split_client.rb', line 113

def impression_router
  @impression_router ||= SplitIoClient::ImpressionRouter.new
end

#keys_from_key(key) ⇒ Object



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

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



149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
# File 'lib/splitclient-rb/clients/split_client.rb', line 149

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

#route_impression(split_name, matching_key, bucketing_key, time, treatment, attributes) ⇒ Object



91
92
93
94
95
96
97
98
99
100
# File 'lib/splitclient-rb/clients/split_client.rb', line 91

def route_impression(split_name, matching_key, bucketing_key, time, treatment, attributes)
  impression_router.add(
    split_name: split_name,
    matching_key: matching_key,
    bucketing_key: bucketing_key,
    time: time,
    treatment: treatment,
    attributes: attributes
  )
end

#route_impressions(split_names, matching_key, bucketing_key, time, treatments_labels_change_numbers, attributes) ⇒ Object



102
103
104
105
106
107
108
109
110
111
# File 'lib/splitclient-rb/clients/split_client.rb', line 102

def route_impressions(split_names, matching_key, bucketing_key, time, treatments_labels_change_numbers, attributes)
  impression_router.add_bulk(
    split_names: split_names,
    matching_key: matching_key,
    bucketing_key: bucketing_key,
    time: time,
    treatments_labels_change_numbers: treatments_labels_change_numbers,
    attributes: attributes
  )
end

#sanitize_split_names(calling_method, split_names) ⇒ Object



165
166
167
168
169
170
171
172
173
174
175
176
177
# File 'lib/splitclient-rb/clients/split_client.rb', line 165

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?
      SplitIoClient.configuration.logger.warn("#{calling_method}: you passed an empty split_name, split_name must be a non-empty String or a Symbol")
      false
    else
      SplitIoClient.configuration.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

#store_impression(split_name, matching_key, bucketing_key, treatment, store_impressions, attributes) ⇒ Object



72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
# File 'lib/splitclient-rb/clients/split_client.rb', line 72

def store_impression(split_name, matching_key, bucketing_key, treatment, store_impressions, attributes)
  time = (Time.now.to_f * 1000.0).to_i

  return if SplitIoClient.configuration.disable_impressions || !store_impressions

  @impressions_repository.add(
    matching_key,
    bucketing_key,
    split_name,
    treatment,
    time
  )

  route_impression(split_name, matching_key, bucketing_key, time, treatment, attributes)

rescue StandardError => error
  SplitIoClient.configuration.log_found_exception(__method__.to_s, error)
end

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



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

def track(key, traffic_type_name, event_type, value = nil, properties = nil)
  return false unless valid_client && SplitIoClient::Validators.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)
      SplitIoClient.configuration.logger.error("The maximum size allowed for the properties is #{EVENTS_SIZE_THRESHOLD}. Current is #{properties_size}. Event not queued")
      return false
    end
  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
    SplitIoClient.configuration.log_found_exception(__method__.to_s, error)
    false
  end
end