Class: Manga::Tools::SessionStore

Inherits:
Object
  • Object
show all
Defined in:
lib/manga/tools/session_store.rb

Overview

A class on session retention.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeSessionStore

Returns a new instance of SessionStore.



12
13
14
15
# File 'lib/manga/tools/session_store.rb', line 12

def initialize
  create_session_dir_if_necessary
  @session_file_name = 'session.txt'
end

Instance Attribute Details

#session_file_name=(value) ⇒ String (writeonly)

Returns the session file name string.

Returns:

  • (String)

    the session file name string



10
11
12
# File 'lib/manga/tools/session_store.rb', line 10

def session_file_name=(value)
  @session_file_name = value
end

Instance Method Details

#exists?Boolean

Returns true if the session exists, otherwise returns false.

Returns:

  • (Boolean)

    true if the session exists, otherwise returns false.



34
35
36
# File 'lib/manga/tools/session_store.rb', line 34

def exists?
  File.exist?(session_file_path)
end

#loadString?

Returns the session ID if the session is stored, otherwise it returns nil

Returns:

  • (String, nil)

    Returns the session ID if the session is stored, otherwise it returns nil



27
28
29
30
31
# File 'lib/manga/tools/session_store.rb', line 27

def load
  return nil unless exists?

  File.read(session_file_path)
end

#session_file_pathString

Returns the session file path.

Returns:

  • (String)

    the session file path



39
40
41
# File 'lib/manga/tools/session_store.rb', line 39

def session_file_path
  "#{session_dir}/#{@session_file_name}"
end

#store(session_id) ⇒ Object

Store a session.



18
19
20
21
22
23
24
# File 'lib/manga/tools/session_store.rb', line 18

def store(session_id)
  return if exists?

  File.open(session_file_path, 'w') do |f|
    f.write(session_id)
  end
end