current_session 0.1.5 → 0.1.6

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: 13ac22af37f6aa5545fb02c46c729473b043b67c4073c9b481015c422c7d5927
4
- data.tar.gz: c2bd6c1da99e69a41ecadf263f2c0219633d45da7cb723476d4fb8904c8344ee
3
+ metadata.gz: fe0094c715e9d1a1152a795e0edd0583434cabcc1d955a8fc5c6e2c2202c0e85
4
+ data.tar.gz: 0d7af31d9cb4f1762c15e6b1a288b2c4c6226dcb153e8709b6e2321ee7007585
5
5
  SHA512:
6
- metadata.gz: 1e193a8e670f0f684dc509193b0d1ada1f092551b312cc3032527520e8c1957c413a2bd77c2637556498b5f898b49974839d4752e1d09792dda68a73e357cc5f
7
- data.tar.gz: 4cd6ca157c873142fc4c43c9a14c2a16e7f8487364d94c116204e7aca36d9b005c5f05d44e80aa3264885363d08e74271d4d442c786d7359fd44c01ee47225d3
6
+ metadata.gz: 41197858ae7dcd61d71a593332ca369ed9526fa0711e3c2b992d2eadaac55df763f800e105d01a67e254043c67ee3673d169be6e5ee889a51797acdfc8b9d962
7
+ data.tar.gz: 6b907dce8ee28afb0a6988cf5384912a732bb56f7ad8f85543cadd02b108c83f15108a6b6ef0593e9c6a86861114ba6aac0e0b053ce2d9cda3bae61469f3acd5
data/Gemfile CHANGED
@@ -9,4 +9,6 @@ gem "rake", "~> 13.0"
9
9
 
10
10
  gem "rspec", "~> 3.0"
11
11
 
12
- gem "rubocop", "~> 1.7"
12
+ gem "rubocop"
13
+ gem "rubocop-performance"
14
+ gem "rubocop-rspec"
data/Gemfile.lock CHANGED
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- current_session (0.1.4)
4
+ current_session (0.1.6)
5
5
  activesupport (>= 6.0.0)
6
6
 
7
7
  GEM
@@ -40,7 +40,7 @@ GEM
40
40
  coderay (~> 1.1)
41
41
  method_source (~> 1.0)
42
42
  rack (3.0.0)
43
- rack-protection (3.0.1)
43
+ rack-protection (3.0.2)
44
44
  rack
45
45
  rainbow (3.1.1)
46
46
  rake (13.0.6)
@@ -59,18 +59,23 @@ GEM
59
59
  diff-lcs (>= 1.2.0, < 2.0)
60
60
  rspec-support (~> 3.11.0)
61
61
  rspec-support (3.11.1)
62
- rubocop (1.36.0)
62
+ rubocop (1.37.0)
63
63
  json (~> 2.3)
64
64
  parallel (~> 1.10)
65
65
  parser (>= 3.1.2.1)
66
66
  rainbow (>= 2.2.2, < 4.0)
67
67
  regexp_parser (>= 1.8, < 3.0)
68
68
  rexml (>= 3.2.5, < 4.0)
69
- rubocop-ast (>= 1.20.1, < 2.0)
69
+ rubocop-ast (>= 1.22.0, < 2.0)
70
70
  ruby-progressbar (~> 1.7)
71
71
  unicode-display_width (>= 1.4.0, < 3.0)
72
- rubocop-ast (1.21.0)
72
+ rubocop-ast (1.23.0)
73
73
  parser (>= 3.1.1.0)
74
+ rubocop-performance (1.15.0)
75
+ rubocop (>= 1.7.0, < 2.0)
76
+ rubocop-ast (>= 0.4.0)
77
+ rubocop-rspec (2.13.2)
78
+ rubocop (~> 1.33)
74
79
  ruby-progressbar (1.11.0)
75
80
  simplecov (0.21.2)
76
81
  docile (~> 1.1)
@@ -78,13 +83,15 @@ GEM
78
83
  simplecov_json_formatter (~> 0.1)
79
84
  simplecov-html (0.12.3)
80
85
  simplecov_json_formatter (0.1.4)
81
- sqlite3 (1.5.1-arm64-darwin)
86
+ sqlite3 (1.5.3-arm64-darwin)
87
+ sqlite3 (1.5.3-x86_64-linux)
82
88
  tzinfo (2.0.5)
83
89
  concurrent-ruby (~> 1.0)
84
90
  unicode-display_width (2.3.0)
85
91
 
86
92
  PLATFORMS
87
93
  arm64-darwin-20
94
+ x86_64-linux
88
95
 
89
96
  DEPENDENCIES
90
97
  activerecord
@@ -94,7 +101,9 @@ DEPENDENCIES
94
101
  pry
95
102
  rake (~> 13.0)
96
103
  rspec (~> 3.0)
