Class: SessionPlugin

Inherits:
Rubko::Plugin show all
Defined in:
lib/rubko/plugins/session.rb

Instance Attribute Summary collapse

Attributes included from Rubko::Base

#parent

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Rubko::Plugin

#config

Methods included from Rubko::Base

#camelize, #finalize, #httpGet, #initialize, #jsonParse, #loadController, #loadFile, #loadModel, #loadPlugin, #loadView, #method_missing, #uncamelize

Dynamic Method Handling

This class handles dynamic methods through the method_missing method in the class Rubko::Base

Instance Attribute Details

Returns the value of attribute cookie.



35
36
37
# File 'lib/rubko/plugins/session.rb', line 35

def cookie
  @cookie
end

#hashSizeObject

Returns the value of attribute hashSize.



35
36
37
# File 'lib/rubko/plugins/session.rb', line 35

def hashSize
  @hashSize
end

#nameObject

Returns the value of attribute name.



35
36
37
# File 'lib/rubko/plugins/session.rb', line 35

def name
  @name
end

#pathObject

Returns the value of attribute path.



35
36
37
# File 'lib/rubko/plugins/session.rb', line 35

def path
  @path
end

#purgeRateObject

Returns the value of attribute purgeRate.



35
36
37
# File 'lib/rubko/plugins/session.rb', line 35

def purgeRate
  @purgeRate
end

#storageObject

Returns the value of attribute storage.



35
36
37
# File 'lib/rubko/plugins/session.rb', line 35

def storage
  @storage
end

#timeoutObject

Returns the value of attribute timeout.



35
36
37
# File 'lib/rubko/plugins/session.rb', line 35

def timeout
  @timeout
end

Class Method Details

.delegate(*funcs) ⇒ Object



77
78
79
80
81
82
83
84
# File 'lib/rubko/plugins/session.rb', line 77

def self.delegate(*funcs)
	funcs.each { |func|
		define_method(func) { |*args|
			sid = id
			storage.send func, name, sid, *args
		}
	}
end

Instance Method Details

#[]=(*args, val) ⇒ Object



71
72
73
74
75
# File 'lib/rubko/plugins/session.rb', line 71

def []=(*args, val)
	create
	return unless id
	storage[name, id, *args] = val
end

#createObject



47
48
49
50
51
# File 'lib/rubko/plugins/session.rb', line 47

def create
	return if id
	cookie[name] = @id = hash unless ip?
	storage[name, @id] = Time.new
end

#destroyObject



53
54
55
56
57
58
# File 'lib/rubko/plugins/session.rb', line 53

def destroy
	return unless id
	storage.prune name, id
	cookie[name] = nil
	@id = false
end

#idObject



14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
# File 'lib/rubko/plugins/session.rb', line 14

def id
	if @id.nil?
		config
		purge if 0 == rand(purgeRate)

		@id = if ip?
			cookie.ip
		else
			cookie[name] || false
		end

		if @id
			if time = storage[name, @id]
				destroy if Time.now - time > timeout
			end
			storage[name, @id] = Time.new
		end
	end
	@id
end

#initObject



4
5
6
7
8
9
10
11
12
# File 'lib/rubko/plugins/session.rb', line 4

def init
	@name = :session	# cookie name
	@timeout = 2*60*60	# session timeout in seconds of inactivity
	@purgeRate = 10_000	# interval (requests) to clear expired sessions
	@hashSize = 16		# entropy of hash in byes

	@storage = memory
	@cookie = loadPlugin :cookie
end

#ip?Boolean

Returns:

  • (Boolean)


37
38
39
# File 'lib/rubko/plugins/session.rb', line 37

def ip?
	name == :ip
end

#purgeObject



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

def purge
	storage.values(name).each { |id, time|
		destroy if Time.now - time > timeout
	}
end

#residObject



64
65
66
67
# File 'lib/rubko/plugins/session.rb', line 64

def resid
	return if ip? || !id
	storage.rename name, id, (cookie[name] = hash)
end