77require 'configcat/rolloutevaluator'
88require 'configcat/datagovernance'
99
10+
1011module ConfigCat
1112 KeyValue = Struct . new ( :key , :value )
1213 class ConfigCatClient
14+ @@sdk_keys = [ ]
15+
1316 def initialize ( sdk_key ,
14- poll_interval_seconds :60 ,
15- max_init_wait_time_seconds :5 ,
16- on_configuration_changed_callback :nil ,
17- cache_time_to_live_seconds :60 ,
18- config_cache_class :nil ,
19- base_url :nil ,
20- proxy_address :nil ,
21- proxy_port :nil ,
22- proxy_user :nil ,
23- proxy_pass :nil ,
17+ poll_interval_seconds : 60 ,
18+ max_init_wait_time_seconds : 5 ,
19+ on_configuration_changed_callback : nil ,
20+ cache_time_to_live_seconds : 60 ,
21+ config_cache_class : nil ,
22+ base_url : nil ,
23+ proxy_address : nil ,
24+ proxy_port : nil ,
25+ proxy_user : nil ,
26+ proxy_pass : nil ,
27+ open_timeout : 10 ,
28+ read_timeout : 30 ,
29+ flag_overrides : nil ,
2430 data_governance : DataGovernance ::GLOBAL )
2531 if sdk_key === nil
2632 raise ConfigCatClientException , "SDK Key is required."
2733 end
34+
35+ if @@sdk_keys . include? ( sdk_key )
36+ ConfigCat . logger . warn ( "A ConfigCat Client is already initialized with sdk_key %s. " \
37+ "We strongly recommend you to use the ConfigCat Client as " \
38+ "a Singleton object in your application." % sdk_key )
39+ else
40+ @@sdk_keys . push ( sdk_key )
41+ end
42+
2843 @_sdk_key = sdk_key
44+ @_override_data_source = flag_overrides
2945
3046 if config_cache_class
3147 @_config_cache = config_cache_class . new ( )
3248 else
3349 @_config_cache = InMemoryConfigCache . new ( )
3450 end
3551
36- if poll_interval_seconds > 0
37- @_config_fetcher = CacheControlConfigFetcher . new ( sdk_key , "p" , base_url , proxy_address , proxy_port , proxy_user , proxy_pass , data_governance )
52+ if !@_override_data_source . equal? ( nil ) && @_override_data_source . get_behaviour ( ) == OverrideBehaviour ::LOCAL_ONLY
53+ @_config_fetcher = nil
54+ @_cache_policy = nil
55+ elsif poll_interval_seconds > 0
56+ @_config_fetcher = CacheControlConfigFetcher . new ( sdk_key , "p" , base_url : base_url ,
57+ proxy_address : proxy_address , proxy_port : proxy_port , proxy_user : proxy_user , proxy_pass : proxy_pass ,
58+ open_timeout : open_timeout , read_timeout : read_timeout ,
59+ data_governance : data_governance )
3860 @_cache_policy = AutoPollingCachePolicy . new ( @_config_fetcher , @_config_cache , _get_cache_key ( ) , poll_interval_seconds , max_init_wait_time_seconds , on_configuration_changed_callback )
61+ elsif cache_time_to_live_seconds > 0
62+ @_config_fetcher = CacheControlConfigFetcher . new ( sdk_key , "l" , base_url : base_url ,
63+ proxy_address : proxy_address , proxy_port : proxy_port , proxy_user : proxy_user , proxy_pass : proxy_pass ,
64+ open_timeout : open_timeout , read_timeout : read_timeout ,
65+ data_governance : data_governance )
66+ @_cache_policy = LazyLoadingCachePolicy . new ( @_config_fetcher , @_config_cache , _get_cache_key ( ) , cache_time_to_live_seconds )
3967 else
40- if cache_time_to_live_seconds > 0
41- @_config_fetcher = CacheControlConfigFetcher . new ( sdk_key , "l" , base_url , proxy_address , proxy_port , proxy_user , proxy_pass , data_governance )
42- @_cache_policy = LazyLoadingCachePolicy . new ( @_config_fetcher , @_config_cache , _get_cache_key ( ) , cache_time_to_live_seconds )
43- else
44- @_config_fetcher = CacheControlConfigFetcher . new ( sdk_key , "m" , base_url , proxy_address , proxy_port , proxy_user , proxy_pass , data_governance )
45- @_cache_policy = ManualPollingCachePolicy . new ( @_config_fetcher , @_config_cache , _get_cache_key ( ) )
46- end
68+ @_config_fetcher = CacheControlConfigFetcher . new ( sdk_key , "m" , base_url : base_url ,
69+ proxy_address : proxy_address , proxy_port : proxy_port , proxy_user : proxy_user , proxy_pass : proxy_pass ,
70+ open_timeout : open_timeout , read_timeout : read_timeout ,
71+ data_governance : data_governance )
72+ @_cache_policy = ManualPollingCachePolicy . new ( @_config_fetcher , @_config_cache , _get_cache_key ( ) )
4773 end
4874 end
4975
5076 def get_value ( key , default_value , user = nil )
51- config = @_cache_policy . get ( )
77+ config = _get_settings ( )
5278 if config === nil
79+ ConfigCat . logger . warn ( "Evaluating get_value('%s') failed. Cache is empty. " \
80+ "Returning default_value in your get_value call: [%s]." % [ key , default_value . to_s ] )
5381 return default_value
5482 end
5583 value , variation_id = RolloutEvaluator . evaluate ( key , user , default_value , nil , config )
5684 return value
5785 end
5886
5987 def get_all_keys ( )
60- config = @_cache_policy . get ( )
88+ config = _get_settings ( )
6189 if config === nil
6290 return [ ]
6391 end
@@ -69,7 +97,7 @@ def get_all_keys()
6997 end
7098
7199 def get_variation_id ( key , default_variation_id , user = nil )
72- config = @_cache_policy . get ( )
100+ config = _get_settings ( )
73101 if config === nil
74102 ConfigCat . logger . warn ( "Evaluating get_variation_id('%s') failed. Cache is empty. " \
75103 "Returning default_variation_id in your get_variation_id call: [%s]." %
@@ -93,7 +121,7 @@ def get_all_variation_ids(user: nil)
93121 end
94122
95123 def get_key_and_value ( variation_id )
96- config = @_cache_policy . get ( )
124+ config = _get_settings ( )
97125 if config === nil
98126 ConfigCat . logger . warn ( "Evaluating get_variation_id('%s') failed. Cache is empty. Returning nil." % variation_id )
99127 return nil
@@ -126,17 +154,56 @@ def get_key_and_value(variation_id)
126154 end
127155 end
128156
157+ def get_all_values ( user : nil )
158+ keys = get_all_keys ( )
159+ all_values = { }
160+ for key in keys
161+ value = get_value ( key , nil , user )
162+ if !value . equal? ( nil )
163+ all_values [ key ] = value
164+ end
165+ end
166+ return all_values
167+ end
168+
129169 def force_refresh ( )
130170 @_cache_policy . force_refresh ( )
131171 end
132172
133173 def stop ( )
134- @_cache_policy . stop ( )
135- @_config_fetcher . close ( )
174+ @_cache_policy . stop ( ) if @_cache_policy
175+ @_config_fetcher . close ( ) if @_config_fetcher
176+ @@sdk_keys . delete ( @_sdk_key )
136177 end
137178
138179 private
139180
181+ def _get_settings ( )
182+ if !@_override_data_source . nil?
183+ behaviour = @_override_data_source . get_behaviour ( )
184+ if behaviour == OverrideBehaviour ::LOCAL_ONLY
185+ return @_override_data_source . get_overrides ( )
186+ elsif behaviour == OverrideBehaviour ::REMOTE_OVER_LOCAL
187+ remote_settings = @_cache_policy . get ( )
188+ local_settings = @_override_data_source . get_overrides ( )
189+ result = local_settings . clone ( )
190+ if remote_settings . key? ( FEATURE_FLAGS ) && local_settings . key? ( FEATURE_FLAGS )
191+ result [ FEATURE_FLAGS ] = result [ FEATURE_FLAGS ] . merge ( remote_settings [ FEATURE_FLAGS ] )
192+ end
193+ return result
194+ elsif behaviour == OverrideBehaviour ::LOCAL_OVER_REMOTE
195+ remote_settings = @_cache_policy . get ( )
196+ local_settings = @_override_data_source . get_overrides ( )
197+ result = remote_settings . clone ( )
198+ if remote_settings . key? ( FEATURE_FLAGS ) && local_settings . key? ( FEATURE_FLAGS )
199+ result [ FEATURE_FLAGS ] = result [ FEATURE_FLAGS ] . merge ( local_settings [ FEATURE_FLAGS ] )
200+ end
201+ return result
202+ end
203+ end
204+ return @_cache_policy . get ( )
205+ end
206+
140207 def _get_cache_key ( )
141208 return Digest ::SHA1 . hexdigest ( "ruby_" + CONFIG_FILE_NAME + "_" + @_sdk_key )
142209 end
0 commit comments