Class: RubyApp::Session

Inherits:
Object
  • Object
show all
Extended by:
Mixins::ConfigurationMixin, Mixins::DelegateMixin, Mixins::TranslateMixin
Defined in:
lib/ruby_app/session.rb

Direct Known Subclasses

_APPLICATION_UPCODE_::Session

Defined Under Namespace

Classes: Identity

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Mixins::ConfigurationMixin

configuration

Methods included from Mixins::DelegateMixin

exists?, method_missing

Methods included from Mixins::TranslateMixin

localize, translate

Constructor Details

#initialize(document = nil) ⇒ Session

Returns a new instance of Session.



35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
# File 'lib/ruby_app/session.rb', line 35

def initialize(document = nil)
  @session_id = RubyApp::Session.generate_session_id(self)
  @expires = Time.now + RubyApp::Session.configuration.expires

  @documents = []
  @data = {}

  @identity = nil

  @steps = []
  @steps_index = 0

  require 'ruby_app/elements/mobile/default/default_document'
  @documents.push(document || RubyApp::Elements::Mobile::Default::DefaultDocument.new)

end

Instance Attribute Details

#dataObject (readonly)

Returns the value of attribute data.



31
32
33
# File 'lib/ruby_app/session.rb', line 31

def data
  @data
end

#documentsObject (readonly)

Returns the value of attribute documents.



30
31
32
# File 'lib/ruby_app/session.rb', line 30

def documents
  @documents
end

#expiresObject (readonly)

Returns the value of attribute expires.



28
29
30
# File 'lib/ruby_app/session.rb', line 28

def expires
  @expires
end

#identityObject

Returns the value of attribute identity.



33
34
35
# File 'lib/ruby_app/session.rb', line 33

def identity
  @identity
end

#session_idObject (readonly)

Returns the value of attribute session_id.



27
28
29
# File 'lib/ruby_app/session.rb', line 27

def session_id
  @session_id
end

Class Method Details

.generate_session_id(session) ⇒ Object



144
145
146
147
148
149
150
151
152
153
# File 'lib/ruby_app/session.rb', line 144

def self.generate_session_id(session)
  session_id = "id_#{SecureRandom.hex(RubyApp::Session.configuration._length)}"
  RubyApp::Session.lock_sessions do
    while RubyApp::Session.get_session(session_id)
      session_id = "id_#{SecureRandom.hex(RubyApp::Session.configuration._length)}"
    end
    RubyApp::Session.sessions.store(session_id, session)
    return session_id
  end
end

.getObject



155
156
157
# File 'lib/ruby_app/session.rb', line 155

def self.get
  Thread.current[:_session]
end

.get_scripts(path = String.interpolate { RubyApp::Session.configuration.scripts.path }) ⇒ Object



208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
# File 'lib/ruby_app/session.rb', line 208

