redis-session-store 0.3.1 → 0.4.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: e8c01a4bd8ba08bf7a390925f4dc40f8b9c008d6
4
- data.tar.gz: 91efb335e55ba86ee01476a74ab4ed0217b48e85
3
+ metadata.gz: 6f3683b0cfb27fd4e78c5639909af2234bb5bbfb
4
+ data.tar.gz: 269d6ab5865c26a9e7f6fb03a467c823569eda6d
5
5
  SHA512:
6
- metadata.gz: cfb8578cfa2a62b70bf4f79ea3574e26c82b5e439d9df7ea3097f6d4bea98543e10590daf2ecc484922feb5868a409af47d5126a768c4f67ba392771837f144f
7
- data.tar.gz: 1b369cf1663b3ba9a581fc0557541b53546762726086ffa0a799d8fd582a45b4c0b73cfd7ee92f50ec74a90f104155422170ab83007c7f15103daf1a8a712dda
6
+ metadata.gz: 896d45ca2a1aa9234bef55dae1170e265bc608a1d300aea69add6a7df1a0de580655017f90804fb4d36d82e5c4beccf8daaafb6a23a0d6702f3b43d0c82eab38
7
+ data.tar.gz: 78bf2f881ecfc5da216115e34f71dbe117b91b99b35a9bd29e030d0bee9380816802b2691b4468539a651dd3ddcc6533bf69a96a9950b9eb8ac389445a586aa3
data/AUTHORS.md CHANGED
@@ -8,6 +8,7 @@ Redis Session Store authors
8
8
  - Gonçalo Silva
9
9
  - Ian C. Anderson
10
10
  - Jorge Pardiñas
11
+ - Justin McNally
11
12
  - Mathias Meyer
12
13
  - Michael Fields
13
14
  - Tim Lossen
data/LICENSE CHANGED
@@ -1,4 +1,4 @@
1
- Copyright (c) 2013 Mathias Meyer
1
+ Copyright (c) 2014 Mathias Meyer
2
2
 
3
3
  MIT License
4
4
 
@@ -4,7 +4,7 @@ require 'redis'
4
4
  # Redis session storage for Rails, and for Rails only. Derived from
5
5
  # the MemCacheStore code, simply dropping in Redis instead.
6
6
  class RedisSessionStore < ActionDispatch::Session::AbstractStore
7
- VERSION = '0.3.1'
7
+ VERSION = '0.4.0'
8
8
 
9
9
  # ==== Options
10
10
  # * +:key+ - Same as with the other cookie stores, key name
@@ -36,11 +36,12 @@ class RedisSessionStore < ActionDispatch::Session::AbstractStore
36
36
  @default_options.merge!(namespace: 'rack:session')
37
37
  @default_options.merge!(redis_options)
38
38
  @redis = Redis.new(redis_options)
39
+ @raise_errors = !!options[:raise_errors]
39
40
  end
40
41
 
41
42
  private
42
43
 
43
- attr_reader :redis, :key, :default_options
44
+ attr_reader :redis, :key, :default_options, :raise_errors
44
45
 
45
46
  def prefixed(sid)
46
47
  "#{default_options[:key_prefix]}#{sid}"
@@ -51,26 +52,29 @@ class RedisSessionStore < ActionDispatch::Session::AbstractStore
51
52
  begin
52
53
  data = redis.get(prefixed(sid))
53
54
  session = data.nil? ? {} : Marshal.load(data)
54
- rescue Errno::ECONNREFUSED
55
+ rescue Errno::ECONNREFUSED => e
56
+ raise e if raise_errors
55
57
  session = {}
56
58
  end
57
59
  [sid, session]
58
60
  end
59
61
 
60
- def set_session(env, sid, session_data, options)
61
- expiry = options[:expire_after] || nil
62
+ def set_session(env, sid, session_data, options = nil)
63
+ expiry = (options || env[ENV_SESSION_OPTIONS_KEY])[:expire_after]
62
64
  if expiry
63
65
  redis.setex(prefixed(sid), expiry, Marshal.dump(session_data))
64
66
  else
65
67
  redis.set(prefixed(sid), Marshal.dump(session_data))
66
68
  end
67
69
  return sid
68
- rescue Errno::ECONNREFUSED
70
+ rescue Errno::ECONNREFUSED => e
71
+ raise e if raise_errors
69
72
  return false
70
73
  end
71
74
 
72
75
  def destroy_session(env, sid, options)
73
76
  redis.del(prefixed(sid))
77
+ return nil if (options || {})[:drop]
74
78
  generate_sid
75
79
  end
76
80
 
@@ -78,12 +82,9 @@ class RedisSessionStore < ActionDispatch::Session::AbstractStore
78
82
  if env['rack.request.cookie_hash'] && env['rack.request.cookie_hash'][key]
79
83
  redis.del(prefixed(env['rack.request.cookie_hash'][key]))
80
84
  end
81
- rescue Errno::ECONNREFUSED
82
- if defined?(Rails)
83
- Rails.logger.warn('RedisSessionStore#destroy: ' <<
84
- 'Connection to redis refused')
85
- else
86
- warn('RedisSessionStore#destroy: Connection to redis refused')
87
- end
85
+ rescue Errno::ECONNREFUSED => e
86
+ raise e if raise_errors
87
+ Rails.logger.warn("RedisSessionStore#destroy: #{e.message}")
88
+ false
88
89
  end
89
90
  end
