Module: UUID::NCName
- Defined in:
- lib/uuid/ncname/version.rb,
lib/uuid/ncname.rb
Defined Under Namespace
Classes: AmbiguousToken
Constant Summary collapse
- VERSION =
"0.4.1"
Class Method Summary collapse
-
.from_ncname(ncname, radix: nil, format: :str, version: nil, align: nil, validate: false) ⇒ String?
Converts an NCName-encoded UUID back to its canonical representation.
-
.from_ncname_32(ncname, format: :str, version: nil, align: nil) ⇒ String?
Shorthand for conversion from the Base32 version.
-
.from_ncname_58(ncname, format: :str, version: nil, align: nil) ⇒ String?
Shorthand for conversion from the Base58 version.
-
.from_ncname_64(ncname, format: :str, version: nil, align: nil) ⇒ String?
Shorthand for conversion from the Base64 version.
-
.to_ncname(uuid, radix: 64, version: nil, align: true) ⇒ String
Converts a UUID (or object that when converted to a string looks like a UUID) to an NCName.
-
.to_ncname_32(uuid, version: nil, align: true) ⇒ String
Shorthand for conversion to the Base32 version.
-
.to_ncname_58(uuid, version: nil, align: true) ⇒ String
Shorthand for conversion to the Base58 version.
-
.to_ncname_64(uuid, version: nil, align: true) ⇒ String
Shorthand for conversion to the Base64 version.
-
.valid?(token, strict: false) ⇒ false, ...
Test if the given token is a UUID NCName, with a hint to its version.
Class Method Details
.from_ncname(ncname, radix: nil, format: :str, version: nil, align: nil, validate: false) ⇒ String?
Converts an NCName-encoded UUID back to its canonical representation. Will return nil if the input doesn't match the radix (if supplied) or is otherwise malformed.
291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 |
# File 'lib/uuid/ncname.rb', line 291 def self.from_ncname ncname, radix: nil, format: :str, version: nil, align: nil, validate: false raise 'Format must be symbol-able' unless format.respond_to? :to_sym raise "Invalid format #{format}" unless FORMAT[format] raise 'Align must be true, false, or nil' unless [true, false, nil].include? align # XXX remove this when appropriate # version = warn_version version version = assert_version version return unless ncname and ncname.respond_to? :to_s ncname = ncname.to_s.strip.gsub(/\s+/, '') match = MATCH.match(ncname) or return return if align and !/[A-Pa-p]$/.match? ncname # MATCH is lax # determine the radix from the input if radix raise ArgumentError, "Radix must be 32, 58, or 64, not #{radix}" unless [32, 58, 64].any? radix return unless { 32 => 26, 58 => 23, 64 => 22 }[radix] == ncname.length else radix = { 26 => 32, 23 => 58, 22 => 64}[ncname.length] or raise ArgumentError, "Not sure what to do with an identifier of length #{ncname.length}." end # note MATCH separates the variant uuidver, *content = match.captures content = content.join align = !!(/[A-Pa-p]$/.match? content) if align.nil? uuidver = decode_version uuidver content = DECODE[radix].call content, align bin = TRANSFORM[version][1].call uuidver, content # double-check the variant (high-order bits have to be 10) return if validate and bin[8].ord >> 6 != 2 FORMAT[format].call bin end |
.from_ncname_32(ncname, format: :str, version: nil, align: nil) ⇒ String?
Shorthand for conversion from the Base32 version
426 427 428 429 |
# File 'lib/uuid/ncname.rb', line 426 def self.from_ncname_32 ncname, format: :str, version: nil, align: nil from_ncname ncname, radix: 32, format: format, version: version, align: align end |
.from_ncname_58(ncname, format: :str, version: nil, align: nil) ⇒ String?
Shorthand for conversion from the Base58 version
394 395 396 397 |
# File 'lib/uuid/ncname.rb', line 394 def self.from_ncname_58 ncname, format: :str, version: nil, align: nil from_ncname ncname, radix: 58, format: format, version: version, align: align end |
.from_ncname_64(ncname, format: :str, version: nil, align: nil) ⇒ String?
Shorthand for conversion from the Base64 version
362 363 364 365 |
# File 'lib/uuid/ncname.rb', line 362 def self.from_ncname_64 ncname, format: :str, version: nil, align: nil from_ncname ncname, radix: 64, format: format, version: version, align: align end |
.to_ncname(uuid, radix: 64, version: nil, align: true) ⇒ String
Converts a UUID (or object that when converted to a string looks like a UUID) to an NCName. By default it produces the Base64 variant.
230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 |
# File 'lib/uuid/ncname.rb', line 230 def self.to_ncname uuid, radix: 64, version: nil, align: true raise 'Radix must be either 32, 58, or 64' unless [32, 58, 64].include? radix raise 'UUID must be something stringable' if uuid.nil? or not uuid.respond_to? :to_s align = !!align # coerce to a boolean # XXX remove this when appropriate # version = warn_version(version) version = assert_version version uuid = uuid.to_s bin = nil if uuid.length == 16 bin = uuid else uuid = uuid.gsub(/\s+/, '') if (m = /^(?:urn:uuid:)?([0-9A-Fa-f-]{32,})$/.match(uuid)) bin = [m[1].tr('-', '')].pack 'H*' elsif (m = /^([0-9A-Za-z+\/_-]+=*)$/.match(uuid)) match = m[1].tr('-_', '+/') bin = ::Base64.decode64(match) else raise "Not sure what to do with #{uuid}" end end raise 'Binary representation of UUID is shorter than 16 bytes' if bin.length < 16 uuidver, content = TRANSFORM[version].first.call bin[0, 16] encode_version(uuidver, radix) + ENCODE[radix].call(content, align) end |
.to_ncname_32(uuid, version: nil, align: true) ⇒ String
Shorthand for conversion to the Base32 version
409 410 411 |
# File 'lib/uuid/ncname.rb', line 409 def self.to_ncname_32 uuid, version: nil, align: true to_ncname uuid, radix: 32, version: version, align: align end |
.to_ncname_58(uuid, version: nil, align: true) ⇒ String
Shorthand for conversion to the Base58 version
377 378 379 |
# File 'lib/uuid/ncname.rb', line 377 def self.to_ncname_58 uuid, version: nil, align: true to_ncname uuid, radix: 58, version: version, align: align end |
.to_ncname_64(uuid, version: nil, align: true) ⇒ String
Shorthand for conversion to the Base64 version
345 346 347 |
# File 'lib/uuid/ncname.rb', line 345 def self.to_ncname_64 uuid, version: nil, align: true to_ncname uuid, version: version, align: align end |
.valid?(token, strict: false) ⇒ false, ...
Version 1 tokens always end with I
, J
, K
, or L
(with
base32 being case-insensitive), so tokens that end in something
else will always be version 0.
Test if the given token is a UUID NCName, with a hint to its
version. This method can positively identify a token as a UUID
NCName, but there is a small subset of UUIDs which will produce
tokens which are valid in both versions. The method returns
false
if the token is invalid, otherwise it returns 0
or 1
for the guessed version.
448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 |
# File 'lib/uuid/ncname.rb', line 448 def self.valid? token, strict: false token = token.to_s if MATCH.match? token # false is definitely version zero but true is only maybe version 1 version = /^(?:.{21}[I-L]|.{25}[I-Li-l])$/.match(token) ? 1 : 0 # try decoding with validation on uu = from_ncname token, version: version, validate: true # note that version 1 will always return something because the # method of detecting it is a version 1 also happens to be the # method of determining whether or not it is valid. return false unless uu if version == 1 and strict # but we can also check if the input is a valid version 0 u0 = from_ncname token, version: 0, validate: true raise AmbiguousToken.new(token, v0: u0, v1: uu) if u0 end version else false end end |