Module: Shamu::Entities::OpaqueId

Defined in:
lib/shamu/entities/opaque_id.rb

Overview

A collection of helper methods for building and reconstructing opaque entity ids. These ids can be used to uniquely reference a resource within the system without knowing the type or service.

See EntityLookupService for lookup up resources by opaque ID.

Constant Summary collapse

PREFIX =
"::".freeze
PATTERN =
%r{\A#{ PREFIX }[a-zA-Z0-9+/]+={0,3}\z}

Class Method Summary collapse

Class Method Details

.opaque_id(entity) ⇒ String

entity.

Returns:

  • (String)

    an opaque value that uniquely identifies the



17
18
19
20
21
22
23
24
25
# File 'lib/shamu/entities/opaque_id.rb', line 17

def opaque_id( entity )
  path = if entity.is_a?( String )
           entity
         else
           Entity.compose_entity_path( [ entity ] )
         end

  "#{ PREFIX }#{ Base64.strict_encode64( path ) }"
end

.opaque_id?(value) ⇒ Boolean

Returns true if the given value is an opaque id.

Parameters:

  • value (String)

    candidate value

Returns:

  • (Boolean)

    true if the given value is an opaque id.



48
49
50
51
# File 'lib/shamu/entities/opaque_id.rb', line 48

def opaque_id?( value )
  return unless value
  PATTERN =~ value
end

.to_entity_path(opaque_id) ⇒ Array<[String, String]>

Returns decodes the id to it's EntityPath.

Returns:

  • (Array<[String, String]>)

    decodes the id to it's EntityPath.



38
39
40
41
42
43
44
# File 'lib/shamu/entities/opaque_id.rb', line 38

def to_entity_path( opaque_id )
  return nil unless opaque_id && opaque_id.start_with?( PREFIX )

  id = opaque_id[ PREFIX.length..-1 ]
  id = Base64.strict_decode64( id )
  id
end

.to_model_id(opaque_id) ⇒ String, Integer

Returns the encoded raw record id.

Returns:

  • (String, Integer)

    the encoded raw record id.



28
29
30
31
32
33
34
35
# File 'lib/shamu/entities/opaque_id.rb', line 28

def to_model_id( opaque_id )
  if path = to_entity_path( opaque_id )
    path = EntityPath.decompose_entity_path( path )
    path.first.last.to_model_id
  else
    opaque_id.to_model_id
  end
end