Class: Sorcery::TestHelpers::Sinatra::CookieSessionMethods::SessionData

Inherits:
Object
  • Object
show all
Defined in:
lib/sorcery/test_helpers/sinatra.rb

Instance Method Summary collapse

Constructor Details

#initialize(cookies) ⇒ SessionData

Returns a new instance of SessionData.



50
51
52
53
54
55
56
57
58
59
# File 'lib/sorcery/test_helpers/sinatra.rb', line 50

def initialize(cookies)
  @cookies = cookies
  @data = cookies['rack.session']
  if @data
    @data = @data.unpack("m*").first
    @data = Marshal.load(@data)
  else
    @data = {}
  end
end

Instance Method Details

#[](key) ⇒ Object



61
62
63
# File 'lib/sorcery/test_helpers/sinatra.rb', line 61

def [](key)
  @data[key]
end

#[]=(key, value) ⇒ Object



65
66
67
68
69
70
71
# File 'lib/sorcery/test_helpers/sinatra.rb', line 65

def []=(key, value)
  @data[key] = value
  session_data = Marshal.dump(@data)
  session_data = [session_data].pack("m*")
  @cookies.merge("rack.session=#{Rack::Utils.escape(session_data)}", URI.parse("//example.org//"))
  raise "session variable not set" unless @cookies['rack.session'] == session_data
end

#clearObject



73
74
75
# File 'lib/sorcery/test_helpers/sinatra.rb', line 73

def clear
  @data = {}
end