Class: TelegramWorkflow::Session

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

Defined Under Namespace

Classes: NullSession

Instance Method Summary collapse

Constructor Details

#initialize(params) ⇒ Session

Returns a new instance of Session.



2
3
4
5
6
7
8
9
10
11
# File 'lib/telegram_workflow/session.rb', line 2

def initialize(params)
  @session_id = params.user_id
  @store = TelegramWorkflow.config.session_store

  @session = if serialized_session = @store.read(@session_id)
    Marshal.load(serialized_session)
  else
    {}
  end
end

Instance Method Details

#clearObject



25
26
27
# File 'lib/telegram_workflow/session.rb', line 25

def clear
  @session.clear
end

#delete(key) ⇒ Object



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

def delete(key)
  @session.delete(key)
end

#dumpObject



29
30
31
# File 'lib/telegram_workflow/session.rb', line 29

def dump
  @session_id && @store.write(@session_id, Marshal.dump(@session))
end

#flashObject

this is a temporary per-action store



41
42
43
44
45
# File 'lib/telegram_workflow/session.rb', line 41

def flash
  @session_id ?
    @session[:flash] ||= {} :
    NullSession.new
end

#read(key) ⇒ Object



13
14
15
# File 'lib/telegram_workflow/session.rb', line 13

def read(key)
  @session[key]
end

#reset_flashObject



47
48
49
# File 'lib/telegram_workflow/session.rb', line 47

def reset_flash
  flash.clear
end

#user_sessionObject

this is a user space to store some session data separately from the gem session



34
35
36
37
38
# File 'lib/telegram_workflow/session.rb', line 34

def user_session
  @session_id ?
    @session[:user_session] ||= {} :
    NullSession.new
end

#write(key, value) ⇒ Object



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

def write(key, value)
  @session[key] = value
end