multi_session_store 0.1.0 → 0.2.1

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
  SHA256:
3
- metadata.gz: 7b649eeeeb5c3a7664b5150d21a3298b1e6aa01f6a12acfff10d948decd446a9
4
- data.tar.gz: 72de55c4116b17e9adfd9af8d182b9fae01614d4a8eeb367665bd54790ee28cd
3
+ metadata.gz: d5da82bc7386c040cd586cc36c42feb917b41e55886c38b7dcdedd33705d478c
4
+ data.tar.gz: ea4d3ead909c521a46f2d61e1ad8a666508a65533652d70ce151e6c68a394cd8
5
5
  SHA512:
6
- metadata.gz: 8ce98ad8fa45122300bd79569dee41e0d491b39b0626ee315adccc8018bf77336766f0e0fa1b9b69701c4d5e9c0a5bed913ff4b58b1fe309c5ba16cb438e1ab5
7
- data.tar.gz: 9bfe13213d26b635ca339566b73ea11c5b6c7cb951716867dd74d8a9cdbb0cdacd4909bcefb35d0ccb7e12a63f87e8b0f9b8b59473f1a9d3037ea602d5c5eeda
6
+ metadata.gz: a6fe4fd2eb79b0bdea61ac1e38b7956a46bf724b756bb2353d407c254f5a4376f97c6123287d1c95445c164547c471c25101e0ef92b5296e2d3307567855dd70
7
+ data.tar.gz: 226b38cfecfbb54d383aa2b7ea1460bab6a069d98361a16eb54860da8420b43ae550d84b20bb2b925442f21d75ab6bd0f693c26b62feb38fdfcce9e009ea752b
data/.gitignore CHANGED
@@ -10,3 +10,4 @@
10
10
  # rspec failure tracking
11
11
  .rspec_status
12
12
  /Gemfile.lock
13
+ .ruby-version
data/bin/console CHANGED
@@ -1,7 +1,7 @@
1
1
  #!/usr/bin/env ruby
2
2
 
3
3
  require "bundler/setup"
4
- require "multi/session/store"
4
+ require "multi_session_store"
5
5
 
6
6
  # You can add fixtures and/or initialization code here to make experimenting
7
7
  # with your gem easier. You can also use a different console, if you like.
@@ -1,47 +1,52 @@
1
1
  require "multi_session_store/version"
2
+ require "multi_session_store/subsession_generator_middleware"
3
+ require "multi_session_store/default_url_options"
4
+ require "multi_session_store/railtie" if defined? Rails
2
5
  require "action_dispatch"
3
6
 
4
7
  module ActionDispatch
5
8
  module Session
6
9
  class MultiSessionStore < AbstractStore
10
+ DEFAULT_SESSION_EXPIRATION = 24 * 60 * 60
11
+
7
12
  def initialize(app, options = {})
8
- @cache = options[:cache]
9
- options[:expire_after] ||= @cache.options[:expires_in]
10
- @param = options[:param]
13
+ options[:expire_after] ||= DEFAULT_SESSION_EXPIRATION
14
+ options[:param_name] ||= 'subsession_id'
15
+ @redis = options[:redis]
16
+ @param_name = options[:param_name]
11
17
  @serializer = options[:serializer]
12
-
13
18
  super
14
19
  end
15
20
 
16
21
  def find_session(env, sid)
17
- sid ||= generate_sid
18
- session = @serializer.parse(@cache.read(cache_key(env, sid)) || "{}")
19
-
20
- [sid, session]
22
+ if sid
23
+ serialized_session_data = @redis.get session_store_key(env, sid)
24
+ session_data = serialized_session_data ? @serializer.parse(serialized_session_data) : {}
25
+ [sid, session_data]
26
+ else
27
+ [generate_sid, {}]
28
+ end
21
29
  end
22
30
 
23
31
  def write_session(env, sid, session, options)
24
- key = cache_key(env, sid)
25
-
32
+ key = session_store_key env, sid
26
33
  if session
27
- @cache.write(key, @serializer.dump(session), expires_in: options[:expire_after])
34
+ @redis.set key, @serializer.dump(session), ex: options[:expire_after]
28
35
  else
29
- @cache.delete(key)
36
+ @redis.del key
30
37
  end
31
-
32
38
  sid
33
39
  end
34
40
 
35
41
  def delete_session(env, sid, options)
36
- @cache.delete(cache_key(env, sid))
37
-
42
+ @redis.del session_store_key(env, sid)
38
43
  sid
39
44
  end
40
45
 
41
- private
46
+ private
42
47
 
