ibm_appconfiguration_ruby_sdk 0.1.0.pre.rc.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (37) hide show
  1. checksums.yaml +7 -0
  2. data/CHANGELOG.md +5 -0
  3. data/CODE_OF_CONDUCT.md +76 -0
  4. data/CONTRIBUTING.md +9 -0
  5. data/LICENSE +201 -0
  6. data/README.md +474 -0
  7. data/Rakefile +8 -0
  8. data/examples/README.md +60 -0
  9. data/examples/app.rb +104 -0
  10. data/lib/ibm_appconfiguration_ruby_sdk/app_configuration.rb +291 -0
  11. data/lib/ibm_appconfiguration_ruby_sdk/configurations/configuration_handler.rb +828 -0
  12. data/lib/ibm_appconfiguration_ruby_sdk/configurations/internal/constants.rb +89 -0
  13. data/lib/ibm_appconfiguration_ruby_sdk/configurations/internal/file_manager.rb +72 -0
  14. data/lib/ibm_appconfiguration_ruby_sdk/configurations/internal/logger.rb +98 -0
  15. data/lib/ibm_appconfiguration_ruby_sdk/configurations/internal/retry_manager/background_retry_manager.rb +284 -0
  16. data/lib/ibm_appconfiguration_ruby_sdk/configurations/internal/retry_manager/config_fetcher.rb +254 -0
  17. data/lib/ibm_appconfiguration_ruby_sdk/configurations/internal/utils.rb +240 -0
  18. data/lib/ibm_appconfiguration_ruby_sdk/configurations/internal/websocket_client/connection_manager.rb +501 -0
  19. data/lib/ibm_appconfiguration_ruby_sdk/configurations/internal/websocket_client/connectivity.rb +30 -0
  20. data/lib/ibm_appconfiguration_ruby_sdk/configurations/internal/websocket_client/driver_socket.rb +28 -0
  21. data/lib/ibm_appconfiguration_ruby_sdk/configurations/internal/websocket_client/retry_policy.rb +42 -0
  22. data/lib/ibm_appconfiguration_ruby_sdk/configurations/internal/websocket_client/state.rb +24 -0
  23. data/lib/ibm_appconfiguration_ruby_sdk/configurations/internal/websocket_client/watchdog.rb +50 -0
  24. data/lib/ibm_appconfiguration_ruby_sdk/configurations/internal/websocket_client/websocket_client.rb +43 -0
  25. data/lib/ibm_appconfiguration_ruby_sdk/configurations/models/feature.rb +121 -0
  26. data/lib/ibm_appconfiguration_ruby_sdk/configurations/models/property.rb +107 -0
  27. data/lib/ibm_appconfiguration_ruby_sdk/configurations/models/rule.rb +87 -0
  28. data/lib/ibm_appconfiguration_ruby_sdk/configurations/models/secret_property.rb +81 -0
  29. data/lib/ibm_appconfiguration_ruby_sdk/configurations/models/segment.rb +39 -0
  30. data/lib/ibm_appconfiguration_ruby_sdk/configurations/models/segment_rules.rb +57 -0
  31. data/lib/ibm_appconfiguration_ruby_sdk/core/api_manager.rb +269 -0
  32. data/lib/ibm_appconfiguration_ruby_sdk/core/metering.rb +400 -0
  33. data/lib/ibm_appconfiguration_ruby_sdk/core/url_builder.rb +252 -0
  34. data/lib/ibm_appconfiguration_ruby_sdk/version.rb +20 -0
  35. data/lib/ibm_appconfiguration_ruby_sdk.rb +20 -0
  36. data/sig/ibm_appconfiguration_ruby_sdk.rbs +4 -0
  37. metadata +209 -0
