Class: IbmAppconfigurationRubySdk::AppConfiguration

Inherits:
Object
  • Object
show all
Includes:
Singleton
Defined in:
lib/ibm_appconfiguration_ruby_sdk/app_configuration.rb

Overview

AppConfiguration client class implementing singleton pattern

Constant Summary collapse

REGION_US_SOUTH =

Region constants

"us-south"
REGION_EU_GB =
"eu-gb"
REGION_AU_SYD =
"au-syd"
REGION_US_EAST =
"us-east"
REGION_EU_DE =
"eu-de"
REGION_CA_TOR =
"ca-tor"
REGION_JP_TOK =
"jp-tok"
REGION_JP_OSA =
"jp-osa"

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeAppConfiguration

Returns a new instance of AppConfiguration.



62
63
64
65
66
67
68
69
# File 'lib/ibm_appconfiguration_ruby_sdk/app_configuration.rb', line 62

def initialize
  @is_initialized = false
  @is_initialized_config = false
  @use_private_endpoint = false
  @configuration_handler = nil
  @logger = Logger.instance
  @url_builder = UrlBuilder.instance
end

Class Method Details

.current_instanceAppConfiguration

Get the current instance without creating a new one

Returns:

Raises:

  • (RuntimeError)

    If instance doesn’t exist



43
44
45
46
47
# File 'lib/ibm_appconfiguration_ruby_sdk/app_configuration.rb', line 43

def current_instance
  raise Constants::SINGLETON_EXCEPTION unless @singleton__instance__

  instance
end

.override_service_url(url) ⇒ Object

Override the default App Configuration URL This method should be invoked before the SDK initialization NOTE: To be used for development purposes only

Parameters:

  • url (String)

    The base service URL



54
55
56
57
58
59
# File 'lib/ibm_appconfiguration_ruby_sdk/app_configuration.rb', line 54

def override_service_url(url)
  return unless url

  url_builder = UrlBuilder.instance
  url_builder.set_base_service_url(url)
end

Instance Method Details

#connected?Boolean

Check if the SDK is connected to the service

Returns:

  • (Boolean)

    Connection status



253
254
255
256
257
# File 'lib/ibm_appconfiguration_ruby_sdk/app_configuration.rb', line 253

def connected?
  return @configuration_handler.connected? if @is_initialized && @is_initialized_config

  false
end

#get_feature(feature_id) ⇒ Feature?

Returns the Feature object with the details of the feature specified by the feature_id

Parameters:

  • feature_id (String)

    The Feature ID

Returns:

  • (Feature, nil)

    Feature object or nil if invalid



177
178
179
180
181
182
# File 'lib/ibm_appconfiguration_ruby_sdk/app_configuration.rb', line 177

def get_feature(feature_id)
  return @configuration_handler.get_feature(feature_id) if @is_initialized && @is_initialized_config

  @logger.error(Constants::COLLECTION_INIT_ERROR)
  nil
end

#get_featuresHash?

Returns all features associated with the collection_id

Returns:

  • (Hash, nil)

    Hash of all features or nil



187
188
189
190
191
192
# File 'lib/ibm_appconfiguration_ruby_sdk/app_configuration.rb', line 187

def get_features
  return @configuration_handler.get_features if @is_initialized && @is_initialized_config

  @logger.error(Constants::COLLECTION_INIT_ERROR)
  nil
end

#get_propertiesHash?

Returns all properties associated with the collection_id

Returns:

  • (Hash, nil)

    Hash of all properties or nil



208
209
210
211
212
213
# File 'lib/ibm_appconfiguration_ruby_sdk/app_configuration.rb', line 208

def get_properties
  return @configuration_handler.get_properties if @is_initialized && @is_initialized_config

  @logger.error(Constants::COLLECTION_INIT_ERROR)
  nil
end

#get_property(property_id) ⇒ Property?

Returns the Property object with the details of the property specified by the property_id

