Class: UUIDUtil
- Inherits:
-
Object
- Object
- UUIDUtil
- Defined in:
- lib/wingify/utils/uuid_util.rb
Class Method Summary collapse
-
.generate_uuid(name, namespace) ⇒ UUIDTools::UUID
Helper function to generate a UUID v5 based on a name and a namespace.
-
.get_random_uuid(sdk_key) ⇒ String
Generates a random UUID based on an API key.
-
.get_uuid(user_id, account_id) ⇒ String
Generates a UUID for a user based on their user_id and account_id.
-
.get_uuid_from_context(settings, context, api_name) ⇒ String
Generates a UUID for a user based on their user_id and account_id.
-
.web_uuid?(id) ⇒ Boolean
Checks if the given ID is a valid Web UUID.
Class Method Details
.generate_uuid(name, namespace) ⇒ UUIDTools::UUID
Helper function to generate a UUID v5 based on a name and a namespace.
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.
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.
35 36 37 38 39 40 41 |
# File 'lib/wingify/utils/uuid_util.rb', line 35 def self.get_uuid(user_id, account_id) vwo_namespace = UUIDTools::UUID.sha1_create(UUIDTools::UUID_URL_NAMESPACE, Constants::SEED_URL) user_id_namespace = generate_uuid(account_id, vwo_namespace) uuid_for_user_id_account_id = generate_uuid(user_id, user_id_namespace) uuid_for_user_id_account_id.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.
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.account_id.to_s ) end else # if web connectivity is disabled, fallback to server-side UUID derivation return get_uuid( context[:id].to_s, SettingsService.instance.account_id.to_s ) end end |
.web_uuid?(id) ⇒ Boolean
Checks if the given ID is a valid Web UUID.
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 |