Class: ConfigCat::ConfigCatClient

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

Constant Summary collapse

@@lock =
Mutex.new
@@instances =
{}

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#hooksObject (readonly)

Returns the value of attribute hooks.



16
17
18
# File 'lib/configcat/configcatclient.rb', line 16

def hooks
  @hooks
end

#logObject (readonly)

Returns the value of attribute log.



16
17
18
# File 'lib/configcat/configcatclient.rb', line 16

def log
  @log
end

Class Method Details

.close_allObject

Closes all ConfigCatClient instances.



45
46
47
48
49
50
51
52
# File 'lib/configcat/configcatclient.rb', line 45

def self.close_all
  @@lock.synchronize do
    @@instances.each do |key, value|
      value.send(:_close_resources)
    end
    @@instances.clear
  end
end

.get(sdk_key, options = nil) ⇒ Object

Creates a new or gets an already existing ConfigCatClient for the given sdk_key.

:param sdk_key [String] ConfigCat SDK Key to access your configuration. :param options [ConfigCatOptions] Configuration for ConfigCatClient. :return [ConfigCatClient] the ConfigCatClient instance.



26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
# File 'lib/configcat/configcatclient.rb', line 26

def self.get(sdk_key, options = nil)
  @@lock.synchronize do
    client = @@instances[sdk_key]
    if client
      if options
        client.log.warn("Client for sdk_key `#{sdk_key}` is already created and will be reused; " +
                        "options passed are being ignored.")
      end
      return client
    end

    options ||= ConfigCatOptions.new
    client = ConfigCatClient.new(sdk_key, options)
    @@instances[sdk_key] = client
    return client
  end
end

Instance Method Details

#clear_default_userObject

Sets the default user to nil.



279
280
281
# File 'lib/configcat/configcatclient.rb', line 279

def clear_default_user
  @_default_user = nil
end

#closeObject

Closes the underlying resources.



301
302
303
304
305
306
# File 'lib/configcat/configcatclient.rb', line 301

def close
  @@lock.synchronize do
    _close_resources
    @@instances.delete(@_sdk_key)
  end
end

#force_refreshObject

Initiates a force refresh on the cached configuration.

:return [RefreshResult]



264
265
266
267
268
269
# File 'lib/configcat/configcatclient.rb', line 264

def force_refresh
  return @_config_service.refresh if @_config_service

  return RefreshResult(false,
                       "The SDK uses the LocalOnly flag override behavior which prevents making HTTP requests.")
end

#get_all_keysObject

Gets all setting keys.

:return list of keys.



140
141
142
143
144
145
146
# File 'lib/configcat/configcatclient.rb', line 140

def get_all_keys
  settings, _ = _get_settings()
  if settings === nil
    return []
  end
  return settings.keys
end

#get_all_value_details(user = nil) ⇒ Object

Gets the values along with evaluation details of all feature flags and settings.

:param user [User] the user object to identify the caller. :return list of all evaluation details



245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
# File 'lib/configcat/configcatclient.rb', line 245

def get_all_value_details(user = nil)
  settings, fetch_time = _get_settings()
  if settings.nil?
    @log.error("Evaluating get_all_value_details() failed. Cache is empty. Returning empty list.")
    return []
  end

  details_result = []
  for key in settings.keys
    details = _evaluate(key, user, nil, nil, settings, fetch_time)
    details_result.push(details)
  end

  return details_result
end

#get_all_values(user = nil) ⇒ Object

Evaluates and returns the values of all feature flags and settings.

:param user [User] the user object to identify the caller. :return dictionary of values



229
230
231
232
233
234
235
236
237
238
239
# File 'lib/configcat/configcatclient.rb', line 229

def get_all_values(user = nil)
  keys = get_all_keys()
  all_values = {}
  for key in keys
    value = get_value(key, nil, user)
    if !value.equal?(nil)
      all_values[key] = value
    end
  end
  return all_values
end

#get_all_variation_ids(user = nil) ⇒ Object

Gets the Variation IDs (analytics) of all feature flags or settings.

:param user [User] the user object to identify the caller. :return list of variation IDs



176
177
178
179
180
181
182
183
184
185
186
187
188
189
# File 'lib/configcat/configcatclient.rb', line 176

def get_all_variation_ids(user = nil)
  @log.warn("get_all_variation_ids is deprecated and will be removed in a future major version. "\
            "Please use [get_value_details] instead.")

  keys = get_all_keys()
  variation_ids = []
  for key in keys
    variation_id = get_variation_id(key, nil, user)
    if !variation_id.equal?(nil)
      variation_ids.push(variation_id)
    end
  end
  return variation_ids
end

#get_key_and_value(variation_id) ⇒ Object

Gets the key of a setting, and it’s value identified by the given Variation ID (analytics)

:param variation_id [String] variation ID :return key and value



195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
# File 'lib/configcat/configcatclient.rb', line 195

