Class: Shamu::Features::ToggleCodec
- Inherits:
-
Object
- Object
- Shamu::Features::ToggleCodec
- Includes:
- Security::HashedValue
- Defined in:
- lib/shamu/features/toggle_codec.rb
Overview
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.
Instance Method Summary collapse
-
#initialize(private_key = Shamu::Security.private_key) ⇒ ToggleCodec
constructor
A new instance of ToggleCodec.
-
#insecure_pack(toggles) ⇒ String
Packs a hash of configured features without any authentication guarantees.
-
#insecure_unpack(token) ⇒ Hash<String,Boolean>
Unpack a insecure packed token into its original hash of configured toggles.
-
#pack(toggles) ⇒ String
Packs a hash of configured features into a string that can be sent from a client at a later date to override those features.
-
#unpack(token) ⇒ Hash<String,Boolean>
Unpack a packed token into its original hash of configured toggles.
Methods included from Security::HashedValue
Constructor Details
#initialize(private_key = Shamu::Security.private_key) ⇒ ToggleCodec
Returns a new instance of ToggleCodec.
12 13 14 |
# File 'lib/shamu/features/toggle_codec.rb', line 12 def initialize( private_key = Shamu::Security.private_key ) @private_key = private_key end |
Instance Method Details
#insecure_pack(toggles) ⇒ String
Packs a hash of configured features without any authentication guarantees. Useful for working with trusted sources such as ENV variables.
32 33 34 35 36 37 38 |
# 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) ⇒ Hash<String,Boolean>
Unpack a insecure packed token into its original hash of configured toggles. If the token is invalid an empty result is returned.
56 57 58 59 60 61 62 63 64 65 |
# 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) ⇒ String
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.
22 23 24 |
# File 'lib/shamu/features/toggle_codec.rb', line 22 def pack( toggles ) hash_value( insecure_pack( toggles ) ) end |
#unpack(token) ⇒ Hash<String,Boolean>
Unpack a packed token into its original hash of configured toggles. If the token is invalid or unauthenticated an empty result is returned.
46 47 48 |
# File 'lib/shamu/features/toggle_codec.rb', line 46 def unpack( token ) insecure_unpack( verify_hash( token ) ) end |