couchrest_session_store 0.2.2 → 0.2.4
Sign up to get free protection for your applications and to get access to all the features.
- data/couchrest_session_store.gemspec +1 -1
- data/lib/couchrest/session/document.rb +23 -9
- data/lib/couchrest/session/store.rb +23 -25
- data/lib/couchrest/session/utility.rb +0 -4
- data/test/couch_tester.rb +5 -3
- data/test/session_document_test.rb +27 -0
- data/test/session_store_test.rb +1 -1
- metadata +3 -2
@@ -14,7 +14,7 @@ Gem::Specification.new do |gem|
|
|
14
14
|
gem.files = `git ls-files`.split("\n")
|
15
15
|
gem.name = "couchrest_session_store"
|
16
16
|
gem.require_paths = ["lib"]
|
17
|
-
gem.version = '0.2.
|
17
|
+
gem.version = '0.2.4'
|
18
18
|
|
19
19
|
gem.add_dependency "couchrest"
|
20
20
|
gem.add_dependency "couchrest_model"
|
@@ -1,16 +1,16 @@
|
|
1
1
|
require 'couchrest/session/utility'
|
2
2
|
require 'time'
|
3
3
|
|
4
|
-
class CouchRest::Session::Document
|
4
|
+
class CouchRest::Session::Document < CouchRest::Document
|
5
|
+
include CouchRest::Model::Configuration
|
6
|
+
include CouchRest::Model::Connection
|
5
7
|
include CouchRest::Session::Utility
|
6
8
|
|
7
|
-
|
8
|
-
@doc = doc
|
9
|
-
end
|
9
|
+
use_database "sessions"
|
10
10
|
|
11
|
-
def self.
|
11
|
+
def self.fetch(sid)
|
12
12
|
self.allocate.tap do |session_doc|
|
13
|
-
session_doc.
|
13
|
+
session_doc.fetch(sid)
|
14
14
|
end
|
15
15
|
end
|
16
16
|
|
@@ -22,15 +22,26 @@ class CouchRest::Session::Document
|
|
22
22
|
|
23
23
|
def self.build_or_update(sid, session, options = {})
|
24
24
|
options[:marshal_data] = true if options[:marshal_data].nil?
|
25
|
-
doc = self.
|
25
|
+
doc = self.fetch(sid)
|
26
26
|
doc.update(session, options)
|
27
27
|
return doc
|
28
28
|
rescue RestClient::ResourceNotFound
|
29
29
|
self.build(sid, session, options)
|
30
30
|
end
|
31
31
|
|
32
|
-
def
|
33
|
-
|
32
|
+
def self.find_by_expires(options = {})
|
33
|
+
options[:reduce] ||= false
|
34
|
+
design = database.get '_design/Session'
|
35
|
+
response = design.view :by_expires, options
|
36
|
+
response['rows']
|
37
|
+
end
|
38
|
+
|
39
|
+
def initialize(doc)
|
40
|
+
@doc = doc
|
41
|
+
end
|
42
|
+
|
43
|
+
def fetch(sid = nil)
|
44
|
+
@doc = database.get(sid || doc['_id'])
|
34
45
|
end
|
35
46
|
|
36
47
|
def to_session
|
@@ -56,6 +67,9 @@ class CouchRest::Session::Document
|
|
56
67
|
|
57
68
|
def save
|
58
69
|
database.save_doc(doc)
|
70
|
+
rescue RestClient::Conflict
|
71
|
+
fetch
|
72
|
+
retry
|
59
73
|
end
|
60
74
|
|
61
75
|
def expired?
|
@@ -1,47 +1,36 @@
|
|
1
1
|
class CouchRest::Session::Store < ActionDispatch::Session::AbstractStore
|
2
2
|
|
3
|
-
|
4
|
-
|
5
|
-
|
6
|
-
def initialize(app, options = {})
|
7
|
-
super
|
8
|
-
self.class.set_options(options)
|
3
|
+
# delegate configure to document
|
4
|
+
def self.configure(*args, &block)
|
5
|
+
CouchRest::Session::Document.configure *args, &block
|
9
6
|
end
|
10
7
|
|
11
8
|
def self.set_options(options)
|
12
9
|
@options = options
|
10
|
+
if @options[:database]
|
11
|
+
CouchRest::Session::Document.use_database @options[:database]
|
12
|
+
end
|
13
13
|
end
|
14
14
|
|
15
|
-
|
16
|
-
|
17
|
-
|
18
|
-
end
|
19
|
-
|
20
|
-
def self.initialize_database
|
21
|
-
use_database @options[:database] || "sessions"
|
15
|
+
def initialize(app, options = {})
|
16
|
+
super
|
17
|
+
self.class.set_options(options)
|
22
18
|
end
|
23
19
|
|
24
20
|
def cleanup(rows)
|
25
21
|
rows.each do |row|
|
26
|
-
doc = CouchRest::Session::Document.
|
22
|
+
doc = CouchRest::Session::Document.fetch(row['id'])
|
27
23
|
doc.delete
|
28
24
|
end
|
29
25
|
end
|
30
26
|
|
31
27
|
def expired
|
32
|
-
find_by_expires startkey: 1,
|
28
|
+
CouchRest::Session::Document.find_by_expires startkey: 1,
|
33
29
|
endkey: Time.now.utc.iso8601
|
34
30
|
end
|
35
31
|
|
36
32
|
def never_expiring
|
37
|
-
find_by_expires endkey: 1
|
38
|
-
end
|
39
|
-
|
40
|
-
def find_by_expires(options = {})
|
41
|
-
options[:reduce] ||= false
|
42
|
-
design = self.class.database.get '_design/Session'
|
43
|
-
response = design.view :by_expires, options
|
44
|
-
response['rows']
|
33
|
+
CouchRest::Session::Document.find_by_expires endkey: 1
|
45
34
|
end
|
46
35
|
|
47
36
|
private
|
@@ -55,6 +44,12 @@ class CouchRest::Session::Store < ActionDispatch::Session::AbstractStore
|
|
55
44
|
rescue RestClient::ResourceNotFound
|
56
45
|
# session data does not exist anymore
|
57
46
|
return [sid, {}]
|
47
|
+
rescue RestClient::Unauthorized,
|
48
|
+
Errno::EHOSTUNREACH,
|
49
|
+
Errno::ECONNREFUSED => e
|
50
|
+
# can't connect to couch. We add some status to the session
|
51
|
+
# so the app can react. (Display error for example)
|
52
|
+
return [sid, {"_status" => {"couch" => "unreachable"}}]
|
58
53
|
end
|
59
54
|
|
60
55
|
def set_session(env, sid, session, options)
|
@@ -62,7 +57,10 @@ class CouchRest::Session::Store < ActionDispatch::Session::AbstractStore
|
|
62
57
|
doc = build_or_update_doc(sid, session, options)
|
63
58
|
doc.save
|
64
59
|
return sid
|
65
|
-
|
60
|
+
# if we can't store the session we just return false.
|
61
|
+
rescue RestClient::Unauthorized,
|
62
|
+
Errno::EHOSTUNREACH,
|
63
|
+
Errno::ECONNREFUSED => e
|
66
64
|
return false
|
67
65
|
end
|
68
66
|
|
@@ -90,7 +88,7 @@ class CouchRest::Session::Store < ActionDispatch::Session::AbstractStore
|
|
90
88
|
# but better be save than sorry.
|
91
89
|
def secure_get(sid)
|
92
90
|
raise RestClient::ResourceNotFound if /^_design\/(.*)/ =~ sid
|
93
|
-
CouchRest::Session::Document.
|
91
|
+
CouchRest::Session::Document.fetch(sid)
|
94
92
|
end
|
95
93
|
|
96
94
|
end
|
data/test/couch_tester.rb
CHANGED
@@ -3,14 +3,16 @@
|
|
3
3
|
# on the SessionStore
|
4
4
|
#
|
5
5
|
|
6
|
-
class CouchTester
|
6
|
+
class CouchTester < CouchRest::Document
|
7
7
|
include CouchRest::Model::Configuration
|
8
8
|
include CouchRest::Model::Connection
|
9
9
|
|
10
|
-
|
10
|
+
use_database 'sessions'
|
11
11
|
|
12
12
|
def initialize(options = {})
|
13
|
-
|
13
|
+
if options[:database]
|
14
|
+
self.class.use_database options[:database]
|
15
|
+
end
|
14
16
|
end
|
15
17
|
|
16
18
|
def get(sid)
|
@@ -0,0 +1,27 @@
|
|
1
|
+
require File.expand_path(File.dirname(__FILE__) + '/test_helper')
|
2
|
+
|
3
|
+
class SessionDocumentTest < MiniTest::Test
|
4
|
+
|
5
|
+
def test_storing_session
|
6
|
+
sid = '1234'
|
7
|
+
session = {'a' => 'b'}
|
8
|
+
options = {}
|
9
|
+
doc = CouchRest::Session::Document.build_or_update(sid, session, options)
|
10
|
+
doc.save
|
11
|
+
doc.fetch(sid)
|
12
|
+
assert_equal session, doc.to_session
|
13
|
+
end
|
14
|
+
|
15
|
+
def test_storing_session_with_conflict
|
16
|
+
sid = '1234'
|
17
|
+
session = {'a' => 'b'}
|
18
|
+
options = {}
|
19
|
+
doc = CouchRest::Session::Document.build_or_update(sid, session, options)
|
20
|
+
doc2 = CouchRest::Session::Document.build_or_update(sid, session, options)
|
21
|
+
doc.save
|
22
|
+
doc2.save
|
23
|
+
doc2.fetch(sid)
|
24
|
+
assert_equal session, doc2.to_session
|
25
|
+
end
|
26
|
+
|
27
|
+
end
|
data/test/session_store_test.rb
CHANGED
@@ -64,7 +64,7 @@ class SessionStoreTest < MiniTest::Test
|
|
64
64
|
|
65
65
|
def test_stored_and_not_expired_yet
|
66
66
|
sid, session = expiring_session
|
67
|
-
doc = CouchRest::Session::Document.
|
67
|
+
doc = CouchRest::Session::Document.fetch(sid)
|
68
68
|
expires = doc.send :expires
|
69
69
|
assert expires
|
70
70
|
assert !doc.expired?
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: couchrest_session_store
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.2.
|
4
|
+
version: 0.2.4
|
5
5
|
prerelease:
|
6
6
|
platform: ruby
|
7
7
|
authors:
|
@@ -9,7 +9,7 @@ authors:
|
|
9
9
|
autorequire:
|
10
10
|
bindir: bin
|
11
11
|
cert_chain: []
|
12
|
-
date: 2013-12-
|
12
|
+
date: 2013-12-13 00:00:00.000000000 Z
|
13
13
|
dependencies:
|
14
14
|
- !ruby/object:Gem::Dependency
|
15
15
|
name: couchrest
|
@@ -110,6 +110,7 @@ files:
|
|
110
110
|
- lib/couchrest/session/utility.rb
|
111
111
|
- lib/couchrest_session_store.rb
|
112
112
|
- test/couch_tester.rb
|
113
|
+
- test/session_document_test.rb
|
113
114
|
- test/session_store_test.rb
|
114
115
|
- test/setup_couch.sh
|
115
116
|
- test/test_clock.rb
|