bacchus 0.0.1 → 0.0.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- data/lib/bacchus/rack/beta_site.rb +79 -81
- data/lib/bacchus/version.rb +1 -1
- data/spec/bacchus/rack/beta_site_spec.rb +38 -0
- metadata +3 -3
- data/spec/lib/bacchus/rack/beta_site.rb +0 -87
@@ -1,90 +1,88 @@
|
|
1
1
|
require 'letters'
|
2
2
|
|
3
|
-
module
|
4
|
-
|
5
|
-
|
6
|
-
|
3
|
+
module Rack
|
4
|
+
class BetaSite
|
5
|
+
attr_reader :options
|
6
|
+
|
7
|
+
def initialize(app, options = {})
|
8
|
+
@app = app
|
9
|
+
@excluded_routes = options[:except] || []
|
10
|
+
@access_form_routes = options[:access_through] || []
|
11
|
+
@server = options[:server] or raise "Need to specify the address of the server."
|
12
|
+
end
|
13
|
+
|
14
|
+
def call(env)
|
15
|
+
# if (Request.new(env).path == "/live_demo/unlock")
|
16
|
+
# msg = "Site has been unlocked"
|
17
|
+
# response = Response.new("Site has been unlocked", 200, {'Content-Type' => 'text/html'})
|
18
|
+
# response.set_cookie("unlocked", "true")
|
19
|
+
# return response.to_a
|
20
|
+
# end
|
21
|
+
|
22
|
+
if access_form_route?(env)
|
23
|
+
render_access_form
|
24
|
+
elsif access_denied?(env)
|
25
|
+
render_locked
|
26
|
+
else
|
27
|
+
@app.call(env)
|
28
|
+
end
|
29
|
+
end
|
7
30
|
|
8
|
-
|
9
|
-
@app = app
|
10
|
-
@excluded_routes = options[:except] || []
|
11
|
-
@access_form_routes = options[:access_through] || []
|
12
|
-
@server = options[:server] or raise "Need to specify the address of the server."
|
13
|
-
end
|
31
|
+
private
|
14
32
|
|
15
|
-
|
16
|
-
|
17
|
-
|
18
|
-
|
19
|
-
|
20
|
-
|
21
|
-
|
22
|
-
|
23
|
-
|
24
|
-
|
25
|
-
|
26
|
-
|
27
|
-
|
28
|
-
|
29
|
-
|
30
|
-
|
31
|
-
|
32
|
-
|
33
|
-
|
34
|
-
def access_form_route?(env) # TODO: Duplication -- access_denied? & excluded_route?
|
35
|
-
request = Request.new(env)
|
36
|
-
@access_form_routes.index {|route| matching_path?(request, route)}
|
37
|
-
end
|
38
|
-
|
39
|
-
def render_access_form
|
40
|
-
body = <<-EOS
|
41
|
-
<html>
|
42
|
-
<head>
|
43
|
-
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js" type="text/javascript"></script>
|
44
|
-
<script src="#{path_to('bacchus.js')}" type="text/javascript"></script>
|
45
|
-
</head>
|
46
|
-
<body>
|
47
|
-
</body>
|
48
|
-
</html>
|
49
|
-
EOS
|
50
|
-
[200, {'Content-Type' => 'text/html', 'Content-Length' => body.length.to_s}, [body]]
|
51
|
-
end
|
33
|
+
def access_form_route?(env) # TODO: Duplication -- access_denied? & excluded_route?
|
34
|
+
request = Request.new(env)
|
35
|
+
@access_form_routes.index {|route| matching_path?(request, route)}
|
36
|
+
end
|
37
|
+
|
38
|
+
def render_access_form
|
39
|
+
body = <<-EOS
|
40
|
+
<html>
|
41
|
+
<head>
|
42
|
+
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js" type="text/javascript"></script>
|
43
|
+
<script src="#{path_to('bacchus.js')}" type="text/javascript"></script>
|
44
|
+
</head>
|
45
|
+
<body>
|
46
|
+
</body>
|
47
|
+
</html>
|
48
|
+
EOS
|
49
|
+
[200, {'Content-Type' => 'text/html', 'Content-Length' => body.length.to_s}, [body]]
|
50
|
+
end
|
52
51
|
|
53
|
-
|
54
|
-
|
55
|
-
|
56
|
-
|
57
|
-
|
58
|
-
|
59
|
-
|
60
|
-
|
61
|
-
|
62
|
-
|
63
|
-
|
64
|
-
|
52
|
+
########################################################
|
53
|
+
|
54
|
+
def path_to(file)
|
55
|
+
::File.join(@server, file)
|
56
|
+
end
|
57
|
+
|
58
|
+
########################################################
|
59
|
+
|
60
|
+
def render_locked
|
61
|
+
msg = "Site is locked"
|
62
|
+
[500, {'Content-Type' => 'text/html', 'Content-Length' => msg.length.to_s}, [msg]]
|
63
|
+
end
|
65
64
|
|
66
|
-
|
67
|
-
|
68
|
-
|
69
|
-
|
70
|
-
|
71
|
-
|
72
|
-
|
73
|
-
|
74
|
-
|
75
|
-
|
76
|
-
|
77
|
-
|
78
|
-
|
79
|
-
|
80
|
-
|
81
|
-
|
82
|
-
|
83
|
-
|
84
|
-
|
85
|
-
|
86
|
-
end
|
65
|
+
def access_denied?(env)
|
66
|
+
request = Request.new(env)
|
67
|
+
locked?(request) && !excluded_route?(request)
|
68
|
+
end
|
69
|
+
|
70
|
+
def locked?(request)
|
71
|
+
request.cookies["unlocked"].nil?
|
72
|
+
end
|
73
|
+
|
74
|
+
def excluded_route?(request)
|
75
|
+
@excluded_routes.index {|route| matching_path?(request, route)}
|
76
|
+
end
|
77
|
+
|
78
|
+
def matching_path?(request, path)
|
79
|
+
if path.is_a?(String)
|
80
|
+
request.fullpath == path
|
81
|
+
elsif path.is_a?(Regexp)
|
82
|
+
request.fullpath.match(path)
|
83
|
+
else
|
84
|
+
raise "Unsupported route type; only String or Regex paths are accepted"
|
87
85
|
end
|
88
86
|
end
|
89
87
|
end
|
90
|
-
end
|
88
|
+
end
|
data/lib/bacchus/version.rb
CHANGED
@@ -0,0 +1,38 @@
|
|
1
|
+
require 'rack/mock'
|
2
|
+
require_relative '../../../lib/bacchus/rack/beta_site'
|
3
|
+
|
4
|
+
describe Rack::BetaSite do
|
5
|
+
let(:app) { lambda { |env| [200, { 'Content-Type' => 'text/plain' }, ['hello']] } }
|
6
|
+
let(:middleware) { Rack::BetaSite.new(app, except: ["/", %r{^/open/?$}], access_through: ["/form"], server: "localhost")}
|
7
|
+
|
8
|
+
def response_for(sample_app, path)
|
9
|
+
sample_app.call(Rack::MockRequest.env_for(path))
|
10
|
+
end
|
11
|
+
|
12
|
+
context "given a locked site" do
|
13
|
+
it "should block access to all pages" do
|
14
|
+
sample_app = middleware
|
15
|
+
status, * = response_for(middleware, "/page1")
|
16
|
+
status.should == 500
|
17
|
+
end
|
18
|
+
|
19
|
+
it "should support string exceptions" do
|
20
|
+
sample_app = middleware
|
21
|
+
status, * = response_for(middleware, "/")
|
22
|
+
status.should == 200
|
23
|
+
end
|
24
|
+
|
25
|
+
it "should support regex exceptions" do
|
26
|
+
sample_app = middleware
|
27
|
+
status, * = response_for(middleware, "/open")
|
28
|
+
status.should == 200
|
29
|
+
end
|
30
|
+
|
31
|
+
it "should render access form script instead of the body" do
|
32
|
+
sample_app = middleware
|
33
|
+
status, _, body = response_for(middleware, "/form")
|
34
|
+
status.should == 200
|
35
|
+
body.first.should include("<script")
|
36
|
+
end
|
37
|
+
end
|
38
|
+
end
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: bacchus
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0.
|
4
|
+
version: 0.0.2
|
5
5
|
prerelease:
|
6
6
|
platform: ruby
|
7
7
|
authors:
|
@@ -23,7 +23,7 @@ files:
|
|
23
23
|
- lib/bacchus/rack/beta_site.rb
|
24
24
|
- lib/bacchus/version.rb
|
25
25
|
- lib/bacchus.rb
|
26
|
-
- spec/
|
26
|
+
- spec/bacchus/rack/beta_site_spec.rb
|
27
27
|
homepage: ''
|
28
28
|
licenses: []
|
29
29
|
post_install_message:
|
@@ -49,4 +49,4 @@ signing_key:
|
|
49
49
|
specification_version: 3
|
50
50
|
summary: Beta invites for Rack-based apps.
|
51
51
|
test_files:
|
52
|
-
- spec/
|
52
|
+
- spec/bacchus/rack/beta_site_spec.rb
|
@@ -1,87 +0,0 @@
|
|
1
|
-
require 'letters'
|
2
|
-
module Rack
|
3
|
-
class BetaSite
|
4
|
-
attr_reader :options
|
5
|
-
|
6
|
-
def initialize(app, options = {})
|
7
|
-
@app = app
|
8
|
-
@excluded_routes = options[:except] || []
|
9
|
-
@access_form_routes = options[:access_through] || []
|
10
|
-
@server = options[:server] or raise "Need to specify the address of the server."
|
11
|
-
end
|
12
|
-
|
13
|
-
def call(env)
|
14
|
-
# if (Request.new(env).path == "/live_demo/unlock")
|
15
|
-
# msg = "Site has been unlocked"
|
16
|
-
# response = Response.new("Site has been unlocked", 200, {'Content-Type' => 'text/html'})
|
17
|
-
# response.set_cookie("unlocked", "true")
|
18
|
-
# return response.to_a
|
19
|
-
# end
|
20
|
-
|
21
|
-
if access_form_route?(env)
|
22
|
-
render_access_form
|
23
|
-
elsif access_denied?(env)
|
24
|
-
render_locked
|
25
|
-
else
|
26
|
-
@app.call(env)
|
27
|
-
end
|
28
|
-
end
|
29
|
-
|
30
|
-
private
|
31
|
-
|
32
|
-
def access_form_route?(env) # TODO: Duplication -- access_denied? & excluded_route?
|
33
|
-
request = Request.new(env)
|
34
|
-
@access_form_routes.index {|route| matching_path?(request, route)}
|
35
|
-
end
|
36
|
-
|
37
|
-
def render_access_form
|
38
|
-
body = <<-EOS
|
39
|
-
<html>
|
40
|
-
<head>
|
41
|
-
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js" type="text/javascript"></script>
|
42
|
-
<script src="#{path_to('bacchus.js')}" type="text/javascript"></script>
|
43
|
-
</head>
|
44
|
-
<body>
|
45
|
-
</body>
|
46
|
-
</html>
|
47
|
-
EOS
|
48
|
-
[200, {'Content-Type' => 'text/html', 'Content-Length' => body.length.to_s}, [body]]
|
49
|
-
end
|
50
|
-
|
51
|
-
########################################################
|
52
|
-
|
53
|
-
def path_to(file)
|
54
|
-
::File.join(@server, file)
|
55
|
-
end
|
56
|
-
|
57
|
-
########################################################
|
58
|
-
|
59
|
-
def render_locked
|
60
|
-
msg = "Site is locked"
|
61
|
-
[500, {'Content-Type' => 'text/html', 'Content-Length' => msg.length.to_s}, [msg]]
|
62
|
-
end
|
63
|
-
|
64
|
-
def access_denied?(env)
|
65
|
-
request = Request.new(env)
|
66
|
-
locked?(request) && !excluded_route?(request)
|
67
|
-
end
|
68
|
-
|
69
|
-
def locked?(request)
|
70
|
-
request.cookies["unlocked"].nil?
|
71
|
-
end
|
72
|
-
|
73
|
-
def excluded_route?(request)
|
74
|
-
@excluded_routes.index {|route| matching_path?(request, route)}
|
75
|
-
end
|
76
|
-
|
77
|
-
def matching_path?(request, path)
|
78
|
-
if path.is_a?(String)
|
79
|
-
request.fullpath == path
|
80
|
-
elsif path.is_a?(Regexp)
|
81
|
-
request.fullpath.match(path)
|
82
|
-
else
|
83
|
-
raise "Unsupported route type; only String or Regex paths are accepted"
|
84
|
-
end
|
85
|
-
end
|
86
|
-
end
|
87
|
-
end
|