Sha256: c1df8fcc92e6ea644d1761d16a659f7b955765d1590ddfb0e1ac6219fed1384c

Contents?: true

Size: 1.58 KB

Versions: 7

Compression:

Stored size: 1.58 KB

Contents

module Shamu
  module Entities

    # 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.
    module OpaqueId
      module_function

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

      # @return [String] an opaque value that uniquely identifies the
      # entity.
      def opaque_id( entity )
        path = if entity.is_a?( String )
                 entity
               else
                 Entity.compose_entity_path( [ entity ] )
               end

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

      # @return [String,Integer] the encoded raw record id.
      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

      # @return [Array<[String, String]>] decodes the id to it's {EntityPath}.
      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

      # @param [String] value candidate value
      # @return [Boolean] true if the given value is an opaque id.
      def opaque_id?( value )
        return unless value
        PATTERN =~ value
      end
    end
  end
end

Version data entries

7 entries across 7 versions & 1 rubygems

Version Path
shamu-0.0.21 lib/shamu/entities/opaque_id.rb
shamu-0.0.20 lib/shamu/entities/opaque_id.rb
shamu-0.0.19 lib/shamu/entities/opaque_id.rb
shamu-0.0.18 lib/shamu/entities/opaque_id.rb
shamu-0.0.17 lib/shamu/entities/opaque_id.rb
shamu-0.0.15 lib/shamu/entities/opaque_id.rb
shamu-0.0.14 lib/shamu/entities/opaque_id.rb