zorglub 0.1.0 → 0.1.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,152 +1,148 @@
1
- # -*- coding: UTF-8 -*-
2
-
3
1
  require 'securerandom'
4
2
 
5
3
  module Zorglub
4
+ class Node
5
+ @sessions = {}
6
+
7
+ class << self
8
+ attr_reader :sessions
9
+ end
10
+
11
+ def session
12
+ @session ||= SessionHash.new @request, @response, Node.sessions, app.opt(:session_options)
13
+ end
14
+ end
15
+
16
+ class SessionHash < Hash
17
+ def initialize(req, resp, sessions, options)
18
+ @request = req
19
+ @response = resp
20
+ @sessions = sessions
21
+ @sid = nil
22
+ @options = options
23
+ super()
24
+ end
25
+
26
+ def [](key)
27
+ load_data!
28
+ super key
29
+ end
30
+
31
+ def key?(key)
32
+ load_data!
33
+ super key
34
+ end
35
+ alias include? key?
36
+
37
+ def []=(key, value)
38
+ load_data!
39
+ super key, value
40
+ end
41
+
42
+ def clear
43
+ load_data!
44
+ # @response.delete_cookie @options[:key]
45
+ # @sessions.delete @sid
46
+ # @sid = nil
47
+ super
48
+ end
49
+
50
+ def to_hash
51
+ load_data!
52
+ h = {}.replace(self)
53
+ h.delete_if { |_k, v| v.nil? }
54
+ h
55
+ end
56
+
57
+ def update(hash)
58
+ load_data!
59
+ super stringify_keys(hash)
60
+ end
61
+
62
+ def delete(key)
63
+ load_data!
64
+ super key
65
+ end
66
+
67
+ def inspect
68
+ if loaded?
69
+ super
70
+ else
71
+ "#<#{self.class}:0x#{object_id.to_s(16)} not yet loaded>"
72
+ end
73
+ end
74
+
75
+ def exists?
76
+ loaded? ? @sessions.key?(@sid) : false
77
+ end
78
+
79
+ def loaded?
80
+ !@sid.nil?
81
+ end
82
+
83
+ def empty?
84
+ load_data!
85
+ super
86
+ end
87
+
88
+ private
89
+
90
+ def load_data!
91
+ return if loaded?
92
+ return unless @options[:enabled]
93
+
94
+ sid = @request.cookies[@options[:key]]
95
+ if sid.nil?
96
+ sid = generate_sid!
97
+ @response.set_cookie @options[:key], sid
98
+ end
99
+ replace @sessions[sid] ||= {}
100
+ @sessions[sid] = self
101
+ @sid = sid
102
+ end
103
+
104
+ def stringify_keys(other)
105
+ hash = {}
106
+ other.each do |key, value|
107
+ hash[key] = value
108
+ end
109
+ hash
110
+ end
111
+
112
+ def generate_sid!
113
+ begin sid = sid_algorithm end while @sessions.key? sid
114
+ sid
115
+ end
6
116
 
7
- class Node
8
-
9
- @sessions = {}
10
-
11
- class << self
12
- attr_reader :sessions
13
- end
14
-
15
- def session
16
- @session ||= SessionHash.new @request, @response, Node.sessions, app.opt(:session_options)
17
- end
18
- end
19
-
20
- class SessionHash < Hash
21
-
22
- def initialize req, resp, sessions, options
23
- @request = req
24
- @response = resp
25
- @sessions = sessions
26
- @sid = nil
27
- @options = options
28
- super()
29
- end
30
-
31
- def [] key
32
- load_data!
33
- super key
34
- end
35
-
36
- def has_key? key
37
- load_data!
38
- super key
39
- end
40
- alias :key? :has_key?
41
- alias :include? :has_key?
42
-
43
- def []= key, value
44
- load_data!
45
- super key, value
46
- end
47
-
48
- def clear
49
- load_data!
50
- # @response.delete_cookie @options[:key]
51
- # @sessions.delete @sid
52
- # @sid = nil
53
- super
54
- end
55
-
56
- def to_hash
57
- load_data!
58
- h = {}.replace(self)
59
- h.delete_if { |k,v| v.nil? }
60
- h
61
- end
62
-
63
- def update hash
64
- load_data!
65
- super stringify_keys(hash)
66
- end
67
-
68
- def delete key
69
- load_data!
70
- super key
71
- end
72
-
73
- def inspect
74
- if loaded?
75
- super
76
- else
77
- "#<#{self.class}:0x#{self.object_id.to_s(16)} not yet loaded>"
78
- end
79
- end
80
-
81
- def exists?
82
- ( loaded? ? @sessions.has_key?(@sid) : false )
83
- end
84
-
85
- def loaded?
86
- not @sid.nil?
87
- end
88
-
89
- def empty?
90
- load_data!
91
- super
92
- end
93
-
94
- private
95
-
96
- def load_data!
97
- return if loaded?
98
- if @options[:enabled]
99
- sid = @request.cookies[@options[:key]]
100
- if sid.nil?
101
- sid = generate_sid!
102
- @response.set_cookie @options[:key], sid
103
- end
104
- replace @sessions[sid] ||={}
105
- @sessions[sid] = self
106
- @sid = sid
107
- end
108
- end
109
-
110
- def stringify_keys other
111
- hash = {}
112
- other.each do |key, value|
113
- hash[key] = value
114
- end
115
- hash
116
- end
117
-
118
- def generate_sid!
119
- begin sid = sid_algorithm end while @sessions.has_key? sid
120
- sid
121
- end
122
-
123
- begin
124
- require 'securerandom'
125
- # Using SecureRandom, optional length.
126
- # SecureRandom is available since Ruby 1.8.7.
127
- # For Ruby versions earlier than that, you can require the uuidtools gem,
128
- # which has a drop-in replacement for SecureRandom.
129
- def sid_algorithm; SecureRandom.hex(@options[:sid_len]); end
130
- rescue LoadError
131
- require 'openssl'
132
- # Using OpenSSL::Random for generation, this is comparable in performance
133
- # with stdlib SecureRandom and also allows for optional length, it should
134
- # have the same behaviour as the SecureRandom::hex method of the
135
- # uuidtools gem.
136
- def sid_algorithm
137
- OpenSSL::Random.random_bytes(@options[:sid_len] / 2).unpack('H*')[0]
138
- end
139
- rescue LoadError
140
- # Digest::SHA2::hexdigest produces a string of length 64, although
141
- # collisions are not very likely, the entropy is still very low and
142
- # length is not optional.
143
- #
144
- # Replacing it with OS-provided random data would take a lot of code and
145
- # won't be as cross-platform as Ruby.
146
- def sid_algorithm
147
- entropy = [ srand, rand, Time.now.to_f, rand, $$, rand, object_id ]
148
- Digest::SHA2.hexdigest(entropy.join)
149
- end
150
- end
117
+ begin
118
+ require 'securerandom'
119
+ # Using SecureRandom, optional length.
120
+ # SecureRandom is available since Ruby 1.8.7.
121
+ # For Ruby versions earlier than that, you can require the uuidtools gem,
122
+ # which has a drop-in replacement for SecureRandom.
123
+ def sid_algorithm
124
+ SecureRandom.hex(@options[:sid_len])
125
+ end
126
+ rescue LoadError
127
+ require 'openssl'
128
+ # Using OpenSSL::Random for generation, this is comparable in performance
129
+ # with stdlib SecureRandom and also allows for optional length, it should
130
+ # have the same behaviour as the SecureRandom::hex method of the
131
+ # uuidtools gem.
132
+ def sid_algorithm
133
+ OpenSSL::Random.random_bytes(@options[:sid_len] / 2).unpack1('H*')[0]
134
+ end
135
+ rescue LoadError
136
+ # Digest::SHA2::hexdigest produces a string of length 64, although
137
+ # collisions are not very likely, the entropy is still very low and
138
+ # length is not optional.
139
+ #
140
+ # Replacing it with OS-provided random data would take a lot of code and
141
+ # won't be as cross-platform as Ruby.
142
+ def sid_algorithm
143
+ entropy = [srand, rand, Time.now.to_f, rand, $$, rand, object_id]
144
+ Digest::SHA2.hexdigest(entropy.join)
145
+ end
151
146
  end
