Module: RackSessionManipulation::Utilities
- Included in:
- RackSessionManipulation
- Defined in:
- lib/rack_session_manipulation/utilities.rb
Overview
Various utility methods that will be module level functions for the parent namespace module.
Instance Method Summary collapse
-
#decode(encoded_data) ⇒ Hash
Helper method for decoding the session state into a standard Ruby hash.
-
#encode(obj) ⇒ String
Helper method for encoding modified session state for the middleware.
-
#random_path_prefix ⇒ String
SecureRandom raises a NotImplementedError if no random device is available in that case fallback on Kernel.rand.
Instance Method Details
#decode(encoded_data) ⇒ Hash
Helper method for decoding the session state into a standard Ruby hash. This data is exposed as JSON, and is parsed as such.
11 12 13 |
# File 'lib/rack_session_manipulation/utilities.rb', line 11 def decode(encoded_data) JSON.parse(encoded_data) end |
#encode(obj) ⇒ String
Helper method for encoding modified session state for the middleware. The middleware expects JSON so this is just a thin wrapper for encoding to JSON.
22 23 24 |
# File 'lib/rack_session_manipulation/utilities.rb', line 22 def encode(obj) JSON.generate(obj) end |
#random_path_prefix ⇒ String
SecureRandom raises a NotImplementedError if no random device is available in that case fallback on Kernel.rand. Shamelessly stolen from the Sinatra session secret generation code.
31 32 33 34 35 36 37 38 39 |
# File 'lib/rack_session_manipulation/utilities.rb', line 31 def random_path_prefix num = begin require 'securerandom' SecureRandom.random_number(2**128 - 1) rescue LoadError, NotImplementedError Kernel.rand(2**128 - 1) end format('/%032x', num) end |