Class: UrlBuilder

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

Overview

This module provides methods to construct headers & different URLs used by the SDK.

The UrlBuilder class implements a Singleton pattern to ensure consistent URL configuration across the SDK. It handles:

  • HTTP/HTTPS endpoints for App Configuration service

  • WebSocket (WSS) connections for real-time updates

  • IAM authentication URLs

  • Support for both public and private endpoints

  • Environment-specific URL overrides for dev/staging/testing

Constant Summary collapse

HTTPS_PROTOCOL =

Constants for URL construction

"https://"
WEBSOCKET_PROTOCOL =
"wss://"
BASE_URL =
".apprapp.cloud.ibm.com"
WEBSOCKET_PATH =
"/wsfeature"
SERVICE_PATH =
"/apprapp"
PRIVATE_ENDPOINT_PREFIX =
"private."
IAM_TEST_URL =

IAM URLs

"iam.test.cloud.ibm.com/identity/token"
IAM_PROD_URL =
"iam.cloud.ibm.com/identity/token"

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeUrlBuilder

Initialize the UrlBuilder with default values



48
49
50
51
52
53
54
55
# File 'lib/ibm_appconfiguration_ruby_sdk/core/url_builder.rb', line 48

def initialize
  @region = ""
  @instance_guid = nil
  @apikey = nil
  @override_service_url = nil
  @use_private_endpoint = false
  @websocket_full_url = nil
end

Instance Attribute Details

#apikeyString

Get the API key

Returns:

  • (String)

    the API key value



96
97
98
# File 'lib/ibm_appconfiguration_ruby_sdk/core/url_builder.rb', line 96

def apikey
  @apikey
end

#regionString

Get the region value

Returns:

  • (String)

    the region value



67
68
69
# File 'lib/ibm_appconfiguration_ruby_sdk/core/url_builder.rb', line 67

def region
  @region
end

#use_private_endpoint=(value) ⇒ Boolean (writeonly)

Enable or disable private endpoint usage. When enabled, all URLs will use IBM Cloud private network routing.

Examples:

Enable private endpoint

builder.use_private_endpoint = true
# All URLs will now include 'private.' prefix

Parameters:

  • value (Boolean)

    Set to true to use private endpoints

Returns:

  • (Boolean)

    the private endpoint setting



224
225
226
# File 'lib/ibm_appconfiguration_ruby_sdk/core/url_builder.rb', line 224

def use_private_endpoint=(value)
  @use_private_endpoint = value
end

Instance Method Details

#base_service_urlString

Get the base URL for the App Configuration service instance. Returns the appropriate URL based on environment and endpoint type.

Examples:

Production public endpoint

# Returns: https://us-south.apprapp.cloud.ibm.com
builder.base_service_url

Production private endpoint

# Returns: https://private.us-south.apprapp.cloud.ibm.com
builder.use_private_endpoint = true
builder.base_service_url

Returns:

  • (String)

    The base service URL



129
130
131
132
133
134
135
136
137
138
139
140
141
# File 'lib/ibm_appconfiguration_ruby_sdk/core/url_builder.rb', line 129

def base_service_url
  # For dev & stage environments
  if @override_service_url
    return add_private_prefix_to_url(@override_service_url) if @use_private_endpoint

    return @override_service_url
  end

  # For production
  return "#{HTTPS_PROTOCOL}#{PRIVATE_ENDPOINT_PREFIX}#{@region}#{BASE_URL}" if @use_private_endpoint

  "#{HTTPS_PROTOCOL}#{@region}#{BASE_URL}"
end

#base_service_url=(value) ⇒ String

Set the overridden base service URL. Used for testing, development, or staging environments.

Parameters:

  • value (String)

    The base service URL

Returns:

  • (String)

    the override URL value



104
105
106
# File 'lib/ibm_appconfiguration_ruby_sdk/core/url_builder.rb', line 104

def base_service_url=(value)
  @override_service_url = value
end

#guidString

Get the service instance GUID

Returns:

  • (String)

    the GUID value