147
+ end
152
148
  end
data/lib/zorglub.rb CHANGED
@@ -1,11 +1,8 @@
1
1
  #! /usr/bin/env ruby
2
- # -*- coding: UTF-8 -*-
3
2
 
4
3
  module Zorglub
5
- VERSION = '0.1.0'
4
+ VERSION = '0.1.2'.freeze
6
5
  end
7
6
 
8
7
  require 'zorglub/node'
9
8
  require 'zorglub/app'
10
-
11
- # EOF
data/spec/app_spec.rb CHANGED
@@ -1,43 +1,37 @@
1
- # -*- coding: UTF-8 -*-
2
-
3
1
  require 'spec_helper'
4
2
 
5
3
  describe Zorglub do
4
+ describe Zorglub::App do
5
+ it 'map should add a mapped node' do
6
+ expect(APP.at('/temp')).to be_nil
7
+ APP.map '/temp', Temp
8
+ expect(APP.at('/temp')).to be Temp
9
+ end
6
10
 
7
- describe Zorglub::App do
8
-
9
- it "map should add a mapped node" do
10
- expect(APP.at("/temp")).to be_nil
11
- APP.map "/temp", Temp
12
- expect(APP.at("/temp")).to be Temp
13
- end
14
-
15
- it "delete should delete a mapped node" do
16
- expect(APP.at("/temp")).to be Temp
17
- APP.delete "/temp"
18
- expect(APP.at("/temp")).to be_nil
19
- end
20
-
21
- it "at should return mapped node" do
22
- expect(APP.at("/node1")).to be Node1
23
- end
24
-
25
- it "at should return nil if no Node mapped" do
26
- expect(APP.at("/none")).to be_nil
27
- end
11
+ it 'delete should delete a mapped node' do
12
+ expect(APP.at('/temp')).to be Temp
13
+ APP.delete '/temp'
14
+ expect(APP.at('/temp')).to be_nil
15
+ end
28
16
 
29
- it "to should return path to node" do
30
- expect(APP.to(Node1)).to eq "/node1"
31
- end
17
+ it 'at should return mapped node' do
18
+ expect(APP.at('/node1')).to be Node1
19
+ end
32
20
 
33
- it "to should return nil if not an existing Node" do
34
- expect(APP.to(nil)).to be_nil
35
- end
21
+ it 'at should return nil if no Node mapped' do
22
+ expect(APP.at('/none')).to be_nil
23
+ end
36
24
 
37
- it "to_hash should return a correct hash" do
38
- expect(APP.to_hash["/node1"]).to be Node1
39
- end
25
+ it 'to should return path to node' do
26
+ expect(APP.to(Node1)).to eq '/node1'
27
+ end
40
28
 
29
+ it 'to should return nil if not an existing Node' do
30
+ expect(APP.to(nil)).to be_nil
41
31
  end
42
32
 
33
+ it 'to_hash should return a correct hash' do
34
+ expect(APP.to_hash['/node1']).to be Node1
35
+ end
36
+ end
43
37
  end
@@ -1 +1 @@
1
- %h1="Hello world"
1
+ %h1="Hello #{@name}"