class Shamu::Features::ToggleCodec

Packs and unpacks sticky toggle settings in a securely verifiable way. The data is not encrypted so it should not be relied on for security. It only guarantees that the packed data was created by calling {ToggleCode#pack}.

Public Class Methods

new( private_key = Shamu::Security.private_key ) click to toggle source

@param [String] private_key the private key used to verify the packed toggles.

# File lib/shamu/features/toggle_codec.rb, line 12
def initialize( private_key = Shamu::Security.private_key )
  @private_key = private_key
end

Public Instance Methods

insecure_pack( toggles ) click to toggle source

Packs a hash of configured features without any authentication guarantees. Useful for working with trusted sources such as ENV variables.

@param [Hash<String,Boolean>] featues hash of name to enabled state. @return [String] the packed string.

# File lib/shamu/features/toggle_codec.rb, line 32
def insecure_pack( toggles )
  toggles.each_with_object("") do |(key, state), packed|
    packed << "," unless packed.blank?
    packed << "!" unless state
    packed << key
  end
end
insecure_unpack( token ) click to toggle source

Unpack a {#insecure_pack insecure packed} token into its original hash of configured toggles. If the token is invalid an empty result is returned.

@param [String] token the packed toggles @return [Hash<String,Boolean>] the configured toggles.

# File lib/shamu/features/toggle_codec.rb, line 56
def insecure_unpack( token )
  return {} unless token

  token.split( "," ).each_with_object( {} ) do |toggle, hash|
    bang = toggle[0] == "!"
    key  = bang ? toggle[1..-1] : toggle

    hash[key] = !bang
  end
end
pack( toggles ) click to toggle source

Packs a hash of configured features into a string that can be sent from a client at a later date to override those features. Use {#unpack} to restore the features hash.

@param [Hash<String,Boolean>] featues hash of name to enabled state. @return [String] the packed string.

# File lib/shamu/features/toggle_codec.rb, line 22
def pack( toggles )
  hash_value( insecure_pack( toggles ) )
end
unpack( token ) click to toggle source

Unpack a {#pack packed} token into its original hash of configured toggles. If the token is invalid or unauthenticated an empty result is returned.

@param [String] token the packed toggles @return [Hash<String,Boolean>] the configured toggles.

# File lib/shamu/features/toggle_codec.rb, line 46
def unpack( token )
  insecure_unpack( verify_hash( token ) )
end