Class: UUIDUtil

Inherits:
Object
  • Object
show all
Defined in:
lib/wingify/utils/uuid_util.rb

Class Method Summary collapse

Class Method Details

.generate_uuid(name, namespace) ⇒ UUIDTools::UUID

Helper function to generate a UUID v5 based on a name and a namespace.

Parameters:

  • name (String)

    The name from which to generate the UUID.

  • namespace (UUIDTools::UUID)

    The namespace used to generate the UUID.

Returns:

  • (UUIDTools::UUID)

    A UUID string or nil if inputs are invalid.



48
49
50
51
52
53
# File 'lib/wingify/utils/uuid_util.rb', line 48

def self.generate_uuid(name, namespace)
  return nil if name.nil? || namespace.nil?
  # Convert name to string to handle integer inputs
  name_str = name.to_s
  UUIDTools::UUID.sha1_create(namespace, name_str)
end

.get_random_uuid(sdk_key) ⇒ String

Generates a random UUID based on an API key.

Parameters:

  • sdk_key (String)

    The API key used to generate a namespace for the UUID.

Returns:

  • (String)

    A random UUID string.



24
25
26
27
28
# File 'lib/wingify/utils/uuid_util.rb', line 24

def self.get_random_uuid(sdk_key)
  namespace = UUIDTools::UUID.sha1_create(UUIDTools::UUID_DNS_NAMESPACE, sdk_key)
  random_uuid = UUIDTools::UUID.sha1_create(namespace, SecureRandom.uuid)
  random_uuid.to_s
end

.get_uuid(user_id, account_id) ⇒ String

Generates a UUID for a user based on their user_id and account_id.

Parameters:

  • user_id (String)

    The user’s ID.

  • account_id (String)

    The account ID associated with the user.

Returns:

  • (String)

    A UUID string formatted without dashes and in uppercase.



35
36
37
38
39
40
41
# File 'lib/wingify/utils/uuid_util.rb', line 35

def self.get_uuid(user_id, )
  vwo_namespace = UUIDTools::UUID.sha1_create(UUIDTools::UUID_URL_NAMESPACE, Constants::SEED_URL)
  user_id_namespace = generate_uuid(, vwo_namespace)
   = generate_uuid(user_id, user_id_namespace)

  .to_s.delete('-').upcase
end

.get_uuid_from_context(settings, context, api_name) ⇒ String

Generates a UUID for a user based on their user_id and account_id.

Parameters:

  • settings (SettingsModel)

    The settings of the VWO client.

  • context (Hash)

    The context of the user.

  • api_name (String)

    The name of the API called.

Returns:

  • (String)

    A UUID string formatted without dashes and in uppercase.



71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
# File 'lib/wingify/utils/uuid_util.rb', line 71

def self.get_uuid_from_context(settings, context, api_name)
  if settings.get_is_web_connectivity_enabled != false
    # if web connectivity is enabled, check if context[:id] is a valid web UUID
    if context && web_uuid?(context[:id])
      # if context[:id] is a valid web UUID, set it as uuid
      LoggerService.log(LogLevelEnum::DEBUG, "WEB_UUID_FOUND", {apiName: api_name, uuid: context[:id]})
      return context[:id]
    else
      # if context[:useIdForWeb] is true and context[:id] is not a valid web UUID, throw error
      if context && context[:useIdForWeb] == true
        raise StandardError, 'UUID passed in context.id is not a valid UUID'
      end
      return get_uuid(
        context[:id].to_s,
        SettingsService.instance..to_s
      )
    end
  else
    # if web connectivity is disabled, fallback to server-side UUID derivation
    return get_uuid(
      context[:id].to_s,
      SettingsService.instance..to_s
    )
  end
end

.web_uuid?(id) ⇒ Boolean

Checks if the given ID is a valid Web UUID.

Parameters:

  • id (String)

    The ID to check.

Returns:

  • (Boolean)

    True if the ID is a valid Web UUID, false otherwise.



59
60
61
62
63
# File 'lib/wingify/utils/uuid_util.rb', line 59

def self.web_uuid?(id)
  return false unless id.is_a?(String)

  !!(id =~ Constants::WEB_UUID_REGEX)
end