Parameters:

  • property_id (String)

    The Property ID

Returns:

  • (Property, nil)

    Property object or nil if invalid



198
199
200
201
202
203
# File 'lib/ibm_appconfiguration_ruby_sdk/app_configuration.rb', line 198

def get_property(property_id)
  return @configuration_handler.get_property(property_id) if @is_initialized && @is_initialized_config

  @logger.error(Constants::COLLECTION_INIT_ERROR)
  nil
end

#get_secret(property_id, secrets_manager_service) ⇒ SecretProperty?

Returns the SecretProperty object corresponding to the given property_id

Parameters:

  • property_id (String)

    ID of the secret property from App Configuration

  • secrets_manager_service (Object)

    Secret Manager client object

Returns:



220
221
222
223
224
225
226
227
228
229
# File 'lib/ibm_appconfiguration_ruby_sdk/app_configuration.rb', line 220

def get_secret(property_id, secrets_manager_service)
  if @is_initialized && @is_initialized_config
    return @configuration_handler.get_secret(property_id, secrets_manager_service) if secrets_manager_service

    @logger.error(Constants::INVALID_SECRET_MANAGER_CLIENT_MESSAGE)
    return nil
  end
  @logger.error(Constants::COLLECTION_INIT_ERROR)
  nil
end

#init(region, guid, apikey) ⇒ Object

Initialize the SDK to connect with your App Configuration service instance

Parameters:

  • region (String)

    Region name where the App Configuration service instance is created

  • guid (String)

    GUID of the App Configuration service

  • apikey (String)

    API key of the App Configuration service

Raises:

  • (RuntimeError)

    If any required parameter is missing or invalid



77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
# File 'lib/ibm_appconfiguration_ruby_sdk/app_configuration.rb', line 77

def init(region, guid, apikey)
  # init is a SDK initialization method. It is expected to be called only once.
  # This condition ensures the init inputs are taken only once even if called multiple times.
  return if @is_initialized

  unless region && guid && apikey
    if !region
      report_error(Constants::REGION_ERROR)
    elsif !guid
      report_error(Constants::GUID_ERROR)
    else
      report_error(Constants::APIKEY_ERROR)
    end
  end

  @configuration_handler = ConfigurationHandler.instance
  @configuration_handler.init(region, guid, apikey, @use_private_endpoint)
  @is_initialized = true
end

#register_configuration_update_listener(&block) ⇒ void

This method returns an undefined value.

Register a configuration update listener The listener will be invoked whenever configurations are updated (initial load or live updates). Only one listener can be registered at a time - calling this method multiple times will replace the previous listener.

Examples:

app_config = IbmAppconfigurationRubySdk::AppConfiguration.instance
app_config.register_configuration_update_listener do
  puts "Configurations updated!"
  feature = app_config.get_feature('my-feature')
  # React to configuration changes
end

Parameters:

  • block (Proc)

    Callback block to be invoked on configuration updates



272
273
274
275
276
277
278
# File 'lib/ibm_appconfiguration_ruby_sdk/app_configuration.rb', line 272

def register_configuration_update_listener(&block)
  if @is_initialized && @is_initialized_config
    @configuration_handler.register_configuration_update_listener(&block)
    return
  end
  @logger.error(Constants::COLLECTION_INIT_ERROR)
end

#set_context(collection_id, environment_id, options = {}) ⇒ Object

Set the context of the SDK

Parameters:

  • collection_id (String)

    ID of the collection created in App Configuration service instance

  • environment_id (String)

    ID of the environment created in App Configuration service instance

  • options (Hash) (defaults to: {})

    Optional configuration parameters

Options Hash (options):

  • :persistent_cache_directory (String)

    Directory path for persistent cache

  • :bootstrap_file (String)

    Absolute path of configuration file

  • :live_config_update_enabled (Boolean)

    Enable live configuration updates (default: true)

Raises:

  • (RuntimeError)

    If init was not called or parameters are invalid



