jj-middleware 1.0.0 → 2.0.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: 9ddac0f6c59a12d3d877ef45218ffbed2cbef94d
4
- data.tar.gz: 7183a18286a9c1a71a5ab68394e922668f2a3240
3
+ metadata.gz: e972d093a7be26e5746d753ca4df5efbd450044f
4
+ data.tar.gz: cc02197f544e446abda319824afddff3b9d66952
5
5
  SHA512:
6
- metadata.gz: 56085af19906726369333841eeef7f281db07c819efbd1bb602d1076a8f58310151314c930302c619ba8bfe45d6a77f7d5b05c66f967ca5c1a8a4412f0c34b41
7
- data.tar.gz: b0836c282e3102fe087ec1968d37584627384bdd51396559fb24b4fd2f40a1b8386db9b3cb7bff06b2566e0670f5b40a1bfd0635c1fad51a14a37d32dcb29bda
6
+ metadata.gz: 35b16bc35278be173cb01e96637a438c74a94814e14f28d3e6221a1516f1aa57dc9268fb518707767741c737e0046091009e6d977037ef155d59e6b0a3221d59
7
+ data.tar.gz: 467590c24da14c2643c4547592855f3c5d42999620a11920cccb482ca7937e502b6fee8e69d27f18528c553b60885ebbb3a003053d4f6f3f36eca6f146cdea3f
@@ -19,6 +19,7 @@ Gem::Specification.new do |spec|
19
19
 
20
20
  spec.add_development_dependency "bundler", "~> 1.9"
21
21
  spec.add_development_dependency "pry"
22
+ spec.add_development_dependency "rack", "~> 1.5"
22
23
  spec.add_development_dependency "rake", "~> 10.0"
23
24
  spec.add_development_dependency "rspec", "~> 3.0"
24
25
 
data/lib/jj/middleware.rb CHANGED
@@ -1,5 +1,5 @@
1
1
  module JJ
2
2
  module Middleware
3
- VERSION = "1.0.0".freeze
3
+ VERSION = "2.0.0".freeze
4
4
  end
5
5
  end
@@ -9,31 +9,37 @@ module JJ
9
9
  end
10
10
 
11
11
  def call(env)
12
- sess = Session.new(env)
13
- logged_in?(sess) ? @app.call(env) : redirect("/login")
12
+ session = Session.new(env)
13
+ logged_in?(session) ? @app.call(env) : redirect("/login")
14
14
  end
15
15
 
16
16
  private
17
17
 
18
- def logged_in?(sess)
19
- unless sess.exist?
20
- log.warn("[JJ::Auth] Session does not exist.")
18
+ def logged_in?(session)
19
+ unless session.exist?
20
+ warn("Session does not exist")
21
21
  return false
22
22
  end
23
23
 
24
- user = sess.fetch("user")
24
+ user = session.fetch("user")
25
25
  if user.nil?
26
- log.warn("[JJ::Auth] User key doesn't exist in session.")
26
+ warn("User key doesn't exist in session")
27
27
  return false
28
28
  end
29
29
 
30
30
  unless user.id && user.email
31
- log.warn("[JJ::Auth] User ID/Email doesn't exist in session.")
31
+ warn("User ID/Email doesn't exist in session")
32
32
  return false
33
33
  end
34
34
 
35
35
  true
36
36
  end
37
+
38
+ def warn(msg)
39
+ log.warn(
40
+ format("[JJ::Auth] %s.", msg)
41
+ )
42
+ end
37
43
  end
38
44
  end
39
45
  end
@@ -6,12 +6,27 @@ module JJ
6
6
  protected
7
7
 
8
8
  def log
9
- @log ||= ::Logger.new STDOUT
9
+ logger = test? ? ::Logger.new("/dev/null") : ::Logger.new(STDOUT)
10
+ logger.formatter = logging_formatter
11
+ @log ||= logger
10
12
  end
11
13
 
12
14
  def redirect(path, code = 302)
13
15
  [code, { "Location" => path, "Content-Type" => "text/html" }, []]
14
16
  end
17
+
18
+ private
19
+
20
+ def logging_formatter
21
+ proc do |severity, datetime, _, msg|
22
+ time = datetime.strftime("%Y/%m/%d %H:%M:%S")
23
+ format("%s %s - %s\n", severity, time, msg)
24
+ end
25
+ end
26
+
27
+ def test?
28
+ ENV["APP_ENV"] == "test"
29
+ end
15
30
  end
16
31
  end
17
32
  end
@@ -10,13 +10,13 @@ module JJ
10
10
  end
11
11
 
12
12
  def call(env)
13
- sess = Session.new(env)
14
- user = sess.fetch("user")
15
-
13
+ session = Session.new(env)
14
+ user = session.fetch("user")
15
+
16
16
  unless user_exist?(user)
17
17
  log.info("[JJ::Onboarding] User record not yet created.")
18
18
  record = create_user!(user.id)
19
- sess.set_model(record)
19
+ session.update_user_model(record)
20
20
  return redirect("/onboarding")
21
21
  end
22
22
 
@@ -1,22 +1,33 @@
1
+ require "rack/request"
2
+
1
3
  module JJ
2
4
  module Middleware
3
5
  class Session
4
6
  def initialize(env)
5
- @env = env
7
+ raise(ArgumentError, "'env' must be Hash") unless env.is_a?(Hash)
8
+ @request = Rack::Request.new(env)
6
9
  end
7
10
 
8
11
  def exist?
9
- @env.key?("rack.session")
12
+ !@request.session.nil?
10
13
  end
11
14
 
12
15
  def fetch(key = nil)
13
- @env["rack.session"].to_hash.tap do |s|
16
+ validate_fetch_key!(key)
17
+ @request.session.tap do |s|
14
18
  return s[key] unless key.nil?
15
19
  end
16
20
  end
17
21
 
18
- def set_model(record)
19
- @env["rack.session"].to_hash["user"].model = record
22
+ def update_user_model(record)
23
+ fetch("user").model = record
24
+ end
25
+
26
+ private
27
+
28
+ def validate_fetch_key!(k)
29
+ return if k.nil? || k.is_a?(String)
30
+ raise(ArgumentError, "'key' must be String/nil")
20
31
  end
21
32
  end
22
33
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: jj-middleware
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.0.0
4
+ version: 2.0.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - ''
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2016-06-03 00:00:00.000000000 Z
11
+ date: 2016-06-14 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler
@@ -38,6 +38,20 @@ dependencies:
38
38
  - - ">="
39
39
  - !ruby/object:Gem::Version
40
40
  version: '0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: rack
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: '1.5'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '1.5'
41
55
  - !ruby/object:Gem::Dependency
42
56
  name: rake
43
57
  requirement: !ruby/object:Gem::Requirement