bacchus 0.0.6 → 0.0.8

Sign up to get free protection for your applications and to get access to all the features.
@@ -1,25 +1,36 @@
1
+ require 'rack/request'
1
2
  module Rack
2
3
  class BetaSite
4
+
5
+ class Request < Rack::Request
6
+ def path=(new_path)
7
+ @new_path = new_path
8
+ end
9
+
10
+ def path
11
+ @new_path || super
12
+ end
13
+ end
14
+
15
+
3
16
  attr_reader :options
4
17
 
5
18
  def initialize(app, options = {})
6
19
  @app = app
7
20
  @excluded_routes = options[:except] || []
8
21
  @access_form_routes = options[:access_through] || []
9
- @server = options[:server] or raise "Need to specify the address of the server."
22
+ @access_template_route = options[:access_template] or raise "Specify the route to the template for the access form."
23
+ @server = options[:server] or raise "Specify the address of the server."
10
24
  end
11
25
 
12
26
  def call(env)
13
- # if (Request.new(env).path == "/live_demo/unlock")
14
- # msg = "Site has been unlocked"
15
- # response = Response.new("Site has been unlocked", 200, {'Content-Type' => 'text/html'})
16
- # response.set_cookie("unlocked", "true")
17
- # return response.to_a
18
- # end
19
-
20
- if access_form_route?(env)
21
- render_access_form
22
- elsif access_denied?(env)
27
+ request = Request.new(env)
28
+ if access_form_route?(request)
29
+ request.path = @access_template_route
30
+ env["PATH_INFO"] = request.path
31
+ env["REQUEST_URI"] = request.fullpath
32
+ render_with_bacchus(env)
33
+ elsif access_denied?(request)
23
34
  render_locked
24
35
  else
25
36
  @app.call(env)
@@ -28,25 +39,17 @@ module Rack
28
39
 
29
40
  private
30
41
 
31
- def access_form_route?(env) # TODO: Duplication -- access_denied? & excluded_route?
32
- request = Request.new(env)
42
+ def access_form_route?(request) # TODO: Duplication -- access_denied? & excluded_route?
33
43
  @access_form_routes.index {|route| matching_path?(request, route)}
34
44
  end
35
45
 
36
- def render_access_form
37
- body = <<-EOS
38
- <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
39
- <html xmlns="http://www.w3.org/1999/xhtml">
40
- <head>
41
- <title>Peer-based website rating service - Criticue.com</title>
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>
46
+ def render_with_bacchus(env)
47
+ status, headers, body = @app.call(env)
48
+ script = <<-EOS
49
+ <script src="#{path_to('bacchus.js')}" type="text/javascript"></script>
48
50
  EOS
49
- [200, {'Content-Type' => 'text/html', 'Content-Length' => body.length.to_s}, [body]]
51
+ body.first.gsub!(/(<\/body>)/i, "\\1\n" + script) or raise "Rack::BetaSite error: No closing </body> tag detected."
52
+ [status, headers.merge('Content-Length' => body.first.size.to_s), body]
50
53
  end
51
54
 
52
55
  ########################################################
@@ -56,19 +59,18 @@ module Rack
56
59
  end
57
60
 
58
61
  ########################################################
59
-
62
+
60
63
  def render_locked
61
64
  msg = "Site is locked"
62
65
  [500, {'Content-Type' => 'text/html', 'Content-Length' => msg.length.to_s}, [msg]]
63
66
  end
64
67
 
65
- def access_denied?(env)
66
- request = Request.new(env)
68
+ def access_denied?(request)
67
69
  locked?(request) && !excluded_route?(request)
68
70
  end
69
71
 
70
72
  def locked?(request)
71
- request.cookies["unlocked"].nil?
73
+ true
72
74
  end
73
75
 
74
76
  def excluded_route?(request)
@@ -77,9 +79,9 @@ module Rack
77
79
 
78
80
  def matching_path?(request, path)
79
81
  if path.is_a?(String)
80
- request.fullpath == path
82
+ request.path_info == path
81
83
  elsif path.is_a?(Regexp)
82
- request.fullpath.match(path)
84
+ request.path_info.match(path)
83
85
  else
84
86
  raise "Unsupported route type; only String or Regex paths are accepted"
85
87
  end
@@ -1,3 +1,3 @@
1
1
  module Bacchus
2
- VERSION = "0.0.6"
2
+ VERSION = "0.0.8"
3
3
  end
@@ -2,8 +2,10 @@ require 'rack/mock'
2
2
  require_relative '../../../lib/bacchus/rack/beta_site'
3
3
 
4
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")}
5
+ let(:app) { lambda { |env| app_response } }
6
+ let(:app_response) { [200, { 'Content-Type' => 'text/plain' }, ['<body>hello</body>']] }
7
+ let(:access_template_route) { "/access_template" }
8
+ let(:middleware) { Rack::BetaSite.new(app, except: ["/", %r{^/open/?$}], access_through: ["/form"], server: "localhost", access_template: access_template_route) }
7
9
 
8
10
  def response_for(sample_app, path)
9
11
  sample_app.call(Rack::MockRequest.env_for(path))
@@ -28,11 +30,18 @@ describe Rack::BetaSite do
28
30
  status.should == 200
29
31
  end
30
32
 
31
- it "should render access form script instead of the body" do
33
+ it "should render access form script after the body closing tag" do
32
34
  sample_app = middleware
33
35
  status, _, body = response_for(middleware, "/form")
34
36
  status.should == 200
35
- body.first.should include("<script")
37
+ body.first.should include("hello")
38
+ body.first.should include("</body>\n<script")
39
+ end
40
+
41
+ it "should redirect to access template" do
42
+ sample_app = middleware
43
+ app.should_receive(:call).with(hash_including("PATH_INFO" => "/access_template")).and_return(app_response)
44
+ response_for(middleware, "/form")
36
45
  end
37
46
  end
38
47
  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.6
4
+ version: 0.0.8
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2012-11-02 00:00:00.000000000 Z
12
+ date: 2012-11-03 00:00:00.000000000 Z
13
13
  dependencies: []
14
14
  description: Adds support for private beta invites for Rack-based apps.
15
15
  email: