jj-middleware 0.1.0 → 0.2.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: b41b197664e0fd6b79b8ff386ab11cf947ee358e
4
- data.tar.gz: 05591a4106897935b6e1d008ccc149cec7c1ba5d
3
+ metadata.gz: b0c8817a2816a94bcd4485e8eb2567487861ce42
4
+ data.tar.gz: 653ff3a437c77d6d042972f3759cea3f8cd3ab61
5
5
  SHA512:
6
- metadata.gz: 2549bc8ba1cfea0f9e45ca7f54aa90b4a461cc8627c49ebd7ad201c263af46579d194d48048e4f1eefe62bd0fda0fb8fd51d6b9fb15d50623e27e5e9487f1e27
7
- data.tar.gz: db971e99686c62ef3bc93ac1c52fb437110b1642a32f69d3f791798e1b03c7df30c4c5c6e01d70812fd02e4ac62aac96a786b019e9665692621c3fa8b9719af2
6
+ metadata.gz: b1ee907a2e37923a147804fae15a720628bd38bbb7c8a9f95ed8ed5b76b4add1d8b112b33c272a6c99b1b01c7291ccb812254c4e343556b2a37da8b68d074e6a
7
+ data.tar.gz: 873935f4e6e40a7efb442f861bc9e7ed4c39ddccd015367224a2083d52bc3012fe14cf6f66a7aea4b5d72fe95076b15dd4f9752029fd769be04ddce4d228fe03
@@ -23,4 +23,5 @@ Gem::Specification.new do |spec|
23
23
  spec.add_development_dependency "rspec", "~> 3.0"
24
24
 
25
25
  spec.add_dependency "json"
26
+ spec.add_dependency "wes-data-api", "~> 5"
26
27
  end
@@ -1,4 +1,5 @@
1
1
  require "jj/middleware/base"
2
+ require "jj/middleware/session"
2
3
 
3
4
  module JJ
4
5
  module Middleware
@@ -8,18 +9,16 @@ module JJ
8
9
  end
9
10
 
10
11
  def call(env)
11
- logged_in?(env) ? @app.call(env) : redirect
12
+ sess = Session.new(env)
13
+ logged_in?(sess) ? @app.call(env) : redirect("/login")
12
14
  end
13
15
 
14
16
  private
15
17
 
16
- def logged_in?(env)
17
- s = session(env)
18
- s && s["user"] && s["user"].email && s["user"].data_api
19
- end
20
-
21
- def redirect
22
- [302, { "Location" => "/login", "Content-Type" => "text/html" }, []]
18
+ def logged_in?(sess)
19
+ return false unless sess.exist?
20
+ user = sess.fetch("user")
21
+ user && user.id && user.email && user.data_api
23
22
  end
24
23
  end
25
24
  end
@@ -3,8 +3,8 @@ module JJ
3
3
  class Base
4
4
  protected
5
5
 
6
- def session(env)
7
- env["rack.session"].to_hash
6
+ def redirect(path, code = 302)
7
+ [code, { "Location" => path, "Content-Type" => "text/html" }, []]
8
8
  end
9
9
  end
10
10
  end
@@ -0,0 +1,42 @@
1
+ require "jj/middleware/base"
2
+ require "jj/middleware/session"
3
+ require "wes/data/api/creator_user"
4
+
5
+ module JJ
6
+ module Middleware
7
+ class Onboarding < Base
8
+ def initialize(app)
9
+ @app = app
10
+ end
11
+
12
+ def call(env)
13
+ sess = Session.new(env).fetch("user")
14
+
15
+ if sess.nil?
16
+ return redirect("/login")
17
+ end
18
+
19
+ unless user_exist?(sess.id)
20
+ create_user!(sess.id)
21
+ return redirect("/onboarding")
22
+ end
23
+
24
+ complete = sess.data_api["onboarding_complete"]
25
+ return redirect("/onboarding") unless complete
26
+
27
+ @app.call(env)
28
+ end
29
+
30
+ private
31
+
32
+ def create_user!(user_id)
33
+ Wes::Data::API::CreatorUser.create(:auth0_id => user_id)
34
+ end
35
+
36
+ def user_exist?(user_id)
37
+ user = Wes::Data::API::CreatorUser.find(:id, user_id)
38
+ user && (user.auth0_id == user_id)
39
+ end
40
+ end
41
+ end
42
+ end
@@ -0,0 +1,19 @@
1
+ module JJ
2
+ module Middleware
3
+ class Session
4
+ def initialize(env)
5
+ @env = env
6
+ end
7
+
8
+ def exist?
9
+ @env.key?("rack.session")
10
+ end
11
+
12
+ def fetch(key = nil)
13
+ @env["rack.session"].to_hash.tap do |s|
14
+ return s[key] unless key.nil?
15
+ end
16
+ end
17
+ end
18
+ end
19
+ end
data/lib/jj/middleware.rb CHANGED
@@ -1,5 +1,5 @@
1
1
  module JJ
2
2
  module Middleware
3
- VERSION = "0.1.0".freeze
3
+ VERSION = "0.2.0".freeze
4
4
  end
5
5
  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: 0.1.0
4
+ version: 0.2.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - ''
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2016-05-17 00:00:00.000000000 Z
11
+ date: 2016-05-18 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler
@@ -80,6 +80,20 @@ dependencies:
80
80
  - - ">="
81
81
  - !ruby/object:Gem::Version
82
82
  version: '0'
83
+ - !ruby/object:Gem::Dependency
84
+ name: wes-data-api
85
+ requirement: !ruby/object:Gem::Requirement
86
+ requirements:
87
+ - - "~>"
88
+ - !ruby/object:Gem::Version
89
+ version: '5'
90
+ type: :runtime
91
+ prerelease: false
92
+ version_requirements: !ruby/object:Gem::Requirement
93
+ requirements:
94
+ - - "~>"
95
+ - !ruby/object:Gem::Version
96
+ version: '5'
83
97
  description:
84
98
  email:
85
99
  - ''
@@ -92,6 +106,8 @@ files:
92
106
  - lib/jj/middleware.rb
93
107
  - lib/jj/middleware/auth.rb
94
108
  - lib/jj/middleware/base.rb
109
+ - lib/jj/middleware/onboarding.rb
110
+ - lib/jj/middleware/session.rb
95
111
  homepage: ''
96
112
  licenses: []
97
113
  metadata: {}