Class: Tilia::Dav::UuidUtil

Inherits:
Object show all
Defined in:
lib/tilia/dav/uuid_util.rb

Overview

UUID Utility

This class has static methods to generate and validate UUID’s. UUIDs are used a decent amount within various *DAV standards, so it made sense to include it.

Class Method Summary collapse

Class Method Details

.uuidObject

Returns a pseudo-random v4 UUID

This function is based on a comment by Andrew Moore on php.net



15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
# File 'lib/tilia/dav/uuid_util.rb', line 15

def self.uuid
  format('%04x%04x-%04x-%04x-%04x-%04x%04x%04x',
         # 32 bits for "time_low"
         rand(0..0xffff), rand(0..0xffff),

         # 16 bits for "time_mid"
         rand(0..0xffff),

         # 16 bits for "time_hi_and_version",
         # four most significant bits holds version number 4
         rand(0..0x0fff) | 0x4000,

         # 16 bits, 8 bits for "clk_seq_hi_res",
         # 8 bits for "clk_seq_low",
         # two most significant bits holds zero and one for variant DCE1.1
         rand(0..0x3fff) | 0x8000,

         # 48 bits for "node"
         rand(0..0xffff), rand(0..0xffff), rand(0..0xffff))
end

.validate_uuid(uuid) ⇒ Object

Checks if a string is a valid UUID.

Parameters:

  • string

    $uuid

Returns:

  • bool



40
41
42
# File 'lib/tilia/dav/uuid_util.rb', line 40

def self.validate_uuid(uuid)
  uuid =~ /^[a-f0-9]{8}-[a-f0-9]{4}-[a-f0-9]{4}-[a-f0-9]{4}-[a-f0-9]{12}$/i
end