@@ -108,12 +108,10 @@ describe RedisSessionStore do
108
108
  let(:options) { { expire_after: 123 } }
109
109
 
110
110
  context 'when successfully persisting the session' do
111
-
112
111
  it 'returns the session id' do
113
112
  store.send(:set_session, env, session_id, session_data, options)
114
113
  .should eq(session_id)
115
114
  end
116
-
117
115
  end
118
116
 
119
117
  context 'when unsuccessfully persisting the session' do
@@ -127,6 +125,33 @@ describe RedisSessionStore do
127
125
  end
128
126
  end
129
127
 
128
+ context 'when no expire_after option is given' do
129
+ let(:options) { {} }
130
+
131
+ it 'sets the session value without expiry' do
132
+ store.send(:set_session, env, session_id, session_data, options)
133
+ .should eq(session_id)
134
+ end
135
+ end
136
+
137
+ context 'when redis is down' do
138
+ before { store.stub(:redis).and_raise(Errno::ECONNREFUSED) }
139
+
140
+ it 'returns false' do
141
+ store.send(:set_session, env, session_id, session_data, options)
142
+ .should eq(false)
143
+ end
144
+
145
+ context 'when :raise_errors option is truthy' do
146
+ let(:options) { { raise_errors: true } }
147
+
148
+ it 'explodes' do
149
+ expect do
150
+ store.send(:set_session, env, session_id, session_data, options)
151
+ end.to raise_error(Errno::ECONNREFUSED)
152
+ end
153
+ end
154
+ end
130
155
  end
131
156
 
132
157
  describe 'fetching a session' do
@@ -146,13 +171,22 @@ describe RedisSessionStore do
146
171
  end
147
172
 
148
173
  context 'when redis is down' do
174
+ before { store.stub(:redis).and_raise(Errno::ECONNREFUSED) }
149
175
 
150
176
  it 'should return an empty session hash' do
151
- store.stub(:redis).and_raise(Errno::ECONNREFUSED)
152
-
153
177
  expect(store.send(:get_session, double('env'), fake_key))
154
178
  .to eq([fake_key, {}])
155
179
  end
180
+
181
+ context 'when :raise_errors option is truthy' do
182
+ let(:options) { { raise_errors: true } }
183
+
184
+ it 'explodes' do
185
+ expect do
186
+ store.send(:get_session, double('env'), fake_key)
187
+ end.to raise_error(Errno::ECONNREFUSED)
188
+ end
189
+ end
156
190
  end
157
191
  end
158
192
 
@@ -174,6 +208,24 @@ describe RedisSessionStore do
174
208
 
175
209
  store.send(:destroy, env)
176
210
  end
211
+
212
+ context 'when redis is down' do
213
+ before { store.stub(:redis).and_raise(Errno::ECONNREFUSED) }
214
+
215
+ it 'should return false' do
216
+ expect(store.send(:destroy, env)).to be_false
217
+ end
218
+
219
+ context 'when :raise_errors option is truthy' do
220
+ let(:options) { { raise_errors: true } }
221
+
222
+ it 'explodes' do
223
+ expect do
224
+ store.send(:destroy, env)
225
+ end.to raise_error(Errno::ECONNREFUSED)
226
+ end
227
+ end
228
+ end
177
229
  end
178
230
 
179
231
  context 'when destroyed via #destroy_session' do
data/spec/spec_helper.rb CHANGED
@@ -1,6 +1,6 @@
1
1
  # vim:fileencoding=utf-8
2
2
 
3
3
  require 'simplecov'
4
- require_relative 'fake_action_dispatch_session_abstract_store'
4
+ require_relative 'support'
5
5
  require 'fakeredis/rspec'
6
6
  require 'redis-session-store'
@@ -4,6 +4,7 @@ unless defined?(ActionDispatch::Session::AbstractStore)
4
4
  module ActionDispatch # rubocop:disable Documentation
5
5
  module Session
6
6
  class AbstractStore
7
+ ENV_SESSION_OPTIONS_KEY = 'rack.session.options'.freeze
7
8
  DEFAULT_OPTIONS = {
8
9
  key: '_session_id',
9
10
  path: '/',
@@ -30,3 +31,13 @@ unless defined?(ActionDispatch::Session::AbstractStore)
30
31
  end
31
32
  end
32
33
  end
34
+
35
+ unless defined?(Rails)
36
+ require 'logger'
37
+
38
+ module Rails
39
+ def self.logger
40
+ @logger ||= Logger.new('/dev/null')
41
+ end
42
+ end
43
+ end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: redis-session-store
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.3.1
4
+ version: 0.4.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Mathias Meyer
@@ -118,9 +118,9 @@ files:
118
118
  - Rakefile
119
119
  - lib/redis-session-store.rb
120
120
  - redis-session-store.gemspec
121
- - spec/fake_action_dispatch_session_abstract_store.rb
122
121
  - spec/redis_session_store_spec.rb
123
122
  - spec/spec_helper.rb
123
+ - spec/support.rb
124
124
  homepage: https://github.com/roidrage/redis-session-store
125
125
  licenses:
126
126
  - MIT
@@ -141,7 +141,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
141
141
  version: '0'
142
142
  requirements: []
143
143
  rubyforge_project:
144
- rubygems_version: 2.0.3
144
+ rubygems_version: 2.0.14
145
145
  signing_key:
146
146
  specification_version: 4
147
147
  summary: A drop-in replacement for e.g. MemCacheStore to store Rails sessions (and