data/README.md ADDED
@@ -0,0 +1,474 @@
1
+ # IBM Cloud App Configuration Ruby SDK
2
+
3
+ IBM Cloud App Configuration SDK is used to perform feature flag and property evaluation and track custom metrics for Experimentation based on the configuration on IBM Cloud App Configuration service.
4
+
5
+ ## Table of Contents
6
+
7
+ - [Overview](#overview)
8
+ - [Installation](#installation)
9
+ - [Import the SDK](#import-the-sdk)
10
+ - [Usage](#usage)
11
+ - [Adding URLs to your allowlist](#adding-urls-to-your-allowlist)
12
+ - [License](#license)
13
+
14
+ ## Overview
15
+
16
+ IBM Cloud App Configuration is a centralized feature management and configuration service
17
+ on [IBM Cloud](https://www.cloud.ibm.com) for use with web and mobile applications, microservices, and distributed
18
+ environments.
19
+
20
+ Instrument your applications with App Configuration Ruby SDK, and use the App Configuration dashboard, CLI or API to
21
+ define feature flags or properties, organized into collections and targeted to segments. Toggle feature flag states in
22
+ the cloud to activate or deactivate features in your application or environment, when required. Run experiments and measure the effect of feature flags on end users by tracking custom metrics. You can also manage the properties for distributed applications centrally.
23
+
24
+ ## Installation
25
+
26
+ Installation is done using the `gem install` command or by adding it to your Gemfile.
27
+
28
+ ```bash
29
+ gem install ibm_appconfiguration_ruby_sdk
30
+ ```
31
+
32
+ Or add this line to your application's Gemfile:
33
+
34
+ ```ruby
35
+ gem 'ibm_appconfiguration_ruby_sdk'
36
+ ```
37
+
38
+ And then execute:
39
+
40
+ ```bash
41
+ bundle install
42
+ ```
43
+
44
+ ## Import the SDK
45
+
46
+ To import the module:
47
+
48
+ ```ruby
49
+ require 'ibm_appconfiguration_ruby_sdk'
50
+ ```
51
+
52
+ ## Usage
53
+
54
+ Initialize the SDK to connect with your App Configuration service instance.
55
+
56
+ ```ruby
57
+ require 'ibm_appconfiguration_ruby_sdk'
58
+
59
+ # Get the singleton instance
60
+ app_config_client = IbmAppconfigurationRubySdk::AppConfiguration.instance
61
+
62
+ region = 'us-south'
63
+ guid = '<guid>'
64
+ apikey = '<apikey>'
65
+ collection_id = 'airlines-webapp'
66
+ environment_id = 'dev'
67
+
68
+ # Enable debug logging (optional)
69
+ app_config_client.set_debug(true)
70
+
71
+ # Initialize the SDK
72
+ app_config_client.init(region, guid, apikey)
73
+
74
+ # Set context
75
+ app_config_client.set_context(collection_id, environment_id)
76
+ ```
77
+
78
+ > :warning: It is expected that initialization to be done **only once**.
79
+
80
+ After the SDK is initialized successfully, the feature flags & properties can be retrieved using the `app_config_client` as shown in the below code snippet.
81
+
82
+ <details><summary>Expand to view the example snippet</summary>
83
+
84
+ ```ruby
85
+ # Get feature
86
+ feature = app_config_client.get_feature('online-check-in')
87
+ if feature
88
+ result = feature.get_current_value(entity_id, entity_attributes)
89
+ puts result
90
+ end
91
+
92
+ # Get property
93
+ property = app_config_client.get_property('check-in-charges')
94
+ if property
95
+ result = property.get_current_value(entity_id, entity_attributes)
96
+ puts result
97
+ end
98
+ ```
99
+ </details>
100
+
101
+ where,
102
+ - **region**: Region name where the App Configuration service instance is created.
103
+ See list of supported locations [here](https://cloud.ibm.com/catalog/services/app-configuration). Eg:- `us-south`, `au-syd`, `eu-gb`, `us-east`, `eu-de`, `ca-tor`, `jp-tok`, `jp-osa` etc.
104
+ - **guid**: Instance Id of the App Configuration service. Obtain it from the service credentials section of the App
105
+ Configuration dashboard.
106
+ - **apikey**: ApiKey of the App Configuration service. Obtain it from the service credentials section of the App
107
+ Configuration dashboard.
108
+ - **collection_id**: Id of the collection created in App Configuration service instance under the **Collections** section.
109
+ - **environment_id**: Id of the environment created in App Configuration service instance under the **Environments** section.
110
+
111
+ ### Connect using private network connection (optional)
112
+
113
+ Set the SDK to connect to App Configuration service by using a private endpoint that is accessible only through the IBM Cloud private network.
114
+
115
+ ```ruby
116
+ app_config_client.use_private_endpoint(true)
117
+ ```
118
+
119
+ This must be done before calling the `init` method on the SDK.
120
+
121
+ ### (Optional)
122
+
123
+ In order for your application and SDK to continue its operations even during the unlikely scenario of App Configuration service across your application restarts, you can configure the SDK to work using a persistent cache. The SDK uses the persistent cache to store the App Configuration data that will be available across your application restarts.
124
+
125
+ ```ruby
126
+ app_config_client.set_context(collection_id, environment_id, {
127
+ persistent_cache_directory: '/var/lib/docker/volumes/'
128
+ })
129
+ ```
130
+
131
+ * **persistent_cache_directory**: Absolute path to a directory which has read & write permission for the user. The SDK will create a file - `appconfiguration.json` in the specified directory, and it will be used as the persistent cache to store the App Configuration service information.
132
+
133
+ When persistent cache is enabled, the SDK will keep the last known good configuration at the persistent cache. In the case of App Configuration server being unreachable, the latest configurations at the persistent cache is loaded to the application to continue working.
134
+
135
+ Please ensure that the cache file is not lost or deleted in any case. For example, consider the case when a kubernetes pod is restarted and the cache file (appconfiguration.json) was stored in ephemeral volume of the pod. As pod gets restarted, kubernetes destroys the ephemeral volume in the pod, as a result the cache file gets deleted. So, make sure that the cache file created by the SDK is always stored in persistent volume by providing the correct absolute path of the persistent directory.
136
+
137
+ ### (Optional)
138
+
139
+ The SDK is also designed to serve configurations, perform feature flag & property evaluations without being connected to App Configuration service.
140
+
141
+ ```ruby
142
+ app_config_client.set_context(collection_id, environment_id, {
143
+ bootstrap_file: 'saflights/flights.json',
144
+ live_config_update_enabled: false
145
+ })
146
+ ```
147
+
148
+ This usecase will throw error if given `bootstrap_file` is not found or if unable to parse the json coming from the bootstrap file.
149
+
150
+ * **bootstrap_file**: Absolute path of the JSON file, which contains configuration details. Make sure to provide a proper JSON file. You can generate this file using `ibmcloud ac export` command of the IBM Cloud App Configuration CLI.
151
+ * **live_config_update_enabled**: Live configuration update from the server. Set this value to `false` if the new configuration values shouldn't be fetched from the server.
152
+
153
+ ## Get single feature
154
+
155
+ ```ruby
156
+ feature = app_config_client.get_feature('online-check-in') # feature can be nil in case of an invalid feature id
157
+
158
+ if feature
159
+ puts "Feature Name: #{feature.get_feature_name}"
160
+ puts "Feature Id: #{feature.get_feature_id}"
161
+ puts "Feature Type: #{feature.get_feature_data_type}"
162
+ if feature.is_enabled?
163
+ # feature flag is enabled
164
+ else
165
+ # feature flag is disabled
166
+ end
167
+ end
168
+ ```
169
+
170
+ ## Get all features
171
+
172
+ ```ruby
173
+ features = app_config_client.get_features
174
+ feature = features['online-check-in']
175
+
176
+ if feature
177
+ puts "Feature Name: #{feature.get_feature_name}"
178
+ puts "Feature Id: #{feature.get_feature_id}"
179
+ puts "Feature Type: #{feature.get_feature_data_type}"
180
+ puts "Is feature enabled? #{feature.is_enabled?}"
181
+ end
182
+ ```
183
+
184
+ ## Evaluate a feature
185
+
186
+ Use the `feature.get_current_value(entity_id, entity_attributes)` method to evaluate the value of the feature flag. This method returns a Hash containing evaluated value, feature flag enabled status & evaluation details.
187
+
188
+ ```ruby
189
+ entity_id = 'john_doe'
190
+ entity_attributes = {
191
+ city: 'Bangalore',
192
+ country: 'India'
193
+ }
194
+
195
+ result = feature.get_current_value(entity_id, entity_attributes)
196
+ puts result[:value] # Evaluated value of the feature flag. The type of evaluated value will match the type of feature flag (Boolean, String, Numeric).
197
+ puts result[:is_enabled] # enabled status.
198
+ puts result[:details] # a Hash containing detailed information of the evaluation. See below
199
+
200
+ # the `result[:details]` will have the following
201
+ puts result[:details][:value_type] # a string value. Example: DISABLED_VALUE
202
+ puts result[:details][:reason] # a string value. Example: Disabled value of the feature flag since the feature flag is disabled.
203
+ puts result[:details][:segment_name] # (only if applicable, else it is nil) a string value containing the segment name for which the feature flag was evaluated.
204
+ puts result[:details][:rollout_percentage_applied] # (only if applicable, else it is nil) a boolean value. True if the entity_id was part of the rollout percentage evaluation, false otherwise.
205
+ puts result[:details][:error_type] # (only if applicable, else it is nil) contains the error message if any error occurred during the evaluation.
206
+ ```
207
+
208
+ - **entity_id**: Id of the Entity. This will be a string identifier related to the Entity against which the feature is evaluated. For example, an entity might be an instance of an app that runs on a mobile device, a microservice that runs on the cloud, or a component of infrastructure that runs that microservice. For any entity to interact with App Configuration, it must provide a unique entity ID.
209
+ - **entity_attributes**: A Hash consisting of the attribute name and their values that defines the specified entity. This is an optional parameter if the feature flag is not configured with any targeting definition. If the targeting is configured, then entity_attributes should be provided for the rule evaluation. An attribute is a parameter that is used to define a segment. The SDK uses the attribute values to determine if the specified entity satisfies the targeting rules, and returns the appropriate feature flag value.
210
+
211
+ ## Send custom metrics
212
+
213
+ Record custom metrics for experiments using the track method. Calling track will queue the metric event, which will be sent in batches to the App Configuration servers.
214
+
215
+ ```ruby
216
+ app_config_client.track(event_key, entity_id)
217
+ ```
218
+
219
+ where
220
+ - **event_key**: The event key for the metric associated with the running experiment. The event key in your metric and the event key in your code must match exactly.
221
+
222
+ ## Get single property
223
+
224
+ ```ruby
225
+ property = app_config_client.get_property('check-in-charges') # property can be nil in case of an invalid property id
226
+
227
+ if property
228
+ puts "Property Name: #{property.get_property_name}"
229
+ puts "Property Id: #{property.get_property_id}"
230
+ puts "Property Type: #{property.get_property_data_type}"
231
+ end
232
+ ```
233
+
234
+ ## Get all properties
235
+
236
+ ```ruby
237
+ properties = app_config_client.get_properties
238
+ property = properties['check-in-charges']
239
+
240
+ if property
241
+ puts "Property Name: #{property.get_property_name}"
242
+ puts "Property Id: #{property.get_property_id}"
243
+ puts "Property Type: #{property.get_property_data_type}"
244
+ end
245
+ ```
246
+
247
+ ## Evaluate a property
248
+
249
+ Use the `property.get_current_value(entity_id, entity_attributes)` method to evaluate the value of the property. This method returns a Hash containing evaluated value & evaluation details.
250
+
251
+ ```ruby
252
+ entity_id = 'john_doe'
253
+ entity_attributes = {
254
+ city: 'Bangalore',
255
+ country: 'India'
256
+ }
257
+
258
+ result = property.get_current_value(entity_id, entity_attributes)
259
+ puts result[:value] # Evaluated value of the property. The type of evaluated value will match the type of property (Boolean, String, Numeric).
260
+ puts result[:details] # a Hash containing detailed information of the evaluation. See below
261
+
262
+ # the `result[:details]` will have the following
263
+ puts result[:details][:value_type] # a string value. Example: DEFAULT_VALUE
264
+ puts result[:details][:reason] # a string value. Example: Default value of the property.
265
+ puts result[:details][:segment_name] # (only if applicable, else it is nil) a string value containing the segment name for which the property was evaluated.
266
+ puts result[:details][:error_type] # (only if applicable, else it is nil) contains the error message if any error occurred during the evaluation.
267
+ ```
268
+
269
+ - **entity_id**: Id of the Entity. This will be a string identifier related to the Entity against which the property is evaluated. For example, an entity might be an instance of an app that runs on a mobile device, a microservice that runs on the cloud, or a component of infrastructure that runs that microservice. For any entity to interact with App Configuration, it must provide a unique entity ID.
270
+ - **entity_attributes**: A Hash consisting of the attribute name and their values that defines the specified entity. This is an optional parameter if the property is not configured with any targeting definition. If the targeting is configured, then entity_attributes should be provided for the rule evaluation. An attribute is a parameter that is used to define a segment. The SDK uses the attribute values to determine if the specified entity satisfies the targeting rules, and returns the appropriate property value.
271
+
272
+ ## Get secret property
273
+
274
+ Explicit method for getting the secret references stored in App Configuration.
275
+
276
+ ```ruby
277
+ secret_property_object = app_config_client.get_secret(property_id, secrets_manager_service)
278
+ ```
279
+
280
+ - **property_id**: property_id is the unique string identifier, using this we will be able to fetch the property which will provide the necessary metadata to fetch the secret.
281
+ - **secrets_manager_service**: an initialized secrets manager client, which will be used for getting the secret data during the secret property evaluation. Create a secret manager client by referring the doc: https://cloud.ibm.com/apidocs/secrets-manager/secrets-manager-v2?code=ruby#authentication
282
+
283
+ ## Evaluate a secret property
284
+
285
+ Use the `secret_property_object.get_current_value(entity_id, entity_attributes)` method to evaluate the value of the secret property.
286
+
287
+ Note that the output of this method call is different from `get_current_value` invoked using feature & property objects. This method returns the actual secret value of the evaluated secret reference. The response contains the secret data from the Secrets Manager.
288
+
289
+ ```ruby
290
+ entity_id = 'john_doe'
291
+ entity_attributes = {
292
+ city: 'Bangalore',
293
+ country: 'India'
294
+ }
295
+
296
+ begin
297
+ response = secret_property_object.get_current_value(entity_id, entity_attributes)
298
+ # See below to know how to access the secret data from the response
299
+ rescue StandardError => e
300
+ # handle the error
301
+ end
302
+ ```
303
+
304
+ ## How to access the secret data from a successful response
305
+
306
+ <details><summary>Full example:</summary>
307
+
308
+ ```ruby
309
+ require 'ibm_appconfiguration_ruby_sdk'
310
+ require 'ibm_secrets_manager_sdk'
311
+
312
+ app_config_client = IbmAppconfigurationRubySdk::AppConfiguration.instance
313
+
314
+ begin
315
+ app_config_client.init(region, guid, apikey)
316
+ app_config_client.set_context(collection_id, environment_id)
317
+ rescue StandardError => e
318
+ puts "Failed to initialize app configuration sdk: #{e}"
319
+ end
320
+
321
+ # Initialize Secrets Manager client
322
+ authenticator = IbmCloudSdkCore::Authenticators::IamAuthenticator.new(
323
+ apikey: '<SECRETS_MANAGER_APIKEY>'
324
+ )
325
+
326
+ secrets_manager_service = IbmCloudSecretsManagerApiV2::SecretsManagerV2.new(
327
+ authenticator: authenticator
328
+ )
329
+ secrets_manager_service.service_url = '<SECRETS_MANAGER_INSTANCE_URL>'
330
+
331
+ begin
332
+ secret_property_object = app_config_client.get_secret(property_id, secrets_manager_service)
333
+ response = secret_property_object.get_current_value(entity_id, entity_attributes)
334
+
335
+ # For Arbitrary secret type
336
+ puts response.result['payload']
337
+
338
+ # For username-password secret type
339
+ puts response.result['username']
340
+ puts response.result['password']
341
+
342
+ # For key-value secret type
343
+ puts response.result['data']['key1']
344
+ puts response.result['data']['key2']
345
+ rescue StandardError => e
346
+ # handle the error
347
+ puts "Error: #{e}"
348
+ end
349
+ ```
350
+ </details>
351
+
352
+ ## Fetching the app_config_client across other modules
353
+
354
+ Once the SDK is initialized, the app_config_client can be obtained across other modules as shown below:
355
+
356
+ ```ruby
357
+ # **other modules**
358
+
359
+ require 'ibm_appconfiguration_ruby_sdk'
360
+
361
+ app_config_client = IbmAppconfigurationRubySdk::AppConfiguration.instance
362
+
363
+ feature = app_config_client.get_feature('online-check-in')
364
+ enabled = feature.is_enabled?
365
+ result = feature.get_current_value(entity_id, entity_attributes)
366
+ ```
367
+
368
+ ## Supported Data types
369
+
370
+ App Configuration service allows to configure the feature flag and properties in the following data types: Boolean,
371
+ Numeric, SecretRef, String. The String data type can be of the format of a text string, JSON or YAML. The SDK processes each
372
+ format accordingly as shown in the below table.
373
+
374
+ <details><summary>View Table</summary>
375
+
376
+ | **Feature or Property value** | **DataType** | **DataFormat** | **Type of data returned <br> by `get_current_value[:value]`** | **Example output** |
377
+ | ------------------------------------------------------------------------------------------------------ | ------------ | -------------- | ----------------------------------------------------- | -------------------------------------------------------------------- |
378
+ | `true` | BOOLEAN | not applicable | `Boolean` | `true` |
379
+ | `25` | NUMERIC | not applicable | `Numeric` | `25` |
380
+ | "a string text" | STRING | TEXT | `String` | `"a string text"` |
381
+ | <pre>{<br> "firefox": {<br> "name": "Firefox",<br> "pref_url": "about:config"<br> }<br>}</pre> | STRING | JSON | `Hash` | `{"firefox"=>{"name"=>"Firefox","pref_url"=>"about:config"}}` |
382
+ | <pre>men:<br> - John Smith<br> - Bill Jones<br>women:<br> - Mary Smith<br> - Susan Williams</pre> | STRING | YAML | `String` | `"men:\n - John Smith\n - Bill Jones\nwomen:\n - Mary Smith\n - Susan Williams"` |
383
+
384
+ For property of type secret reference, refer to readme section [evaluate-a-secret-property](#evaluate-a-secret-property)
385
+ </details>
386
+
387
+ <details><summary>Feature flag</summary>
388
+
389
+ ```ruby
390
+ feature = app_config_client.get_feature('json-feature')
391
+ feature.get_feature_data_type # STRING
392
+ feature.get_feature_data_format # JSON
393
+
394
+ # Example (traversing the returned Hash)
395
+ result = feature.get_current_value(entity_id, entity_attributes)
396
+ puts result[:value]['key'] # prints the value of the key
397
+
398
+ feature = app_config_client.get_feature('yaml-feature')
399
+ feature.get_feature_data_type # STRING
400
+ feature.get_feature_data_format # YAML
401
+ feature.get_current_value(entity_id, entity_attributes)
402
+ ```
403
+ </details>
404
+
405
+ <details><summary>Property</summary>
406
+
407
+ ```ruby
408
+ property = app_config_client.get_property('json-property')
409
+ property.get_property_data_type # STRING
410
+ property.get_property_data_format # JSON
411
+
412
+ # Example (traversing the returned Hash)
413
+ result = property.get_current_value(entity_id, entity_attributes)
414
+ puts result[:value]['key'] # prints the value of the key
415
+
416
+ property = app_config_client.get_property('yaml-property')
417
+ property.get_property_data_type # STRING
418
+ property.get_property_data_format # YAML
419
+ property.get_current_value(entity_id, entity_attributes)
420
+ ```
421
+ </details>
422
+
423
+ ## Set listener for feature and property data changes
424
+
425
+ The SDK provides a callback mechanism to notify you in real-time when feature flag's or property's configuration changes. You can register a configuration update listener using the same app_config_client.
426
+
427
+ ```ruby
428
+ app_config_client.register_configuration_update_listener do
429
+ # **add your code**
430
+ # To find the effect of any configuration changes, you can call the feature or property related methods
431
+
432
+ # feature = app_config_client.get_feature('online-check-in')
433
+ # new_result = feature.get_current_value(entity_id, entity_attributes)
434
+ end
435
+ ```
436
+
437
+ ## Enable debugger (optional)
438
+
439
+ Use this method to enable/disable the logging in SDK.
440
+
441
+ ```ruby
442
+ app_config_client.set_debug(true)
443
+ ```
444
+
445
+ ## Examples
446
+
447
+ Try [this](./examples) sample application in the examples folder to learn more about feature and property evaluation.
448
+
449
+ ## Adding URLs to your allowlist
450
+
451
+ This SDK requires connectivity to the internet (if bootstrap based initialization is not done). The endpoints listed below should be reachable from the host/infrastructure where this SDK will run.
452
+
453
+ ```
454
+ https://cloud.ibm.com:443
455
+ https://iam.cloud.ibm.com:443
456
+ https://{region}.apprapp.cloud.ibm.com:443
457
+ wss://{region}.apprapp.cloud.ibm.com:443
458
+ ```
459
+
460
+ If opted for private endpoint by setting `app_config_client.use_private_endpoint(true)` then the allowlist will be
461
+
462
+ ```
463
+ https://cloud.ibm.com:443
464
+ https://private.iam.cloud.ibm.com:443
465
+ https://private.{region}.apprapp.cloud.ibm.com:443
466
+ wss://private.{region}.apprapp.cloud.ibm.com:443
467
+ ```
468
+
469
+ where `region` is the region where your App Configuration service instance is provisioned such as `us-south`, `us-east`, `eu-gb`, `au-syd`, `eu-de`, `ca-tor`, `jp-tok`, `jp-osa` etc.
470
+
471
+ ## License
472
+
473
+ This project is released under the Apache 2.0 license. The license's full text can be found
474
+ in [LICENSE](https://github.com/IBM/appconfiguration-ruby-sdk/blob/master/LICENSE)
data/Rakefile ADDED
@@ -0,0 +1,8 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "bundler/gem_tasks"
4
+ require "rubocop/rake_task"
5
+
6
+ RuboCop::RakeTask.new
7
+
8
+ task default: :rubocop
@@ -0,0 +1,60 @@
1
+ # Examples
2
+
3
+ This folder contains sample applications demonstrating how to use the IBM Cloud App Configuration Ruby SDK.
4
+
5
+ ## Running app.rb
6
+
7
+ The `app.rb` file demonstrates how to use the SDK to evaluate feature flags in a continuous loop.
8
+
9
+ ### Prerequisites
10
+
11
+ 1. Install the SDK dependencies from the root directory:
12
+ ```bash
13
+ bundle install
14
+ ```
15
+
16
+ 2. Set up your App Configuration service credentials. You'll need:
17
+ - Region (e.g., `us-south`, `eu-gb`)
18
+ - GUID (Instance ID from service credentials)
19
+ - API Key (from service credentials)
20
+ - Collection ID (from Collections section)
21
+ - Environment ID (from Environments section)
22
+
23
+ ### Steps to run
24
+
25
+ 1. Open `app.rb` and uncomment the configuration constants at the top of the file (lines 8-12), then add your credentials:
26
+ ```ruby
27
+ REGION = 'us-south' # Your region
28
+ GUID = 'your-guid' # Your instance GUID
29
+ APIKEY = 'your-apikey' # Your API key
30
+ COLLECTION_ID = 'your-collection-id' # Your collection ID
31
+ ENVIRONMENT_ID = 'your-environment-id' # Your environment ID
32
+ ```
33
+
34
+ 2. Update the feature ID in the example (line 64) to match a feature flag in your App Configuration instance:
35
+ ```ruby
36
+ feature = client.get_feature('your-feature-id')
37
+ ```
38
+
39
+ 3. Run the example:
40
+ ```bash
41
+ ruby examples/app.rb
42
+ ```
43
+
44
+ The application will continuously evaluate the feature flag and display statistics showing enabled and disabled evaluations. Press `Ctrl+C` to stop the application.
45
+
46
+ ### Optional configurations
47
+
48
+ The example also demonstrates:
49
+ - **Bootstrap file mode**: Uncomment lines 32-35 and comment out lines 24-30 to run offline using a bootstrap configuration file
50
+ - **Entity attributes**: Modify the `entity_attributes` hash (lines 59-61) to test segment targeting
51
+ - **Failure simulation**: Uncomment the failure simulation blocks (lines 79-89) to test error handling
52
+
53
+ ### What the example does
54
+
55
+ The application:
56
+ 1. Initializes the App Configuration SDK with your credentials
57
+ 2. Continuously generates random user IDs
58
+ 3. Evaluates a feature flag for each user
59
+ 4. Tracks and displays statistics of enabled vs disabled evaluations
60
+ 5. Demonstrates real-time feature flag evaluation with entity attributes
data/examples/app.rb ADDED
@@ -0,0 +1,104 @@
1
+ #!/usr/bin/env ruby
2
+ # frozen_string_literal: true
3
+
4
+ require "securerandom"
5
+ require_relative "../lib/ibm_appconfiguration_ruby_sdk/app_configuration"
6
+
7
+ # Configuration
8
+ REGION = ""
9
+ GUID = ""
10
+ APIKEY = ""
11
+ COLLECTION_ID = ""
12
+ ENVIRONMENT_ID = ""
13
+
14
+ def initialize_app_config
15
+ # Get AppConfiguration singleton instance
16
+ client = IbmAppconfigurationRubySdk::AppConfiguration.instance
17
+
18
+ # Initialize the SDK
19
+ client.init(REGION, GUID, APIKEY)
20
+
21
+ # Set context with options (only pass options that have values)
22
+ client.set_context(
23
+ COLLECTION_ID,
24
+ ENVIRONMENT_ID,
25
+ {
26
+ live_config_update_enabled: true
27
+ }
28
+ )
29
+
30
+ # Wait for initial configuration fetch
31
+ sleep 3
32
+
33
+ client
34
+ end
35
+
36
+ def main
37
+ puts "Initializing App Configuration SDK..."
38
+ client = initialize_app_config
39
+ puts "āœ… SDK initialized successfully"
40
+ puts ""
41
+
42
+ disabled_evals = 0
43
+ enabled_evals = 0
44
+
45
+ loop do
46
+ # Generate random user ID (10 character hex string)
47
+ user_id = SecureRandom.hex(5).upcase
48
+
49
+ entity_id = user_id
50
+ entity_attributes = {
51
+ email: "#{user_id}@ibm.com" # Must match segment attribute_name: "ibmemail"
52
+ }
53
+
54
+ # Get feature using the AppConfiguration client
55
+ feature = client.get_feature("demoflg")
56
+
57
+ if feature.nil?
58
+ puts "\nāŒ Feature 'demoflg' not found"
59
+ sleep 1
60
+ next
61
+ end
62
+
63
+ # Get current value
64
+ begin
65
+ feature_value = feature.get_current_value(entity_id, entity_attributes)
66
+
67
+ if feature_value && feature_value[:value]
68
+ enabled_evals += 1
69
+ # Uncomment to simulate failures
70
+ # if rand >= 0.4
71
+ # enabled_fails += 1
72
+ # raise StandardError, 'We failed!'
73
+ # end
74
+ else
75
+ disabled_evals += 1
76
+ # Uncomment to simulate failures
77
+ # if rand >= 0.99
78
+ # disabled_fails += 1
79
+ # raise StandardError, 'We failed!'
80
+ # end
81
+ end
82
+ rescue StandardError
83
+ # Handle exceptions
84
+ # puts "Exception: #{e.message}"
85
+ end
86
+
87
+ # Print stats (with carriage return to overwrite line)
88
+ # print "enabled_evals #{enabled_evals}, enabled_fails #{enabled_fails}, disabled_evals #{disabled_evals}, disabled_fails #{disabled_fails}\r"
89
+ print "enabled_evals #{enabled_evals}, disabled_evals #{disabled_evals}\r"
90
+ $stdout.flush
91
+
92
+ # Small delay between evaluations (10ms)
93
+ sleep 0.01
94
+ end
95
+ end
96
+
97
+ # Handle Ctrl+C gracefully
98
+ trap("INT") do
99
+ puts "\n\nšŸ›‘ Shutting down..."
100
+ exit(0)
101
+ end
102
+
103
+ # Run main
104
+ main