82
83
84
# File 'lib/ibm_appconfiguration_ruby_sdk/core/url_builder.rb', line 82

def guid
  @instance_guid
end

#guid=(value) ⇒ String

Set the service instance GUID

Parameters:

  • value (String)

    GUID of the service instance

Returns:

  • (String)

    the GUID value



74
75
76
# File 'lib/ibm_appconfiguration_ruby_sdk/core/url_builder.rb', line 74

def guid=(value)
  @instance_guid = value
end

#iam_urlString

Get the IAM (Identity and Access Management) URL for authentication.

Examples:

Production IAM URL

# Returns: https://iam.cloud.ibm.com
builder.iam_url

Test environment IAM URL

# Returns: https://iam.test.cloud.ibm.com
builder.base_service_url = 'https://test.example.com'
builder.iam_url

Returns:

  • (String)

    The IAM URL



157
158
159
160
161
162
163
164
165
166
167
168
169
# File 'lib/ibm_appconfiguration_ruby_sdk/core/url_builder.rb', line 157

def iam_url
  # For dev & stage environments
  if @override_service_url
    return "#{HTTPS_PROTOCOL}#{PRIVATE_ENDPOINT_PREFIX}#{IAM_TEST_URL}" if @use_private_endpoint

    return "#{HTTPS_PROTOCOL}#{IAM_TEST_URL}"
  end

  # For production
  return "#{HTTPS_PROTOCOL}#{PRIVATE_ENDPOINT_PREFIX}#{IAM_PROD_URL}" if @use_private_endpoint

  "#{HTTPS_PROTOCOL}#{IAM_PROD_URL}"
end

#set_base_service_url(value) ⇒ Object

Alias for base_service_url= to match AppConfiguration usage



110
111
112
# File 'lib/ibm_appconfiguration_ruby_sdk/core/url_builder.rb', line 110

def set_base_service_url(value)
  self.base_service_url = value
end

#set_websocket_url(collection_id, environment_id) ⇒ String

Set the WebSocket URL with collection and environment IDs. Constructs the complete WebSocket URL with query parameters.

Examples:

builder.set_websocket_url('collection-1', 'env-prod')
# Sets: wss://us-south.apprapp.cloud.ibm.com/apprapp/wsfeature?instance_id=...&collection_id=collection-1&environment_id=env-prod

Parameters:

  • collection_id (String)

    The collection ID

  • environment_id (String)

    The environment ID

Returns:

  • (String)

    The constructed WebSocket URL



183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
# File 'lib/ibm_appconfiguration_ruby_sdk/core/url_builder.rb', line 183

def set_websocket_url(collection_id, environment_id)
  ws = WEBSOCKET_PROTOCOL.dup

  if @override_service_url
    # For dev & stage environments
    temp = @override_service_url.gsub(%r{https?://}, "")
    ws += PRIVATE_ENDPOINT_PREFIX if @use_private_endpoint
    ws += temp
  else
    # For production
    ws += PRIVATE_ENDPOINT_PREFIX if @use_private_endpoint
    ws += @region
    ws += BASE_URL
  end

  @websocket_full_url = "#{ws}#{SERVICE_PATH}#{WEBSOCKET_PATH}?" \
                       "instance_id=#{@instance_guid}&" \
                       "collection_id=#{collection_id}&" \
                       "environment_id=#{environment_id}"
end

#use_private_endpoint?Boolean

Check if private endpoint is enabled

Returns:

  • (Boolean)

    true if private endpoint is enabled



230
231
232
# File 'lib/ibm_appconfiguration_ruby_sdk/core/url_builder.rb', line 230

def use_private_endpoint?
  @use_private_endpoint
end

#websocket_urlString?

Get the WebSocket URL. Must call set_websocket_url first to construct the URL.

Returns:

  • (String, nil)

    The WebSocket URL or nil if not set



209
210
211
# File 'lib/ibm_appconfiguration_ruby_sdk/core/url_builder.rb', line 209

def websocket_url
  @websocket_full_url
end