Class: ConfigCat::ConfigCatClient

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

Instance Method Summary collapse

Constructor Details

#initialize(sdk_key, poll_interval_seconds: 60, max_init_wait_time_seconds: 5, on_configuration_changed_callback: nil, cache_time_to_live_seconds: 60, config_cache_class: nil, base_url: nil, proxy_address: nil, proxy_port: nil, proxy_user: nil, proxy_pass: nil, data_governance: DataGovernance::GLOBAL) ⇒ ConfigCatClient

Returns a new instance of ConfigCatClient.



13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
# File 'lib/configcat/configcatclient.rb', line 13

def initialize(sdk_key,
               poll_interval_seconds:60,
               max_init_wait_time_seconds:5,
               on_configuration_changed_callback:nil,
               cache_time_to_live_seconds:60,
               config_cache_class:nil,
               base_url:nil,
               proxy_address:nil,
               proxy_port:nil,
               proxy_user:nil,
               proxy_pass:nil,
               data_governance: DataGovernance::GLOBAL)
  if sdk_key === nil
    raise ConfigCatClientException, "SDK Key is required."
  end
  @_sdk_key = sdk_key

  if config_cache_class
    @_config_cache = config_cache_class.new()
  else
    @_config_cache = InMemoryConfigCache.new()
  end

  if poll_interval_seconds > 0
    @_config_fetcher = CacheControlConfigFetcher.new(sdk_key, "p", base_url, proxy_address, proxy_port, proxy_user, proxy_pass, data_governance)
    @_cache_policy = AutoPollingCachePolicy.new(@_config_fetcher, @_config_cache, _get_cache_key(), poll_interval_seconds, max_init_wait_time_seconds, on_configuration_changed_callback)
  else
    if cache_time_to_live_seconds > 0
      @_config_fetcher = CacheControlConfigFetcher.new(sdk_key, "l", base_url, proxy_address, proxy_port, proxy_user, proxy_pass, data_governance)
      @_cache_policy = LazyLoadingCachePolicy.new(@_config_fetcher, @_config_cache, _get_cache_key(), cache_time_to_live_seconds)
    else
      @_config_fetcher = CacheControlConfigFetcher.new(sdk_key, "m", base_url, proxy_address, proxy_port, proxy_user, proxy_pass, data_governance)
      @_cache_policy = ManualPollingCachePolicy.new(@_config_fetcher, @_config_cache, _get_cache_key())
    end
  end
end

Instance Method Details

#force_refreshObject



129
130
131
# File 'lib/configcat/configcatclient.rb', line 129

def force_refresh()
  @_cache_policy.force_refresh()
end

#get_all_keysObject



59
60
61
62
63
64
65
66
67
68
69
# File 'lib/configcat/configcatclient.rb', line 59

def get_all_keys()
  config = @_cache_policy.get()
  if config === nil
    return []
  end
  feature_flags = config.fetch(FEATURE_FLAGS, nil)
  if feature_flags === nil
    return []
  end
  return feature_flags.keys
end

#get_all_variation_ids(user: nil) ⇒ Object



83
84
85
86
87
88
89
90
91
92
93
# File 'lib/configcat/configcatclient.rb', line 83

def get_all_variation_ids(user: nil)
  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



95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
# File 'lib/configcat/configcatclient.rb', line 95

def get_key_and_value(variation_id)
  config = @_cache_policy.get()
  if config === nil
    ConfigCat.logger.warn("Evaluating get_variation_id('%s') failed. Cache is empty. Returning nil." % variation_id)
    return nil
  end

  feature_flags = config.fetch(FEATURE_FLAGS, nil)
  if feature_flags === nil
    ConfigCat.logger.warn("Evaluating get_key_and_value('%s') failed. Cache is empty. Returning None." % variation_id)
    return nil
  end

  for key, value in feature_flags
    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
end

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



50
51
52
53
54
55
56
57
# File 'lib/configcat/configcatclient.rb', line 50

def get_value(key, default_value, user=nil)
  config = @_cache_policy.get()
  if config === nil
    return default_value
  end
  value, variation_id = RolloutEvaluator.evaluate(key, user, default_value, nil, config)
  return value
end

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



71
72
73
74
75
76
77
78
79
80
81
# File 'lib/configcat/configcatclient.rb', line 71

def get_variation_id(key, default_variation_id, user=nil)
  config = @_cache_policy.get()
  if config === nil
    ConfigCat.logger.warn("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])
    return default_variation_id
  end
  value, variation_id = RolloutEvaluator.evaluate(key, user, nil, default_variation_id, config)
  return variation_id
end

#stopObject



133
134
135
136
# File 'lib/configcat/configcatclient.rb', line 133

def stop()
  @_cache_policy.stop()
  @_config_fetcher.close()
end