module VWO::Utils::UUID

Constants

VWO_NAMESPACE

Public Class Methods

parse(obj) click to toggle source
# File lib/vwo/utils/uuid.rb, line 27
def self.parse(obj)
  str = obj.to_s.sub(/\Aurn:uuid:/, '')
  str.gsub!(/[^0-9A-Fa-f]/, '')
  [str[0..31]].pack 'H*'
end
uuid_v5(uuid_namespace, name) click to toggle source
# File lib/vwo/utils/uuid.rb, line 33
def self.uuid_v5(uuid_namespace, name)
  uuid_namespace = parse(uuid_namespace)
  hash_class = ::Digest::SHA1
  version = 5

  hash = hash_class.new
  hash.update(uuid_namespace)
  hash.update(name)

  ary = hash.digest.unpack('NnnnnN')
  ary[2] = (ary[2] & 0x0FFF) | (version << 12)
  ary[3] = (ary[3] & 0x3FFF) | 0x8000
  # rubocop:disable Lint/FormatStringToken,Style/FormatString
  '%08x-%04x-%04x-%04x-%04x%08x' % ary
  # rubocop:enable Lint/FormatStringToken,Style/FormatString
end

Public Instance Methods

generate(namespace, name) click to toggle source

Generated uuid from namespace and name, uses uuid5

@param :namespace Namespace @param[String) :name Name

@return UUID, nil if any of the arguments is empty

# File lib/vwo/utils/uuid.rb, line 86
def generate(namespace, name)
  VWO::Utils::UUID.uuid_v5(namespace, name) if name && namespace
end
generator_for(user_id, account_id) click to toggle source

Generates desired UUID

@param :user_id User identifier @param :account_id Account identifier

@return Desired UUID

# File lib/vwo/utils/uuid.rb, line 59
def generator_for(user_id, account_id)
  user_id = user_id.to_s
  account_id = account_id.to_s
  user_id_namespace = generate(VWO_NAMESPACE, account_id)
  uuid_for_account_user_id = generate(user_id_namespace, user_id)

  desired_uuid = uuid_for_account_user_id.delete('-').upcase

  VWO::Logger.get_instance.log(
    LogLevelEnum::DEBUG,
    format(
      LogMessageEnum::DebugMessages::UUID_FOR_USER,
      file: FileNameEnum::UuidUtil,
      user_id: user_id,
      account_id: account_id,
      desired_uuid: desired_uuid
    )
  )
  desired_uuid
end