kazoo 0.0.5 → 0.0.6
Sign up to get free protection for your applications and to get access to all the features.
- data.tar.gz.sig +0 -0
- data/Manifest +6 -0
- data/Rakefile +3 -1
- data/kazoo.gemspec +4 -4
- data/lib/kazoo.rb +11 -4
- data/lib/kazoo/accessors.rb +2 -0
- data/lib/kazoo/app.rb +89 -0
- data/lib/kazoo/bouncer.rb +61 -0
- data/lib/kazoo/bouncer/authenticator.rb +30 -0
- data/lib/kazoo/router.rb +1 -63
- data/lib/kazoo/router/app.rb +44 -0
- data/lib/kazoo/router/common.rb +104 -0
- data/lib/kazoo/router/context.rb +28 -1
- data/lib/kazoo/router/dispatch.rb +30 -0
- data/lib/kazoo/router/route.rb +18 -6
- data/lib/kazoo/sinatra.rb +18 -70
- data/lib/kazoo/support.rb +23 -0
- data/readme.markdown +1 -1
- data/spec/router/route_spec.rb +12 -2
- data/spec/router_spec.rb +58 -24
- metadata +16 -4
- metadata.gz.sig +0 -0
data.tar.gz.sig
CHANGED
Binary file
|
data/Manifest
CHANGED
@@ -3,8 +3,14 @@ Manifest
|
|
3
3
|
Rakefile
|
4
4
|
lib/kazoo.rb
|
5
5
|
lib/kazoo/accessors.rb
|
6
|
+
lib/kazoo/app.rb
|
7
|
+
lib/kazoo/bouncer.rb
|
8
|
+
lib/kazoo/bouncer/authenticator.rb
|
6
9
|
lib/kazoo/router.rb
|
10
|
+
lib/kazoo/router/app.rb
|
11
|
+
lib/kazoo/router/common.rb
|
7
12
|
lib/kazoo/router/context.rb
|
13
|
+
lib/kazoo/router/dispatch.rb
|
8
14
|
lib/kazoo/router/route.rb
|
9
15
|
lib/kazoo/sinatra.rb
|
10
16
|
lib/kazoo/support.rb
|
data/Rakefile
CHANGED
@@ -5,7 +5,9 @@ require 'echoe'
|
|
5
5
|
require 'rspec'
|
6
6
|
require 'rspec/core/rake_task'
|
7
7
|
|
8
|
-
|
8
|
+
$LOAD_PATH.push( File.join(File.dirname(__FILE__), 'lib') )
|
9
|
+
|
10
|
+
Echoe.new('kazoo', '0.0.6') do |p|
|
9
11
|
p.description = "A moduler framework to facilitate the development of content management systems"
|
10
12
|
p.url = "http://rubykazoo.com"
|
11
13
|
p.author = "Jeremy Nicoll"
|
data/kazoo.gemspec
CHANGED
@@ -2,16 +2,16 @@
|
|
2
2
|
|
3
3
|
Gem::Specification.new do |s|
|
4
4
|
s.name = %q{kazoo}
|
5
|
-
s.version = "0.0.
|
5
|
+
s.version = "0.0.6"
|
6
6
|
|
7
7
|
s.required_rubygems_version = Gem::Requirement.new(">= 1.2") if s.respond_to? :required_rubygems_version=
|
8
8
|
s.authors = ["Jeremy Nicoll"]
|
9
9
|
s.cert_chain = ["/Users/eltiare/keys/gem-public_cert.pem"]
|
10
|
-
s.date = %q{2011-
|
10
|
+
s.date = %q{2011-03-10}
|
11
11
|
s.description = %q{A moduler framework to facilitate the development of content management systems}
|
12
12
|
s.email = %q{jnicoll @nspam@ accentuate.me}
|
13
|
-
s.extra_rdoc_files = ["lib/kazoo.rb", "lib/kazoo/accessors.rb", "lib/kazoo/router.rb", "lib/kazoo/router/context.rb", "lib/kazoo/router/route.rb", "lib/kazoo/sinatra.rb", "lib/kazoo/support.rb"]
|
14
|
-
s.files = ["MIT-License", "Manifest", "Rakefile", "lib/kazoo.rb", "lib/kazoo/accessors.rb", "lib/kazoo/router.rb", "lib/kazoo/router/context.rb", "lib/kazoo/router/route.rb", "lib/kazoo/sinatra.rb", "lib/kazoo/support.rb", "readme.markdown", "spec/router/context_spec.rb", "spec/router/route_spec.rb", "spec/router_spec.rb", "spec/spec.opts", "spec/spec_helper.rb", "kazoo.gemspec"]
|
13
|
+
s.extra_rdoc_files = ["lib/kazoo.rb", "lib/kazoo/accessors.rb", "lib/kazoo/app.rb", "lib/kazoo/bouncer.rb", "lib/kazoo/bouncer/authenticator.rb", "lib/kazoo/router.rb", "lib/kazoo/router/app.rb", "lib/kazoo/router/common.rb", "lib/kazoo/router/context.rb", "lib/kazoo/router/dispatch.rb", "lib/kazoo/router/route.rb", "lib/kazoo/sinatra.rb", "lib/kazoo/support.rb"]
|
14
|
+
s.files = ["MIT-License", "Manifest", "Rakefile", "lib/kazoo.rb", "lib/kazoo/accessors.rb", "lib/kazoo/app.rb", "lib/kazoo/bouncer.rb", "lib/kazoo/bouncer/authenticator.rb", "lib/kazoo/router.rb", "lib/kazoo/router/app.rb", "lib/kazoo/router/common.rb", "lib/kazoo/router/context.rb", "lib/kazoo/router/dispatch.rb", "lib/kazoo/router/route.rb", "lib/kazoo/sinatra.rb", "lib/kazoo/support.rb", "readme.markdown", "spec/router/context_spec.rb", "spec/router/route_spec.rb", "spec/router_spec.rb", "spec/spec.opts", "spec/spec_helper.rb", "kazoo.gemspec"]
|
15
15
|
s.homepage = %q{http://rubykazoo.com}
|
16
16
|
s.rdoc_options = ["--line-numbers", "--inline-source", "--title", "Kazoo", "--main", "readme.markdown"]
|
17
17
|
s.require_paths = ["lib"]
|
data/lib/kazoo.rb
CHANGED
@@ -1,12 +1,19 @@
|
|
1
|
-
$LOAD_PATH.push(File.dirname(__FILE__))
|
2
|
-
|
3
1
|
require 'cgi'
|
4
2
|
|
5
|
-
module Kazoo;
|
3
|
+
module Kazoo; end
|
6
4
|
|
7
5
|
require "kazoo/support"
|
8
6
|
require "kazoo/accessors"
|
9
7
|
|
8
|
+
require 'kazoo/router/common'
|
9
|
+
require 'kazoo/router/app'
|
10
|
+
require 'kazoo/router/dispatch'
|
10
11
|
require 'kazoo/router/route'
|
11
12
|
require 'kazoo/router/context'
|
12
|
-
require "kazoo/router"
|
13
|
+
require "kazoo/router"
|
14
|
+
|
15
|
+
require 'kazoo/app'
|
16
|
+
|
17
|
+
|
18
|
+
require 'kazoo/bouncer'
|
19
|
+
require 'kazoo/bouncer/authenticator'
|
data/lib/kazoo/accessors.rb
CHANGED
data/lib/kazoo/app.rb
ADDED
@@ -0,0 +1,89 @@
|
|
1
|
+
module Kazoo::App
|
2
|
+
|
3
|
+
def self.included(klass)
|
4
|
+
klass.send(:extend, ClassMethods)
|
5
|
+
klass.send(:include, Kazoo::Support)
|
6
|
+
end
|
7
|
+
|
8
|
+
module ClassMethods
|
9
|
+
|
10
|
+
def krouter
|
11
|
+
@_krouter ||= Kazoo::Router::App.new
|
12
|
+
end
|
13
|
+
|
14
|
+
def path_options=(val)
|
15
|
+
if val.is_a?(Hash)
|
16
|
+
@_route_options = val
|
17
|
+
else
|
18
|
+
raise ArgumentError, "route_options must be a hash"
|
19
|
+
end
|
20
|
+
end
|
21
|
+
|
22
|
+
def path_options
|
23
|
+
@_route_options || {}
|
24
|
+
end
|
25
|
+
|
26
|
+
def set_paths(opts)
|
27
|
+
opts.each { |k,v| path(k,v) }
|
28
|
+
end
|
29
|
+
|
30
|
+
def path(name, path = nil, opts = {})
|
31
|
+
opts = path_options.merge(opts)
|
32
|
+
|
33
|
+
path_prefix = opts.delete(:path_prefix)
|
34
|
+
name_prefix = opts.delete(:name_prefix)
|
35
|
+
|
36
|
+
name = "#{name_prefix}_#{name}" if name_prefix
|
37
|
+
|
38
|
+
if path
|
39
|
+
raise ArgumentError, "Path must be a string" unless path.is_a?(String)
|
40
|
+
path = File.join(path_prefix, path) if path_prefix
|
41
|
+
route = Kazoo::Router::Route.new(path, opts.merge(:app_mode => true))
|
42
|
+
krouter.add_route(route, name.to_sym)
|
43
|
+
path
|
44
|
+
else
|
45
|
+
raise ArgumentError, "#The route `{name}` does not exist" unless krouter.named_routes[name]
|
46
|
+
krouter.named_routes[name].to_s
|
47
|
+
end
|
48
|
+
end
|
49
|
+
|
50
|
+
end
|
51
|
+
|
52
|
+
def path(name,path,opts)
|
53
|
+
self.class.path(name,path,opts)
|
54
|
+
end
|
55
|
+
|
56
|
+
def krouter
|
57
|
+
self.class.krouter
|
58
|
+
end
|
59
|
+
|
60
|
+
def url(name = '', opts = {})
|
61
|
+
|
62
|
+
if name.is_a?(Symbol)
|
63
|
+
raise ArgumentError, "Invalid url name: #{name}" unless krouter.named_routes[name]
|
64
|
+
|
65
|
+
url_path = krouter.named_routes[name].to_s.split('/').map { |part|
|
66
|
+
next unless part.is_a?(String)
|
67
|
+
if matches = part.match(/^:([a-z_]+)$/i)
|
68
|
+
matched = matches[1].downcase
|
69
|
+
opts[matched] || opts[matched.to_sym] || params[matched] || raise("You need to pass '#{matched}' to generate URL")
|
70
|
+
else
|
71
|
+
part
|
72
|
+
end
|
73
|
+
}.join('/')
|
74
|
+
|
75
|
+
# Check for prefix
|
76
|
+
full_path = kenv['path_prefix'] ? File.join(kenv['path_prefix'], url_path) : url_path
|
77
|
+
|
78
|
+
format = opts[:format] || opts['format']
|
79
|
+
full_path << ".#{format}" if format
|
80
|
+
|
81
|
+
full_path
|
82
|
+
else
|
83
|
+
kenv['HTTP_PREFIX'] ? File.join(kenv['HTTP_PREFIX'], name) : name
|
84
|
+
end
|
85
|
+
|
86
|
+
end
|
87
|
+
|
88
|
+
|
89
|
+
end
|
@@ -0,0 +1,61 @@
|
|
1
|
+
class Kazoo::Bouncer
|
2
|
+
|
3
|
+
def self.authenticators
|
4
|
+
@_authenticators ||= []
|
5
|
+
end
|
6
|
+
|
7
|
+
def self.add_authenticator(klass)
|
8
|
+
authenticators << klass
|
9
|
+
end
|
10
|
+
|
11
|
+
def initialize(env)
|
12
|
+
@env = env
|
13
|
+
@request = Rack::Request.new(env)
|
14
|
+
end
|
15
|
+
|
16
|
+
def request
|
17
|
+
@request
|
18
|
+
end
|
19
|
+
|
20
|
+
def session
|
21
|
+
@request.session
|
22
|
+
end
|
23
|
+
|
24
|
+
def current_user
|
25
|
+
@_current_user ||= if session['kazoo.user'] && session['kazoo.user'][0]
|
26
|
+
Kernel.const_get(session['kazoo.user'][0]).find(session['kazoo.user'][1]) rescue nil
|
27
|
+
end
|
28
|
+
end
|
29
|
+
|
30
|
+
def current_user=(user)
|
31
|
+
if user.nil?
|
32
|
+
forget
|
33
|
+
else
|
34
|
+
session['kazoo.user'] = [user.class.name, user.id]
|
35
|
+
user
|
36
|
+
end
|
37
|
+
end
|
38
|
+
|
39
|
+
def authenticated?
|
40
|
+
session['kazoo.authenticated'] ? true : false
|
41
|
+
end
|
42
|
+
|
43
|
+
def forget
|
44
|
+
session.delete('kazoo.authenticated')
|
45
|
+
session.delete('kazoo.user')
|
46
|
+
end
|
47
|
+
|
48
|
+
def authenticate!
|
49
|
+
self.class.authenticators.each { |auth|
|
50
|
+
user = auth.new.call(request)
|
51
|
+
if user
|
52
|
+
session['kazoo.authenticated'] = '1'
|
53
|
+
self.current_user = user if user.class.respond_to?(:find)
|
54
|
+
puts 'Session: ', session.inspect
|
55
|
+
return user
|
56
|
+
end
|
57
|
+
}
|
58
|
+
nil
|
59
|
+
end
|
60
|
+
|
61
|
+
end
|
@@ -0,0 +1,30 @@
|
|
1
|
+
class Kazoo::Bouncer
|
2
|
+
|
3
|
+
class Authenticator
|
4
|
+
|
5
|
+
def self.inherited(klass)
|
6
|
+
Kazoo::Bouncer.add_authenticator klass
|
7
|
+
end
|
8
|
+
|
9
|
+
def call(req)
|
10
|
+
@_params = req.params
|
11
|
+
@_request = req
|
12
|
+
run
|
13
|
+
end
|
14
|
+
|
15
|
+
def params
|
16
|
+
@_params
|
17
|
+
end
|
18
|
+
|
19
|
+
def request
|
20
|
+
@_request
|
21
|
+
end
|
22
|
+
|
23
|
+
def run
|
24
|
+
raise NotImplementedError, "You must override the `run` method in #{self.class.name}. Please don't punch any ducks"
|
25
|
+
end
|
26
|
+
|
27
|
+
end
|
28
|
+
|
29
|
+
|
30
|
+
end
|
data/lib/kazoo/router.rb
CHANGED
@@ -1,67 +1,5 @@
|
|
1
1
|
class Kazoo::Router
|
2
2
|
|
3
|
-
|
4
|
-
new.map(&blk)
|
5
|
-
end
|
6
|
-
|
7
|
-
def map(&blk)
|
8
|
-
@context ||= Context.new(self)
|
9
|
-
@context.instance_eval(&blk)
|
10
|
-
self
|
11
|
-
end
|
12
|
-
|
13
|
-
def routes
|
14
|
-
@routes ||= []
|
15
|
-
end
|
16
|
-
|
17
|
-
def named_routes
|
18
|
-
@named_routes ||= {}
|
19
|
-
end
|
20
|
-
|
21
|
-
def error_handler(app)
|
22
|
-
@error_handler = app
|
23
|
-
end
|
24
|
-
|
25
|
-
def general_handler(app)
|
26
|
-
@general_handler = app
|
27
|
-
end
|
28
|
-
|
29
|
-
def kenv
|
30
|
-
@env['kazoo'] ||= {}
|
31
|
-
end
|
32
|
-
|
33
|
-
def call(env)
|
34
|
-
@env = env
|
35
|
-
@routes.each do |route|
|
36
|
-
env['PATH_INFO'] = "#{env["PATH_INFO"]}/" unless %r'/$'.match(env['PATH_INFO'])
|
37
|
-
params = route.extract_params(env['PATH_INFO'])
|
38
|
-
|
39
|
-
if params && route.app
|
40
|
-
kenv['params'] ||= {}
|
41
|
-
kenv['params'].merge!(params)
|
42
|
-
|
43
|
-
match = route.to_regexp.match(env['PATH_INFO'])[0]
|
44
|
-
kenv['path_prefix'] = kenv['path_prefix'] ? File.join(kenv['path_prefix'], match) : match
|
45
|
-
|
46
|
-
env['PATH_INFO'] = env['PATH_INFO'].sub(match, '')
|
47
|
-
env['PATH_INFO'] = "/#{env['PATH_INFO']}" unless env['PATH_INFO'].match(%r'^/')
|
48
|
-
|
49
|
-
response = route.app.call(env)
|
50
|
-
return response unless response[1]['X-Cascade'] == 'pass'
|
51
|
-
end
|
52
|
-
|
53
|
-
end
|
54
|
-
|
55
|
-
# If no routes found
|
56
|
-
default_response = [404, {'Content-Type' => 'text/plain', "X-Cascade" => "pass"}, 'The requested URI is not found']
|
57
|
-
if @error_handler
|
58
|
-
env['error'] = 'not_found'
|
59
|
-
env['default_response'] = default_response
|
60
|
-
@error_handler.call(env)
|
61
|
-
else
|
62
|
-
default_response
|
63
|
-
end
|
64
|
-
end
|
65
|
-
|
3
|
+
include Dispatch
|
66
4
|
|
67
5
|
end
|
@@ -0,0 +1,44 @@
|
|
1
|
+
class Kazoo::Router
|
2
|
+
|
3
|
+
class App
|
4
|
+
|
5
|
+
include Common
|
6
|
+
|
7
|
+
set :type, :app
|
8
|
+
|
9
|
+
def self.map(opts = {}, &blk)
|
10
|
+
@_context_options = opts
|
11
|
+
@_context ||= Context.new(self, opts)
|
12
|
+
@_context.instance_eval(&blk)
|
13
|
+
self
|
14
|
+
end#def map
|
15
|
+
|
16
|
+
def self.context_options
|
17
|
+
@_context_opts
|
18
|
+
end
|
19
|
+
|
20
|
+
def self.add_route(route, name=nil)
|
21
|
+
routes << route
|
22
|
+
named_routes[name.to_sym] = route if name
|
23
|
+
end
|
24
|
+
|
25
|
+
def self.routes
|
26
|
+
@_routes ||= []
|
27
|
+
end
|
28
|
+
|
29
|
+
def self.named_routes
|
30
|
+
@_named_routes ||= {}
|
31
|
+
end
|
32
|
+
|
33
|
+
def routes
|
34
|
+
@_routes ||= self.class.routes.dup
|
35
|
+
end
|
36
|
+
|
37
|
+
def named_routes
|
38
|
+
@_named_routes ||= self.class.named_routes.dup
|
39
|
+
end
|
40
|
+
|
41
|
+
|
42
|
+
end#module App
|
43
|
+
|
44
|
+
end#class Kazoo::Router
|
@@ -0,0 +1,104 @@
|
|
1
|
+
class Kazoo::Router
|
2
|
+
|
3
|
+
module Common
|
4
|
+
|
5
|
+
def self.included(klass)
|
6
|
+
klass.send(:extend, ClassMethods)
|
7
|
+
end
|
8
|
+
|
9
|
+
module ClassMethods
|
10
|
+
|
11
|
+
def set(var, val)
|
12
|
+
@_settings ||= {}
|
13
|
+
@_settings[var.to_sym] = val
|
14
|
+
end
|
15
|
+
|
16
|
+
def get(var)
|
17
|
+
@_settings ||= {}
|
18
|
+
@_settings[var.to_sym]
|
19
|
+
end
|
20
|
+
|
21
|
+
def context_options
|
22
|
+
nil
|
23
|
+
end
|
24
|
+
|
25
|
+
end#ClassMethods
|
26
|
+
|
27
|
+
def set(var,val)
|
28
|
+
@_settings ||= {}
|
29
|
+
@_settings[var.to_sym] = val
|
30
|
+
end
|
31
|
+
|
32
|
+
def get(var)
|
33
|
+
@_settings ||= {}
|
34
|
+
@_settings[var.to_sym] || self.class.get(var)
|
35
|
+
end
|
36
|
+
|
37
|
+
def map(opts = {}, &blk)
|
38
|
+
opts = self.class.context_options.merge(opts) if self.class.context_options
|
39
|
+
@context ||= Context.new(self, opts)
|
40
|
+
@context.instance_eval(&blk)
|
41
|
+
self
|
42
|
+
end
|
43
|
+
|
44
|
+
def add_route(route, name=nil)
|
45
|
+
routes << route
|
46
|
+
named_routes[name.to_sym] = route if name
|
47
|
+
end
|
48
|
+
|
49
|
+
def routes
|
50
|
+
@routes ||= []
|
51
|
+
end
|
52
|
+
|
53
|
+
def named_routes
|
54
|
+
@named_routes ||= {}
|
55
|
+
end
|
56
|
+
|
57
|
+
def kenv
|
58
|
+
@env['kazoo'] ||= {}
|
59
|
+
end
|
60
|
+
|
61
|
+
def call(env)
|
62
|
+
@env = env
|
63
|
+
kenv['params'] ||= {}
|
64
|
+
|
65
|
+
#TODO: This hack is causing problems. Figure out regexp to match paths better.
|
66
|
+
env['PATH_INFO'] = "#{env["PATH_INFO"]}/" unless %r'/$'.match(env['PATH_INFO'])
|
67
|
+
|
68
|
+
@routes.each do |route|
|
69
|
+
|
70
|
+
params = route.extract_params(env['PATH_INFO'])
|
71
|
+
|
72
|
+
if params && route.app
|
73
|
+
kenv['params'].merge!(params)
|
74
|
+
|
75
|
+
match = route.to_regexp.match(env['PATH_INFO'])[0]
|
76
|
+
kenv['path_prefix'] = kenv['path_prefix'] ? File.join(kenv['path_prefix'], match) : match
|
77
|
+
|
78
|
+
env['PATH_INFO'] = env['PATH_INFO'].sub(match, '')
|
79
|
+
env['PATH_INFO'] = "/#{env['PATH_INFO']}" unless env['PATH_INFO'].match(%r'^/')
|
80
|
+
|
81
|
+
response = route.app.call(env)
|
82
|
+
|
83
|
+
if response[1]['X-Cascade'] == 'pass'
|
84
|
+
next
|
85
|
+
elsif @error_handler && response[0] > 399
|
86
|
+
return @error_handler.call(env, *response)
|
87
|
+
else
|
88
|
+
return response
|
89
|
+
end
|
90
|
+
|
91
|
+
elsif get(:mode) == :app && params
|
92
|
+
return params
|
93
|
+
end
|
94
|
+
|
95
|
+
end
|
96
|
+
|
97
|
+
# If no routes found
|
98
|
+
default_response = [404, {'Content-Type' => 'text/plain', "X-Cascade" => "pass"}, 'The requested URI is not found']
|
99
|
+
@error_handler ? @error_handler.call(env, *default_response) : default_response
|
100
|
+
end
|
101
|
+
|
102
|
+
end
|
103
|
+
|
104
|
+
end
|
data/lib/kazoo/router/context.rb
CHANGED
@@ -10,12 +10,33 @@ class Kazoo::Router
|
|
10
10
|
def match(path, opts)
|
11
11
|
opts = @opts.merge(opts)
|
12
12
|
name = extract_name(opts)
|
13
|
+
|
14
|
+
if get(:mode) == :app
|
15
|
+
opts[:app_mode] = true
|
16
|
+
else
|
17
|
+
opts[:default_app] = @default_app
|
18
|
+
end
|
19
|
+
|
13
20
|
route = Route.new(path, opts)
|
14
21
|
@router.named_routes[name] = route if name
|
15
22
|
@router.routes << route
|
16
23
|
route
|
17
24
|
end
|
18
25
|
|
26
|
+
def set(var,val)
|
27
|
+
@router.set(var,val)
|
28
|
+
end
|
29
|
+
|
30
|
+
def get(var)
|
31
|
+
@router.get(var)
|
32
|
+
end
|
33
|
+
|
34
|
+
def default_app(app)
|
35
|
+
raise ArgumentError, "default_app must be a rack application (#{app} doesn't respond to call)" unless app.respond_to?(:call)
|
36
|
+
raise NameError, "NameError: undefined local variable or method `default_app' for #{self}" if get(:mode) == :app
|
37
|
+
@default_app = app
|
38
|
+
end
|
39
|
+
|
19
40
|
def context(opts = {})
|
20
41
|
opts = opts.dup
|
21
42
|
|
@@ -34,7 +55,13 @@ class Kazoo::Router
|
|
34
55
|
end
|
35
56
|
|
36
57
|
def extract_name(opts)
|
37
|
-
|
58
|
+
if @opts[:name_prefix] && opts[:name]
|
59
|
+
"#{@opts[:name_prefix]}_#{opts[:name]}"
|
60
|
+
elsif opts[:name]
|
61
|
+
opts[:name]
|
62
|
+
else
|
63
|
+
nil
|
64
|
+
end
|
38
65
|
end
|
39
66
|
|
40
67
|
def error_handler(app)
|
@@ -0,0 +1,30 @@
|
|
1
|
+
class Kazoo::Router
|
2
|
+
|
3
|
+
module Dispatch
|
4
|
+
|
5
|
+
def self.included(klass)
|
6
|
+
klass.send(:include, Common)
|
7
|
+
klass.set :type, :dispatch
|
8
|
+
klass.send(:extend, ClassMethods)
|
9
|
+
end
|
10
|
+
|
11
|
+
module ClassMethods
|
12
|
+
|
13
|
+
def map(&blk)
|
14
|
+
new.map(&blk)
|
15
|
+
end
|
16
|
+
|
17
|
+
end
|
18
|
+
|
19
|
+
def error_handler(app)
|
20
|
+
@error_handler = app
|
21
|
+
end
|
22
|
+
|
23
|
+
def general_handler(app)
|
24
|
+
@general_handler = app
|
25
|
+
end
|
26
|
+
|
27
|
+
end
|
28
|
+
|
29
|
+
|
30
|
+
end
|
data/lib/kazoo/router/route.rb
CHANGED
@@ -5,14 +5,26 @@ class Kazoo::Router
|
|
5
5
|
def initialize(path, opts = {})
|
6
6
|
|
7
7
|
@named_params = []
|
8
|
-
@opts = opts
|
8
|
+
@opts = opts.dup
|
9
9
|
|
10
10
|
# Extract & delete the special options
|
11
|
-
|
11
|
+
app_mode = @opts.delete(:app_mode)
|
12
12
|
|
13
|
-
|
14
|
-
|
15
|
-
|
13
|
+
to = @opts.delete(:to)
|
14
|
+
default_app = @opts.delete(:default_app)
|
15
|
+
@app = to || default_app
|
16
|
+
|
17
|
+
raise ArgumentError, 'You must supply an application/block' if !@app && !app_mode
|
18
|
+
|
19
|
+
@path_prefix = @opts.delete(:path_prefix)
|
20
|
+
@regex_prefix = @opts.delete(:regex_prefix)
|
21
|
+
@opts.delete(:name_prefix)
|
22
|
+
|
23
|
+
@opts.each { |k,v|
|
24
|
+
next if k.is_a?(String)
|
25
|
+
@opts.delete(k)
|
26
|
+
@opts[k.to_s] = v
|
27
|
+
}
|
16
28
|
|
17
29
|
# Parse the path
|
18
30
|
if path.is_a?(String)
|
@@ -46,7 +58,7 @@ class Kazoo::Router
|
|
46
58
|
|
47
59
|
final_path = "/#{final_path}" unless final_path.match(%r"^\/")
|
48
60
|
final_path = "#{final_path}/" unless final_path.match(%r"\/$")
|
49
|
-
@matcher = Regexp.new("^#{final_path}")
|
61
|
+
@matcher = Regexp.new(app_mode ? "^#{final_path}$" : "^#{final_path}" )
|
50
62
|
end
|
51
63
|
|
52
64
|
def to_s
|
data/lib/kazoo/sinatra.rb
CHANGED
@@ -2,12 +2,17 @@ require 'sinatra/base'
|
|
2
2
|
|
3
3
|
class Kazoo::Sinatra < Sinatra::Base
|
4
4
|
|
5
|
-
|
5
|
+
include Kazoo::App
|
6
6
|
|
7
7
|
before do
|
8
8
|
params.merge!(kenv['params']) if kenv['params']
|
9
9
|
end
|
10
10
|
|
11
|
+
def call!(env)
|
12
|
+
@_bouncer ||= Kazoo::Bouncer.new(env)
|
13
|
+
super
|
14
|
+
end
|
15
|
+
|
11
16
|
def ksession
|
12
17
|
session['kazoo'] ||= {}
|
13
18
|
end
|
@@ -17,17 +22,22 @@ class Kazoo::Sinatra < Sinatra::Base
|
|
17
22
|
end
|
18
23
|
|
19
24
|
def current_user=(user)
|
20
|
-
|
21
|
-
ksession['user_id'] = user.id
|
22
|
-
ksession['user_class'] = user.class.name
|
23
|
-
else
|
24
|
-
ksession['user_class'] = ksession['user_id'] = nil
|
25
|
-
end
|
25
|
+
@_bouncer.current_user = user
|
26
26
|
user
|
27
27
|
end
|
28
28
|
|
29
29
|
def current_user
|
30
|
-
@
|
30
|
+
@_bouncer.current_user
|
31
|
+
end
|
32
|
+
|
33
|
+
def authenticate!
|
34
|
+
unless @_bouncer.authenticated?
|
35
|
+
if @_bouncer.authenticate!
|
36
|
+
halt 302, {'Location' => request.env['rack.url_scheme'] + '://' +request.env['HTTP_HOST'] + request.env['REQUEST_PATH']}, ''
|
37
|
+
else
|
38
|
+
throw(:halt, [401, "Not authorized\n"])
|
39
|
+
end
|
40
|
+
end
|
31
41
|
end
|
32
42
|
|
33
43
|
def render(engine, data, options = {}, *args)
|
@@ -44,68 +54,6 @@ class Kazoo::Sinatra < Sinatra::Base
|
|
44
54
|
super(engine, data, options, *args)
|
45
55
|
|
46
56
|
end
|
47
|
-
|
48
|
-
|
49
|
-
def self.set_paths(opts)
|
50
|
-
opts.each { |k,v| path(k,v) }
|
51
|
-
end
|
52
|
-
|
53
|
-
|
54
|
-
def self.path(name, path = nil)
|
55
|
-
if path
|
56
|
-
raise ArgumentError, "Path must be a string" unless path.is_a?(String)
|
57
|
-
paths[name.to_sym] = path
|
58
|
-
path
|
59
|
-
else
|
60
|
-
paths[name.to_sym]
|
61
|
-
end
|
62
|
-
end
|
63
|
-
|
64
|
-
def self.paths
|
65
|
-
@paths ||= {}
|
66
|
-
end
|
67
|
-
|
68
|
-
def path(name,path)
|
69
|
-
self.class.path(name,path)
|
70
|
-
end
|
71
|
-
|
72
|
-
def paths
|
73
|
-
self.class.paths
|
74
|
-
end
|
75
|
-
|
76
|
-
def url(name = '', opts = {})
|
77
|
-
if name.is_a?(Symbol)
|
78
|
-
raise ArgumentError, "Invalid url name: #{name}" unless paths[name]
|
79
|
-
|
80
|
-
url_path = paths[name].split('/').map { |part|
|
81
|
-
next unless part.is_a?(String)
|
82
|
-
if matches = part.match(/^:([a-z_]+)$/i)
|
83
|
-
matched = matches[1].downcase
|
84
|
-
opts[matched] || opts[matched.to_sym] || params[matched] || raise("You need to pass '#{matched}' to generate URL")
|
85
|
-
else
|
86
|
-
part
|
87
|
-
end
|
88
|
-
}.join('/')
|
89
|
-
|
90
|
-
# Check for prefix
|
91
|
-
full_path = kenv['HTTP_PREFIX'] ? File.join(kenv['HTTP_PREFIX'], url_path) : url_path
|
92
|
-
|
93
|
-
format = opts[:format] || opts['format']
|
94
|
-
full_path << ".#{format}" if format
|
95
|
-
|
96
|
-
full_path
|
97
|
-
else
|
98
|
-
kenv['HTTP_PREFIX'] ? File.join(kenv['HTTP_PREFIX'], name) : name
|
99
|
-
end
|
100
|
-
end
|
101
|
-
|
102
|
-
private
|
103
57
|
|
104
|
-
def decamelize(class_name)
|
105
|
-
parts = class_name.split('::')
|
106
|
-
parts.map { |part|
|
107
|
-
part.gsub(/([A-Z])/, '_\1').gsub(/(^\_)|(\_$)/, '').downcase
|
108
|
-
}.join('/')
|
109
|
-
end
|
110
58
|
|
111
59
|
end
|
data/lib/kazoo/support.rb
CHANGED
@@ -1,5 +1,28 @@
|
|
1
1
|
module Kazoo::Support
|
2
2
|
|
3
|
+
HTTP_CODES = {
|
4
|
+
400 => :bad_request,
|
5
|
+
401 => :unauthorized,
|
6
|
+
402 => :payment_required,
|
7
|
+
403 => :forbidden,
|
8
|
+
404 => :not_found,
|
9
|
+
405 => :method_not_allowed,
|
10
|
+
406 => :not_acceptable,
|
11
|
+
500 => :internal_server_error,
|
12
|
+
501 => :not_implemented,
|
13
|
+
502 => :bad_gateway,
|
14
|
+
503 => :service_unavailable,
|
15
|
+
504 => :gateway_timeout,
|
16
|
+
505 => :http_version_not_supported
|
17
|
+
}
|
18
|
+
|
19
|
+
|
20
|
+
def decamelize(class_name)
|
21
|
+
parts = class_name.split('::')
|
22
|
+
parts.map { |part|
|
23
|
+
part.gsub(/([A-Z])/, '_\1').gsub(/(^\_)|(\_$)/, '').downcase
|
24
|
+
}.join('/')
|
25
|
+
end
|
3
26
|
|
4
27
|
def load_files(files)
|
5
28
|
excepted_inits = []
|
data/readme.markdown
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
Kazoo
|
2
2
|
=====
|
3
3
|
|
4
|
-
Kazoo is a Ruby framework to faciliate modular content management applications. So far, it's a router and some methods added to Sinatra to make it easier to have multiple Sinatra apps act more cohesively. Please
|
4
|
+
Kazoo is a Ruby framework to faciliate modular content management applications. So far, it's a router and some methods added to Sinatra to make it easier to have multiple Sinatra apps act more cohesively. Please be aware that it is not production ready yet. Kazoo is on rubygems.org so that you can install it by running:
|
5
5
|
|
6
6
|
gem install kazoo
|
7
7
|
|
data/spec/router/route_spec.rb
CHANGED
@@ -3,14 +3,20 @@ require File.join(File.dirname(__FILE__), '../spec_helper')
|
|
3
3
|
describe Kazoo::Router::Route do
|
4
4
|
|
5
5
|
before(:each) do
|
6
|
-
|
7
|
-
@
|
6
|
+
blk = lambda { [200, { 'Content-Type' => 'text/plan' }, 'Success!'] }
|
7
|
+
@route = Kazoo::Router::Route.new('/your/:relative/is/:adjective', :to => blk)
|
8
|
+
@route2 = Kazoo::Router::Route.new('/testing', :to => blk, :test => 3)
|
9
|
+
@app_route = Kazoo::Router::Route.new('/testing', :app_mode => true)
|
8
10
|
end
|
9
11
|
|
10
12
|
it "extracts the variables from a path" do
|
11
13
|
@route.named_params.should eq(['relative', 'adjective'])
|
12
14
|
end
|
13
15
|
|
16
|
+
it "converts param keys to a string" do
|
17
|
+
@route2.extract_params('/testing/').keys.should eq(['test'])
|
18
|
+
end
|
19
|
+
|
14
20
|
it "generates a path with variables" do
|
15
21
|
@route.path(:relative => 'mom', :adjective => 'huge').should eq(
|
16
22
|
'/your/mom/is/huge'
|
@@ -36,4 +42,8 @@ describe Kazoo::Router::Route do
|
|
36
42
|
@route2.extract_params('/testing_and_stuff').should eq(nil)
|
37
43
|
end
|
38
44
|
|
45
|
+
it "matches exactly when acting as an app route" do
|
46
|
+
@app_route.to_regexp.inspect.should eq('/^\/testing\/$/')
|
47
|
+
end
|
48
|
+
|
39
49
|
end
|
data/spec/router_spec.rb
CHANGED
@@ -1,38 +1,72 @@
|
|
1
1
|
require File.join(File.dirname(__FILE__), 'spec_helper')
|
2
2
|
|
3
|
+
def path(p)
|
4
|
+
{ 'PATH_INFO' => p }
|
5
|
+
end
|
3
6
|
|
4
7
|
describe Kazoo::Router do
|
5
8
|
|
6
|
-
|
7
|
-
|
8
|
-
|
9
|
-
|
10
|
-
|
11
|
-
|
12
|
-
|
13
|
-
|
14
|
-
|
15
|
-
|
16
|
-
|
17
|
-
|
9
|
+
|
10
|
+
describe "dispatch mode" do
|
11
|
+
|
12
|
+
before :each do
|
13
|
+
|
14
|
+
@proper_response = [200, {'Content-Type' => 'text/plain'}, 'Yay!']
|
15
|
+
@proper_response2 = [200, {'Content-Type' => 'text/plain'}, 'Response2']
|
16
|
+
@not_found_response = [404, {"X-Cascade"=>"pass", "Content-Type"=>"text/plain"}, "The requested URI is not found"]
|
17
|
+
|
18
|
+
handler = lambda { @proper_response }
|
19
|
+
handler2 = lambda { @proper_response2 }
|
20
|
+
|
21
|
+
@router = Kazoo::Router.map do
|
22
|
+
match '/something/:id', :to => handler
|
23
|
+
match '/something', :to => handler
|
24
|
+
match '/something_else', :to => handler2
|
25
|
+
end
|
26
|
+
end
|
27
|
+
|
28
|
+
it "routes to the proper handler" do
|
29
|
+
@router.call(path '/something').should eq(@proper_response)
|
30
|
+
@router.call(path '/something/else').should eq(@proper_response)
|
31
|
+
end
|
32
|
+
|
33
|
+
it "sets the kazoo params" do
|
34
|
+
env = path '/something/123'
|
35
|
+
@router.call(env)
|
36
|
+
env['kazoo']['params'].should eq({'id' => '123'})
|
37
|
+
end
|
38
|
+
|
39
|
+
it "matches later routes properly" do
|
40
|
+
@router.call(path '/something_else/testing_blah').should eq(@proper_response2)
|
41
|
+
end
|
42
|
+
|
43
|
+
it "allows for a default app" do
|
44
|
+
router = Kazoo::Router.map do
|
45
|
+
default_app lambda { |hash|
|
46
|
+
raise "hell" unless hash
|
47
|
+
[200, {'Content-Type' => 'text/plain'}, "Hey!"]
|
48
|
+
}
|
49
|
+
end
|
18
50
|
end
|
19
51
|
|
20
52
|
end
|
21
53
|
|
22
|
-
it "routes to the proper handler" do
|
23
|
-
@router.call({ 'PATH_INFO' => '/something' }).should eq(@proper_response)
|
24
|
-
@router.call({ 'PATH_INFO' => '/something/else'}).should eq(@proper_response)
|
25
|
-
end
|
26
54
|
|
27
|
-
|
28
|
-
|
29
|
-
|
30
|
-
|
55
|
+
describe "app mode" do
|
56
|
+
|
57
|
+
before :each do
|
58
|
+
@router = Kazoo::Router.map do
|
59
|
+
set :mode, :app
|
60
|
+
match '/testing', :view => 'good'
|
61
|
+
end
|
62
|
+
end
|
63
|
+
|
64
|
+
it "returns a hash" do
|
65
|
+
@router.call(path '/testing').should eq({'view' => 'good'})
|
66
|
+
end
|
67
|
+
|
68
|
+
|
31
69
|
end
|
32
70
|
|
33
|
-
it "matches later routes properly" do
|
34
|
-
env = { 'PATH_INFO' => '/something_else/testing_blah' }
|
35
|
-
@router.call(env).should eq(@proper_response2)
|
36
|
-
end
|
37
71
|
|
38
72
|
end
|
metadata
CHANGED
@@ -1,13 +1,13 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: kazoo
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
hash:
|
4
|
+
hash: 19
|
5
5
|
prerelease:
|
6
6
|
segments:
|
7
7
|
- 0
|
8
8
|
- 0
|
9
|
-
-
|
10
|
-
version: 0.0.
|
9
|
+
- 6
|
10
|
+
version: 0.0.6
|
11
11
|
platform: ruby
|
12
12
|
authors:
|
13
13
|
- Jeremy Nicoll
|
@@ -36,7 +36,7 @@ cert_chain:
|
|
36
36
|
ZVeyFF4suKZEIEKi
|
37
37
|
-----END CERTIFICATE-----
|
38
38
|
|
39
|
-
date: 2011-
|
39
|
+
date: 2011-03-10 00:00:00 -07:00
|
40
40
|
default_executable:
|
41
41
|
dependencies: []
|
42
42
|
|
@@ -49,8 +49,14 @@ extensions: []
|
|
49
49
|
extra_rdoc_files:
|
50
50
|
- lib/kazoo.rb
|
51
51
|
- lib/kazoo/accessors.rb
|
52
|
+
- lib/kazoo/app.rb
|
53
|
+
- lib/kazoo/bouncer.rb
|
54
|
+
- lib/kazoo/bouncer/authenticator.rb
|
52
55
|
- lib/kazoo/router.rb
|
56
|
+
- lib/kazoo/router/app.rb
|
57
|
+
- lib/kazoo/router/common.rb
|
53
58
|
- lib/kazoo/router/context.rb
|
59
|
+
- lib/kazoo/router/dispatch.rb
|
54
60
|
- lib/kazoo/router/route.rb
|
55
61
|
- lib/kazoo/sinatra.rb
|
56
62
|
- lib/kazoo/support.rb
|
@@ -60,8 +66,14 @@ files:
|
|
60
66
|
- Rakefile
|
61
67
|
- lib/kazoo.rb
|
62
68
|
- lib/kazoo/accessors.rb
|
69
|
+
- lib/kazoo/app.rb
|
70
|
+
- lib/kazoo/bouncer.rb
|
71
|
+
- lib/kazoo/bouncer/authenticator.rb
|
63
72
|
- lib/kazoo/router.rb
|
73
|
+
- lib/kazoo/router/app.rb
|
74
|
+
- lib/kazoo/router/common.rb
|
64
75
|
- lib/kazoo/router/context.rb
|
76
|
+
- lib/kazoo/router/dispatch.rb
|
65
77
|
- lib/kazoo/router/route.rb
|
66
78
|
- lib/kazoo/sinatra.rb
|
67
79
|
- lib/kazoo/support.rb
|
metadata.gz.sig
CHANGED
Binary file
|