97
- rubocop (~> 1.7)
104
+ rubocop
105
+ rubocop-performance
106
+ rubocop-rspec
98
107
  simplecov
99
108
  sqlite3
100
109
 
@@ -7,12 +7,14 @@ module CurrentSession
7
7
  class AuthMethod
8
8
  class_attribute :user_class
9
9
 
10
+ # only exist users
10
11
  class FindBy < self
11
12
  def call(&block)
12
13
  find_by_auth.try(&block)
13
14
  end
14
15
  end
15
16
 
17
+ # admit new users
16
18
  class FindOrCreateBy < self
17
19
  def call(&block)
18
20
  find_or_create_by_auth.try(&block)
@@ -12,9 +12,7 @@ module CurrentSession
12
12
  end
13
13
 
14
14
  def update(request)
15
- session_repository(request).try do |repository|
16
- repository.find { |user| self.current_user = user }
17
- end
15
+ session_repository(request).find_by_token { |user| self.current_user = user }
18
16
  self
19
17
  end
20
18
 
@@ -28,10 +26,7 @@ module CurrentSession
28
26
 
29
27
  def destroy(request)
30
28
  self.current_user = nil
31
- session_repository(request).try do |repository|
32
- repository.destroy
33
- repository.delete_session_token
34
- end
29
+ session_repository(request).delete_session_token
35
30
  end
36
31
 
37
32
  private
@@ -17,30 +17,51 @@ module CurrentSession
17
17
  end
18
18
  attr_reader :current_time, :request, :user_class, :session_token_class
19
19
 
20
- def key
21
- @key ||= CurrentSession.key(user_class)
22
- end
23
-
24
- def session_token
25
- @session_token ||= request.session[key]
26
- end
27
-
28
- def new_session_token
29
- SecureRandom.urlsafe_base64(64)
20
+ def find_by_token(&block)
21
+ try_session_token { find(&block) }
30
22
  end
31
23
 
32
24
  def delete_session_token
33
- request.session.delete(key)
25
+ try_session_token do
26
+ request.session.delete(key)
27
+ destroy
28
+ end
34
29
  end
35
30
 
36
31
  def update_session_token(user)
37
- request.session[key] = create(user)
32
+ create(user) { |value| request.session[key] = value }
38
33
  end
39
34
 
40
- def try
41
- session_token.presence.try do |_|
35
+ protected
36
+
37
+ def try_session_token
38
+ session_token.presence.try do
42
39
  yield self
43
40
  end
44
41
  end
42
+
43
+ def find
44
+ fail NotImplementedError, "You must implement #{self.class}##{__method__}"
45
+ end
46
+
47
+ def create(user, &block)
48
+ fail NotImplementedError, "You must implement #{self.class}##{__method__}"
49
+ end
50
+
51
+ def destroy
52
+ fail NotImplementedError, "You must implement #{self.class}##{__method__}"
53
+ end
54
+
55
+ def key
56
+ @key ||= CurrentSession.key(user_class)
57
+ end
58
+
59
+ def session_token
60
+ @session_token ||= request.session[key]
61
+ end
62
+
63
+ def new_session_token
64
+ SecureRandom.urlsafe_base64(64)
65
+ end
45
66
  end
46
67
  end
@@ -16,9 +16,16 @@ module CurrentSession
16
16
  def create(user)
17
17
  session_token_class.create(user_id: user.id, value: new_session_token) do |record|
18
18
  update(record)
19
- end.value
19
+ yield record.value
20
+ end
21
+ end
22
+
23
+ def destroy
24
+ session_token_class.find_by(value: session_token)&.destroy
20
25
  end
21
26
 
27
+ private
28
+
22
29
  def update(token)
23
30
  token.update(
24
31
  last_request_at: current_time,
@@ -26,10 +33,6 @@ module CurrentSession
26
33
  last_request_user_agent: request.user_agent
27
34
  )
28
35
  end
29
-
30
- def destroy
31
- session_token_class.find_by(value: session_token)&.destroy
32
- end
33
36
  end
34
37
  end
35
38
  end
@@ -11,7 +11,7 @@ module CurrentSession
11
11
  Module.new do
12
12
  define_method(:current_user) { user_class.find(current_user_id) }
13
13
 
14
- def try
14
+ def try_session_token
15
15
  yield self
16
16
  end
17
17
 
@@ -11,7 +11,7 @@ module CurrentSession
11
11
  end
12
12
 
13
13
  def create(user)
14
- user.uid
14
+ yield user.uid
15
15
  end
16
16
 
17
17
  def destroy
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module CurrentSession
4
- VERSION = "0.1.5"
4
+ VERSION = "0.1.6"
5
5
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: current_session
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.5
4
+ version: 0.1.6
5
5
  platform: ruby
6
6
  authors:
7
7
  - Masa (Aileron inc)
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2022-10-21 00:00:00.000000000 Z
11
+ date: 2022-10-23 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: activesupport