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



139
140
141
142
143
144
145
146
147
148
# File 'lib/ruby_app/session.rb', line 139

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



150
151
152
# File 'lib/ruby_app/session.rb', line 150

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

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



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

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



124
125
126
127
128
129
130
131
132
133
134
135
136
137
# File 'lib/ruby_app/session.rb', line 124

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



154
155
156
157
158
159
160
161
# File 'lib/ruby_app/session.rb', line 154

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
  end
  Thread.current[:_session] = session
end

.lock_sessionsObject



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

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

.sessionsObject



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

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

.start_thread!Object



167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
# File 'lib/ruby_app/session.rb', line 167

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("QUIT  #{session.session_id.inspect}")
              session.quit!
            end
          end
          sleep(RubyApp::Session.configuration.interval)
        end
      rescue => exception
        RubyApp::Log.exception(RubyApp::Log::ERROR, exception)
      end
    end

    at_exit do
      begin
        RubyApp::Session.stop_thread!
      rescue => exception
        RubyApp::Log.exception(RubyApp::Log::ERROR, exception)
      end
    end

    RubyApp::Log.debug(RubyApp::Log.prefix(self, __method__))

  end
end

.stop_thread!Object



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

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

.unload!Object



163
164
165
# File 'lib/ruby_app/session.rb', line 163

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
# 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   #{step.file}:#{step.line}") do
            step.block.call(event)
          end
          if @steps_index == @steps.length
            RubyApp::Log.info("STEP   Completed #{@steps.length} steps")
          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



110
111
112
# File 'lib/ruby_app/session.rb', line 110

def reset_script!
  @steps_index = 0
end