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

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.

Parameters:

  • encoded_data (String)

    JSON encoded session data

Returns:

  • (Hash)

    Hash version of the session data



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.

Parameters:

  • obj (Hash)

    An object that can be serialized using JSON, generally this is a hash.

Returns:

  • (String)

    The encoded data.



22
23
24
# File 'lib/rack_session_manipulation/utilities.rb', line 22

def encode(obj)
  JSON.generate(obj)
end

#random_path_prefixString

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.

Returns:

  • (String)


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