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.



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

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
43
# 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(3000, "There is an existing client instance for the specified SDK Key. " \
                              "No new client instance will be created and the specified options are ignored. " \
                              "Returning the existing client instance. SDK Key: '#{sdk_key}'.")
      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.



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

def clear_default_user
  @_default_user = nil
end

#closeObject

Closes the underlying resources.



297
298
299
300
301
302
# File 'lib/configcat/configcatclient.rb', line 297

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]



258
259
260
261
262
263
# File 'lib/configcat/configcatclient.rb', line 258

def force_refresh
  return @_config_service.refresh if @_config_service

  return RefreshResult.new(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.



151
152
153
154
155
156
157
158
159
# File 'lib/configcat/configcatclient.rb', line 151

def get_all_keys
  config, _ = _get_config()
  if config.nil?
    @log.error(1000, "Config JSON is not present. Returning empty list.")
    return []
  end
  settings = config.fetch(FEATURE_FLAGS, {})
  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



238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
# File 'lib/configcat/configcatclient.rb', line 238

def get_all_value_details(user = nil)
  config, fetch_time = _get_config()
  if config.nil?
    @log.error(1000, "Config JSON is not present. Returning empty list.")
    return []
  end

  details_result = []
  settings = config.fetch(FEATURE_FLAGS, {})
  for key in settings.keys
    details = _evaluate(key, user, nil, nil, config, 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



216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
# File 'lib/configcat/configcatclient.rb', line 216

def get_all_values(user = nil)
  config, _ = _get_config()
  if config.nil?
    @log.error(1000, "Config JSON is not present. Returning empty dictionary.")
    return {}
  end

  settings = config.fetch(FEATURE_FLAGS, {})
  all_values = {}
  for key in settings.keys
    value = get_value(key, nil, user)
    if !value.equal?(nil)
      all_values[key] = value
    end
  end
  return all_values
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



165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
# File 'lib/configcat/configcatclient.rb', line 165

def get_key_and_value(variation_id)
  config, _ = _get_config()
  if config.nil?
    @log.error(1000, "Config JSON is not present. Returning nil.")
    return nil
  end

  settings = config.fetch(FEATURE_FLAGS, {})
  begin
    settings.each do |key, value|
      setting_type = value.fetch(SETTING_TYPE, nil)
      if variation_id == value.fetch(VARIATION_ID, nil)
        return KeyValue.new(key, Config.get_value(value, setting_type))
      end

      targeting_rules = value.fetch(TARGETING_RULES, [])
      targeting_rules.each do |targeting_rule|
        served_value = targeting_rule.fetch(SERVED_VALUE, nil)
        if !served_value.nil?
          if variation_id == served_value.fetch(VARIATION_ID, nil)
            return KeyValue.new(key, Config.get_value(served_value, setting_type))
          end
        else
          percentage_options = targeting_rule.fetch(PERCENTAGE_OPTIONS, [])
          percentage_options.each do |percentage_option|
            if variation_id == percentage_option.fetch(VARIATION_ID, nil)
              return KeyValue.new(key, Config.get_value(percentage_option, setting_type))
            end
          end
        end
      end

      percentage_options = value.fetch(PERCENTAGE_OPTIONS, [])
      percentage_options.each do |percentage_option|
        if variation_id == percentage_option.fetch(VARIATION_ID, nil)
          return KeyValue.new(key, Config.get_value(percentage_option, setting_type))
        end
      end
    end
  rescue => e
    @log.error("Error occurred in the `#{self.class.name}` method. Returning nil.", event_id: 1002)
    return nil
  end

  @log.error(2011, "Could not find the setting for the specified 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.



117
118
119
120
121
122
123
124
125
126
127
# File 'lib/configcat/configcatclient.rb', line 117

def get_value(key, default_value, user = nil)
  config, fetch_time = _get_config()
  if config.nil? || config[FEATURE_FLAGS].nil?
    message = "Config JSON is not present when evaluating setting '#{key}'. Returning the `default_value` parameter that you specified in your application: '#{default_value}'."
    @log.error(1000, 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, config, 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.



135
136
137
138
139
140
141
142
143
144
145
146
# File 'lib/configcat/configcatclient.rb', line 135

def get_value_details(key, default_value, user = nil)
  config, fetch_time = _get_config()
  if config.nil? || config[FEATURE_FLAGS].nil?
    message = "Config JSON is not present when evaluating setting '#{key}'. Returning the `default_value` parameter that you specified in your application: '#{default_value}'."
    @log.error(1000, message)
    details = EvaluationDetails.from_error(key, default_value, error: message)
    @hooks.invoke_on_flag_evaluated(details)
    return details
  end
  details = _evaluate(key, user, default_value, nil, config, fetch_time)
  return details
end

#offline?Boolean

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

Returns:

  • (Boolean)


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

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.



268
269
270
# File 'lib/configcat/configcatclient.rb', line 268

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.



287
288
289
# File 'lib/configcat/configcatclient.rb', line 287

def set_offline
  @_config_service.set_offline if @_config_service
end

#set_onlineObject

Configures the SDK to allow HTTP requests.



278
279
280
281
282
283
284
# File 'lib/configcat/configcatclient.rb', line 278

def set_online
  if @_config_service
    @_config_service.set_online
  else
    @log.warn(3202, "Client is configured to use the `LOCAL_ONLY` override behavior, thus `set_online()` has no effect.")
  end
end