Module: Exid::Coder

Defined in:
lib/exid/coder.rb

Class Method Summary collapse

Class Method Details

.decode(eid) ⇒ Object



40
41
42
43
44
45
46
47
48
49
50
51
52
53
# File 'lib/exid/coder.rb', line 40

def self.decode(eid)
  prefix, base62 = eid.to_s.split("_", 2)

  if prefix.nil? || base62.nil?
    raise DecodeError, "Invalid EID #{eid.inspect}"
  end

  hex = Base62.decode(base62).to_s(16).rjust(32, "0")

  Result.new(
    prefix,
    [hex[0..7], hex[8..11], hex[12..15], hex[16..19], hex[20..31]].join("-")
  )
end

.encode(prefix, uuid) ⇒ Object



33
34
35
36
37
38
# File 'lib/exid/coder.rb', line 33

def self.encode(prefix, uuid)
  [
    prefix.to_s,
    Base62.encode(uuid.delete("-").hex)
  ].join("_")
end