def self.get_scripts(path = String.interpolate { RubyApp::Session.configuration.scripts.path })
  scripts = []
  Dir.new(path).each do |item|
    unless item.start_with?('.')
      _path = File.join(path, item)
      if File.directory?(_path)
        scripts += self.get_scripts(_path)
      elsif _path =~ /\.rb/
        name = _path.gsub(String.interpolate { RubyApp::Session.configuration.scripts.path }, '').gsub(/^\//, '').gsub(/\.rb/, '')
        scripts.push({:name => name,
                      :url  => "/quit?go=#{CGI.escape("/?script=#{name}")}"})
      end
    end
  end
  return scripts.sort { |a, b| a.name <=> b.name }
end

.get_session(session_id) ⇒ Object



129
130
131
132
133
134
135
136
137
138
139
140
141
142
# File 'lib/ruby_app/session.rb', line 129

def self.get_session(session_id)
  session = RubyApp::Session.sessions[session_id]
  if session
    unless session.expired?
      session.reset_expiry!
      return session
    else
      session.quit!
      return nil
    end
  else
    return nil
  end
end

.load!(session_id = nil, script_path = nil) ⇒ Object



159
160
161
162
163
164
165
166
167
168
169
170
# File 'lib/ruby_app/session.rb', line 159

def self.load!(session_id = nil, script_path = nil)
  session = RubyApp::Session.get_session(session_id)
  unless session
    session = Kernel.eval(RubyApp::Session.configuration._class).new
    session.load_script!(script_path) if script_path
    RubyApp::Log.debug("SESSION   RubyApp::Session.session_id=#{session.session_id.inspect}")
    RubyApp::Session.configuration.log.each do |variable|
      RubyApp::Log.debug("SESSION   RubyApp::Request.environment['#{variable}']=#{RubyApp::Request.environment[variable].inspect}")
    end
  end
  Thread.current[:_session] = session
end

.lock_sessionsObject



123
124
125
126
127
# File 'lib/ruby_app/session.rb', line 123

def self.lock_sessions
  ( @@_lock ||= ::Mutex.new ).synchronize do
    yield
  end
end

.sessionsObject



119
120
121
# File 'lib/ruby_app/session.rb', line 119

def self.sessions
  return @@_sessions ||= {}
end

.start_thread!Object



176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
# File 'lib/ruby_app/session.rb', line 176

def self.start_thread!
  unless @@_thread ||= nil
    @@_thread = Thread.new do
      begin
        while true
          RubyApp::Session.sessions.values.each do |session|
            if session.expired?
              RubyApp::Log.debug("SESSION   session.session_id=#{session.session_id.inspect}")
              RubyApp::Log.debug("SESSION   session.quit!")
              session.quit!
            end
          end
          sleep(RubyApp::Session.configuration.interval)
        end
      rescue => exception
        RubyApp::Log.exception(RubyApp::Log::ERROR, exception)
      end
    end

    RubyApp::Log.debug("SESSION   #{RubyApp::Log.prefix(self, __method__)}")

  end
end

.stop_thread!Object



200
201
202
203
204
205
206
# File 'lib/ruby_app/session.rb', line 200

def self.stop_thread!
  if @@_thread ||= nil
    RubyApp::Log.debug("SESSION   #{RubyApp::Log.prefix(self, __method__)}")
    @@_thread.exit
    @@_thread = nil
  end
end

.unload!Object



172
173
174
# File 'lib/ruby_app/session.rb', line 172

def self.unload!
  Thread.current[:_session] = nil
end

Instance Method Details

#add_step!(_class, &block) ⇒ Object



75
76
77
78
79
80
81
# File 'lib/ruby_app/session.rb', line 75

def add_step!(_class, &block)
  caller = Kernel.caller.first.split(':')
  @steps.push({:_class => _class,
               :file => caller[0].gsub(String.interpolate { RubyApp::Session.configuration.scripts.path }, '').gsub(/^\//, ''),
               :line => caller[1].to_i,
               :block => block})
end

#documentObject



52
53
54
# File 'lib/ruby_app/session.rb', line 52

def document
  return @documents.last
end

#expired?Boolean

Returns:

  • (Boolean)


60
61
62
# File 'lib/ruby_app/session.rb', line 60

def expired?
  return @expires <= Time.now
end

#load_script!(path) ⇒ Object



70
71
72
73
# File 'lib/ruby_app/session.rb', line 70

def load_script!(path)
  _path = File.join(String.interpolate { RubyApp::Session.configuration.scripts.path }, "#{path}.rb")
  Kernel.eval(File.read(_path), self.send(:binding), _path)
end

#process_event!(event) ⇒ Object



83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
# File 'lib/ruby_app/session.rb', line 83

def process_event!(event)

  event.process!

  if RubyApp::Session.configuration.scripts.enabled
    step = @steps[@steps_index]
    if step
      begin
        if event.is_a?(step._class)
          @steps_index += 1
          RubyApp::Log.duration(RubyApp::Log::INFO, "STEP      Current #{step._class} #{step.file}:#{step.line}#{step.block ? nil : ' (no block)'}") do
            step.block.call(event) if step.block
          end
          if @steps_index == @steps.length
            RubyApp::Log.info('-' * 80)
            RubyApp::Log.info("STEP      Completed #{@steps.length} steps")
            RubyApp::Log.info('-' * 80)
          else
            step = @steps[@steps_index]
            RubyApp::Log.info("STEP      Next    #{step._class} #{step.file}:#{step.line}#{step.block ? nil : ' (no block)'}")
          end
        end
      rescue => exception
        RubyApp::Log.info("STEP      Exception occurred at #{step.file}:#{step.line}")
        @steps_index = @steps.length
        raise
      end
    end
  end

end

#quit!Object



64
65
66
67
68
# File 'lib/ruby_app/session.rb', line 64

def quit!
  RubyApp::Session.lock_sessions do
    RubyApp::Session.sessions.delete(self.session_id)
  end
end

#reset_expiry!Object



56
57
58
# File 'lib/ruby_app/session.rb', line 56

def reset_expiry!
  @expires = Time.now + RubyApp::Session.configuration.expires
end

#reset_script!Object



115
116
117
# File 'lib/ruby_app/session.rb', line 115

def reset_script!
  @steps_index = 0
end