bacchus 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
data/LICENSE.txt ADDED
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2012 Martin Bilski
2
+
3
+ MIT License
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining
6
+ a copy of this software and associated documentation files (the
7
+ "Software"), to deal in the Software without restriction, including
8
+ without limitation the rights to use, copy, modify, merge, publish,
9
+ distribute, sublicense, and/or sell copies of the Software, and to
10
+ permit persons to whom the Software is furnished to do so, subject to
11
+ the following conditions:
12
+
13
+ The above copyright notice and this permission notice shall be
14
+ included in all copies or substantial portions of the Software.
15
+
16
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,29 @@
1
+ # Bacchus
2
+
3
+ TODO: Write a gem description
4
+
5
+ ## Installation
6
+
7
+ Add this line to your application's Gemfile:
8
+
9
+ gem 'bacchus'
10
+
11
+ And then execute:
12
+
13
+ $ bundle
14
+
15
+ Or install it yourself as:
16
+
17
+ $ gem install bacchus
18
+
19
+ ## Usage
20
+
21
+ TODO: Write usage instructions here
22
+
23
+ ## Contributing
24
+
25
+ 1. Fork it
26
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
27
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
28
+ 4. Push to the branch (`git push origin my-new-feature`)
29
+ 5. Create new Pull Request
data/lib/bacchus.rb ADDED
@@ -0,0 +1,2 @@
1
+ require "bacchus/version"
2
+ require "bacchus/rack/beta_site"
@@ -0,0 +1,90 @@
1
+ require 'letters'
2
+
3
+ module Bacchus
4
+ module Rack
5
+ class BetaSite
6
+ attr_reader :options
7
+
8
+ def initialize(app, options = {})
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
14
+
15
+ def call(env)
16
+ # if (Request.new(env).path == "/live_demo/unlock")
17
+ # msg = "Site has been unlocked"
18
+ # response = Response.new("Site has been unlocked", 200, {'Content-Type' => 'text/html'})
19
+ # response.set_cookie("unlocked", "true")
20
+ # return response.to_a
21
+ # end
22
+
23
+ if access_form_route?(env)
24
+ render_access_form
25
+ elsif access_denied?(env)
26
+ render_locked
27
+ else
28
+ @app.call(env)
29
+ end
30
+ end
31
+
32
+ private
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
52
+
53
+ ########################################################
54
+
55
+ def path_to(file)
56
+ ::File.join(@server, file)
57
+ end
58
+
59
+ ########################################################
60
+
61
+ def render_locked
62
+ msg = "Site is locked"
63
+ [500, {'Content-Type' => 'text/html', 'Content-Length' => msg.length.to_s}, [msg]]
64
+ end
65
+
66
+ def access_denied?(env)
67
+ request = Request.new(env)
68
+ locked?(request) && !excluded_route?(request)
69
+ end
70
+
71
+ def locked?(request)
72
+ request.cookies["unlocked"].nil?
73
+ end
74
+
75
+ def excluded_route?(request)
76
+ @excluded_routes.index {|route| matching_path?(request, route)}
77
+ end
78
+
79
+ def matching_path?(request, path)
80
+ if path.is_a?(String)
81
+ request.fullpath == path
82
+ elsif path.is_a?(Regexp)
83
+ request.fullpath.match(path)
84
+ else
85
+ raise "Unsupported route type; only String or Regex paths are accepted"
86
+ end
87
+ end
88
+ end
89
+ end
90
+ end
@@ -0,0 +1,3 @@
1
+ module Bacchus
2
+ VERSION = "0.0.1"
3
+ end
@@ -0,0 +1,87 @@
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
metadata ADDED
@@ -0,0 +1,52 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: bacchus
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - Martin Bilski
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2012-10-30 00:00:00.000000000 Z
13
+ dependencies: []
14
+ description: Adds support for private beta invites for Rack-based apps.
15
+ email:
16
+ - gyamtso@gmail.com
17
+ executables: []
18
+ extensions: []
19
+ extra_rdoc_files: []
20
+ files:
21
+ - README.md
22
+ - LICENSE.txt
23
+ - lib/bacchus/rack/beta_site.rb
24
+ - lib/bacchus/version.rb
25
+ - lib/bacchus.rb
26
+ - spec/lib/bacchus/rack/beta_site.rb
27
+ homepage: ''
28
+ licenses: []
29
+ post_install_message:
30
+ rdoc_options: []
31
+ require_paths:
32
+ - lib
33
+ required_ruby_version: !ruby/object:Gem::Requirement
34
+ none: false
35
+ requirements:
36
+ - - ! '>='
37
+ - !ruby/object:Gem::Version
38
+ version: '0'
39
+ required_rubygems_version: !ruby/object:Gem::Requirement
40
+ none: false
41
+ requirements:
42
+ - - ! '>='
43
+ - !ruby/object:Gem::Version
44
+ version: '0'
45
+ requirements: []
46
+ rubyforge_project:
47
+ rubygems_version: 1.8.24
48
+ signing_key:
49
+ specification_version: 3
50
+ summary: Beta invites for Rack-based apps.
51
+ test_files:
52
+ - spec/lib/bacchus/rack/beta_site.rb