43
- def cache_key(env, sid)
44
- subsession_id = env.params[@param] || "no_subsession"
48
+ def session_store_key(env, sid)
49
+ subsession_id = env.params[@param_name] || 'no_subsession'
45
50
  "_session_id:#{sid}:#{subsession_id}"
46
51
  end
47
52
  end
@@ -0,0 +1,12 @@
1
+ module MultiSessionStore
2
+ module DefaultUrlOptions
3
+ def default_url_options
4
+ options = params[:subsession_id] ? {subsession_id: params[:subsession_id]} : {}
5
+ begin
6
+ super.merge options
7
+ rescue NoMethodError
8
+ options
9
+ end
10
+ end
11
+ end
12
+ end
@@ -0,0 +1,15 @@
1
+ module MultiSessionStore
2
+ class Railtie < Rails::Railtie
3
+ config.multi_session_store = ActiveSupport::OrderedOptions.new
4
+
5
+ initializer 'multi_session_store.add_middleware' do |app|
6
+ app.config.middleware.insert_before ActionDispatch::Session::MultiSessionStore,
7
+ MultiSessionStore::SubsessionGeneratorMiddleware,
8
+ exclude_paths: app.config.multi_session_store.exclude_paths
9
+ end
10
+
11
+ config.after_initialize do
12
+ ApplicationController.prepend MultiSessionStore::DefaultUrlOptions
13
+ end
14
+ end
15
+ end
@@ -0,0 +1,34 @@
1
+ require 'securerandom'
2
+
3
+ module MultiSessionStore
4
+ class SubsessionGeneratorMiddleware
5
+ def initialize(app, config = {})
6
+ @app = app
7
+ @config = config
8
+ end
9
+
10
+ def call(env)
11
+ request = Rack::Request.new(env)
12
+ request.update_param 'subsession_id', new_subsession_id if subsession_id_is_needed?(request)
13
+ @app.call(env)
14
+ end
15
+
16
+ private
17
+
18
+ def new_subsession_id
19
+ SecureRandom.hex
20
+ end
21
+
22
+ def subsession_id_is_needed?(request)
23
+ !request.params['subsession_id'] && !path_excluded?(request.path)
24
+ end
25
+
26
+ def path_excluded?(current_path)
27
+ excluded_paths.any? { |excluded_path| excluded_path.match? current_path }
28
+ end
29
+
30
+ def excluded_paths
31
+ @config[:exclude_paths] ||= []
32
+ end
33
+ end
34
+ end
@@ -1,3 +1,3 @@
1
1
  module MultiSessionStore
2
- VERSION = "0.1.0"
2
+ VERSION = "0.2.1"
3
3
  end
@@ -31,5 +31,5 @@ Gem::Specification.new do |spec|
31
31
  spec.add_development_dependency "bundler", "~> 2.0"
32
32
  spec.add_development_dependency "rake", "~> 10.0"
33
33
  spec.add_development_dependency "rspec", "~> 3.0"
34
- spec.add_development_dependency "pry"
34
+ spec.add_development_dependency "redis", "~> 4.0"
35
35
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: multi_session_store
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.0
4
+ version: 0.2.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Emarsys Smart Insight developers
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2019-11-14 00:00:00.000000000 Z
11
+ date: 2019-11-18 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: actionpack
@@ -67,19 +67,19 @@ dependencies:
67
67
  - !ruby/object:Gem::Version
68
68
  version: '3.0'
69
69
  - !ruby/object:Gem::Dependency
70
- name: pry
70
+ name: redis
71
71
  requirement: !ruby/object:Gem::Requirement
72
72
  requirements:
73
- - - ">="
73
+ - - "~>"
74
74
  - !ruby/object:Gem::Version
75
- version: '0'
75
+ version: '4.0'
76
76
  type: :development
77
77
  prerelease: false
78
78
  version_requirements: !ruby/object:Gem::Requirement
79
79
  requirements:
80
- - - ">="
80
+ - - "~>"
81
81
  - !ruby/object:Gem::Version
82
- version: '0'
82
+ version: '4.0'
83
83
  description: This gem provides a means to support multiple sessions across different
84
84
  tabs in the same browser window.
85
85
  email:
@@ -98,6 +98,9 @@ files:
98
98
  - bin/console
99
99
  - bin/setup
100
100
  - lib/multi_session_store.rb
101
+ - lib/multi_session_store/default_url_options.rb
102
+ - lib/multi_session_store/railtie.rb
103
+ - lib/multi_session_store/subsession_generator_middleware.rb
101
104
  - lib/multi_session_store/version.rb
102
105
  - multi_session_store.gemspec
103
106
  homepage: https://github.com/emartech/multi_session_store