Class: Puffs::Session

Inherits:
Object
  • Object
show all
Defined in:
lib/session.rb

Overview

Session token.

Instance Method Summary collapse

Constructor Details

#initialize(req) ⇒ Session

find the cookie for this app deserialize the cookie into a hash



8
9
10
11
12
13
14
15
16
# File 'lib/session.rb', line 8

def initialize(req)
  cookie = req.cookies['_rails_lite_app']

  if cookie
    @session_data = JSON.parse(cookie)
  else
    @session_data = {}
  end
end

Instance Method Details

#[](key) ⇒ Object



18
19
20
# File 'lib/session.rb', line 18

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

#[]=(key, val) ⇒ Object



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

def []=(key, val)
  @session_data[key] = val
end

#store_session(res) ⇒ Object

serialize the hash into json and save in a cookie add to the responses cookies



28
29
30
31
32
33
34
# File 'lib/session.rb', line 28

def store_session(res)
  res.set_cookie(
    '_rails_lite_app',
    path: '/',
    value: @session_data.to_json
  )
end