Sha256: 7fbf655cde5820e229069d893bbeb0186f3fc499ef579f8f66b3a3521610a8e3

Contents?: true

Size: 1.52 KB

Versions: 2

Compression:

Stored size: 1.52 KB

Contents

# -*- coding: utf-8 -*-
=begin rdoc
ZID class.
=end

require "securerandom"
require "digest/sha2"

class ZID

  # Generate a new ZID string.
  #
  # Example:
  #
  #     string = ZID.generate
  #     #=> "4bb88af57d4ddc224fecad688442423d"
  #
  # Return: [String] A new ZID string
  #
  def self.generate
    SecureRandom.hex
  end

  # Is a given ZID string valid?
  #
  # Example:
  #
  #     ZID.valid?("4bb88af57d4ddc224fecad688442423d") #=> true
  #     ZID.valid?("hello") #=> false
  #
  # Implemenation note: this method tests the string class,
  # then size, then regex. The test of the string size comes
  # before the regex because the test of the size runs faster,
  # and is an optimization for fast-fail because when the string
  # is not size 32, then the regex doesn't need to be run.
  #
  # Return: [true/false]
  #
  def self.valid?(string)
    !!(string && string.is_a?(String) && string.size == 32 && string =~ /\A[0-9a-f]+\z/)
  end

  # Parse any object to an ZID string.
  #
  # This does these steps:
  #
  #   * Convert the object to a string by calling `#to_s`.
  #   * Convert the string to lower case by calling `#downcase`.
  #   * Delete any non-hex characters.
  #   * Take the first 32 characters.
  #
  # Example:
  #
  #     string = ZID.parse("***FFAD30A1-BE5E-B511-9ED8-976CAB0281B6***")
  #     #=> "ffad30a1be5eb5119ed8976cab0281b6"
  #
  # Return: [ZID] A new ZID
  #
  def self.parse(object)
    zid = object.to_s.downcase.gsub(/[^0-9a-f]/,'')[0...32]
    raise ArgumentError if !ZID.valid?(zid)
    zid
  end

end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
sixarm_ruby_zid-6.0.0 lib/sixarm_ruby_zid/zid.rb
sixarm_ruby_zid-5.0.1 lib/sixarm_ruby_zid/zid.rb