106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
# File 'lib/ibm_appconfiguration_ruby_sdk/app_configuration.rb', line 106

def set_context(collection_id, environment_id, options = {})
  # setContext is also a SDK initialization method. It is expected to be called only once.
  # This condition ensures the setContext inputs are taken only once even if called multiple times.
  return if @is_initialized_config

  report_error(Constants::COLLECTION_ID_ERROR) unless @is_initialized

  report_error(Constants::COLLECTION_ID_VALUE_ERROR) unless collection_id

  report_error(Constants::ENVIRONMENT_ID_VALUE_ERROR) unless environment_id

  default_options = {
    persistent_cache_directory: nil,
    bootstrap_file: nil,
    live_config_update_enabled: true
  }

  if options
    report_error(Constants::INVALID_OPTIONS_PARAMETER.to_s) unless options.is_a?(Hash)

    if options.key?(:persistent_cache_directory)
      given_dir_path = options[:persistent_cache_directory]
      if given_dir_path.is_a?(String) && !given_dir_path.empty?
        default_options[:persistent_cache_directory] = given_dir_path
      else
        report_error("#{Constants::PERSISTENT_CACHE_OPTION_ERROR} #{given_dir_path}")
      end
    end

    if options.key?(:bootstrap_file)
      given_file_path = options[:bootstrap_file]
      if given_file_path.is_a?(String) && !given_file_path.empty? && File.extname(given_file_path) == ".json"
        default_options[:bootstrap_file] = given_file_path
      else
        report_error("#{Constants::BOOTSTRAP_FILEPATH_OPTION_ERROR} #{given_file_path}")
      end
    end

    if options.key?(:live_config_update_enabled)
      given_flag_value = options[:live_config_update_enabled]
      if [true, false].include?(given_flag_value)
        default_options[:live_config_update_enabled] = given_flag_value
      else
        report_error("#{Constants::LIVE_CONFIG_UPDATE_OPTION_ERROR} #{given_flag_value}")
      end
    end

    report_error(Constants::CONFIGURATION_FILE_NOT_FOUND_ERROR) if default_options[:live_config_update_enabled] == false && default_options[:bootstrap_file].nil?
  end

  @is_initialized_config = true
  @configuration_handler = ConfigurationHandler.instance
  @configuration_handler.set_context(collection_id, environment_id, default_options)
end

#set_debug(value = false) ⇒ Object

Enable or disable the logger By default, logger is disabled

Parameters:

  • value (Boolean) (defaults to: false)

    Enable (true) or disable (false) debug logging



246
247
248
# File 'lib/ibm_appconfiguration_ruby_sdk/app_configuration.rb', line 246

def set_debug(value = false)
  Logger.set_debug(value)
end

#track(event_key, entity_id) ⇒ Object

Record custom metric events for an entity_id while running an experiment

Parameters:

  • event_key (String)

    SDK event key

  • entity_id (String)

    The entity ID



235
236
237
238
239
240
# File 'lib/ibm_appconfiguration_ruby_sdk/app_configuration.rb', line 235

def track(event_key, entity_id)
  return @configuration_handler.track(event_key, entity_id) if @is_initialized && @is_initialized_config

  @logger.error(Constants::COLLECTION_INIT_ERROR)
  nil
end

#use_private_endpoint(use_private_endpoint_param) ⇒ Object

Set the SDK to connect to App Configuration service using a private endpoint This function must be called before calling the init function

Parameters:

  • use_private_endpoint_param (Boolean)

    Set to true to use private endpoint (default: false)



165
166
167
168
169
170
171
# File 'lib/ibm_appconfiguration_ruby_sdk/app_configuration.rb', line 165

def use_private_endpoint(use_private_endpoint_param)
  if [true, false].include?(use_private_endpoint_param)
    @use_private_endpoint = use_private_endpoint_param
    return
  end
  @logger.error(Constants::INPUT_PARAMETER_NOT_BOOLEAN)
end