def get_key_and_value(variation_id)
  settings, _ = _get_settings()
  if settings === nil
    @log.warn("Evaluating get_key_and_value('%s') failed. Cache is empty. Returning nil." % variation_id)
    return nil
  end

  for key, value in settings
    if variation_id == value.fetch(VARIATION_ID, nil)
      return KeyValue.new(key, value[VALUE])
    end

    rollout_rules = value.fetch(ROLLOUT_RULES, [])
    for rollout_rule in rollout_rules
      if variation_id == rollout_rule.fetch(VARIATION_ID, nil)
        return KeyValue.new(key, rollout_rule[VALUE])
      end
    end

    rollout_percentage_items = value.fetch(ROLLOUT_PERCENTAGE_ITEMS, [])
    for rollout_percentage_item in rollout_percentage_items
      if variation_id == rollout_percentage_item.fetch(VARIATION_ID, nil)
        return KeyValue.new(key, rollout_percentage_item[VALUE])
      end
    end
  end

  @log.error("Could not find the setting for the given variation_id: " + variation_id)
end

#get_value(key, default_value, user = nil) ⇒ Object

Gets the value of a feature flag or setting identified by the given key.

:param key [String] the identifier of the feature flag or setting. :param default_value in case of any failure, this value will be returned. :param user [User] the user object to identify the caller. :return the value.



105
106
107
108
109
110
111
112
113
114
115
116
# File 'lib/configcat/configcatclient.rb', line 105

def get_value(key, default_value, user = nil)
  settings, fetch_time = _get_settings()
  if settings.nil?
    message = "Evaluating get_value('%s') failed. Cache is empty. " \
              "Returning default_value in your get_value call: [%s]." % [key, default_value.to_s]
    @log.error(message)
    @hooks.invoke_on_flag_evaluated(EvaluationDetails.from_error(key, default_value, error: message))
    return default_value
  end
  details = _evaluate(key, user, default_value, nil, settings, fetch_time)
  return details.value
end

#get_value_details(key, default_value, user = nil) ⇒ Object

Gets the value and evaluation details of a feature flag or setting identified by the given key.

:param key [String] the identifier of the feature flag or setting. :param default_value in case of any failure, this value will be returned. :param user [User] the user object to identify the caller. :return [EvaluationDetails] the evaluation details.



124
125
126
127
128
129
130
131
132
133
134
135
# File 'lib/configcat/configcatclient.rb', line 124

def get_value_details(key, default_value, user = nil)
  settings, fetch_time = _get_settings()
  if settings.nil?
    message = "Evaluating get_value_details('%s') failed. Cache is empty. " \
              "Returning default_value in your get_value_details call: [%s]." % [key, default_value.to_s]
    @log.error(message)
    @hooks.invoke_on_flag_evaluated(EvaluationDetails.from_error(key, default_value, error: message))
    return default_value
  end
  details = _evaluate(key, user, default_value, nil, settings, fetch_time)
  return details
end

#get_variation_id(key, default_variation_id, user = nil) ⇒ Object

Gets the Variation ID (analytics) of a feature flag or setting based on it’s key.

:param key [String] the identifier of the feature flag or setting. :param default_variation_id in case of any failure, this value will be returned. :param user [User] the user object to identify the caller. :return the variation ID.



154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
# File 'lib/configcat/configcatclient.rb', line 154

def get_variation_id(key, default_variation_id, user = nil)
  @log.warn("get_variation_id is deprecated and will be removed in a future major version. "\
            "Please use [get_value_details] instead.")

  settings, fetch_time = _get_settings()
  if settings === nil
    message = "Evaluating get_variation_id('%s') failed. Cache is empty. "\
              "Returning default_variation_id in your get_variation_id call: [%s]." %
              [key, default_variation_id.to_s]
    @log.error(message)
    @hooks.invoke_on_flag_evaluated(EvaluationDetails.from_error(key, nil, error: message,
                                                                 variation_id: default_variation_id))
    return default_variation_id
  end
  details = _evaluate(key, user, nil, default_variation_id, settings, fetch_time)
  return details.variation_id
end

#offline?Boolean

Returns true when the SDK is configured not to initiate HTTP requests, otherwise false.

Returns:

  • (Boolean)


296
297
298
# File 'lib/configcat/configcatclient.rb', line 296

def offline?
  return @_config_service ? @_config_service.offline? : true
end

#set_default_user(user) ⇒ Object

Sets the default user.

:param user [User] the user object to identify the caller.



274
275
276
# File 'lib/configcat/configcatclient.rb', line 274

def set_default_user(user)
  @_default_user = user
end

#set_offlineObject

Configures the SDK to not initiate HTTP requests and work only from its cache.



290
291
292
293
# File 'lib/configcat/configcatclient.rb', line 290

def set_offline
  @_config_service.set_offline if @_config_service
  @log.debug('Switched to OFFLINE mode.')
end

#set_onlineObject

Configures the SDK to allow HTTP requests.



284
285
286
287
# File 'lib/configcat/configcatclient.rb', line 284

def set_online
  @_config_service.set_online if @_config_service
  @log.debug('Switched to ONLINE mode.')
end