rack-golem 0.0.3 → 0.0.4
Sign up to get free protection for your applications and to get access to all the features.
- data/README.md +10 -2
- data/lib/rack/golem.rb +27 -4
- data/rack-golem.gemspec +2 -2
- data/test/spec_golem.rb +67 -3
- metadata +6 -8
data/README.md
CHANGED
@@ -18,7 +18,7 @@ Install with:
|
|
18
18
|
Config.ru is one of his names, so say it in a Rackup file.
|
19
19
|
|
20
20
|
require 'db' # Loads ORM models and all
|
21
|
-
require 'go' # Our controller (I
|
21
|
+
require 'go' # Our controller (I do not like that word really)
|
22
22
|
use Rack::ContentLength
|
23
23
|
use Rack::Session::Cookies
|
24
24
|
run Go
|
@@ -42,7 +42,7 @@ And the winner is:
|
|
42
42
|
def index(*args)
|
43
43
|
# When no public method is found
|
44
44
|
# Of course you don't have to declare one and it is gonna use Controller#not_found instead
|
45
|
-
#
|
45
|
+
# Still can have arguments
|
46
46
|
@articles = Post.all
|
47
47
|
erb :index
|
48
48
|
end
|
@@ -66,6 +66,14 @@ And the winner is:
|
|
66
66
|
Email.alert('Too many people are looking for porn here') if args.includes?("porn")
|
67
67
|
super(args)
|
68
68
|
end
|
69
|
+
|
70
|
+
def error(err, *args)
|
71
|
+
# Again this one is defined by Golem and only shows up when RACK_ENV is not `nil` or `dev` or `development`
|
72
|
+
# Default only prints "ERROR"
|
73
|
+
# Here we're going to send the error message
|
74
|
+
# One would rarely show that to the end user but this is just a demo
|
75
|
+
err.message
|
76
|
+
end
|
69
77
|
|
70
78
|
after do
|
71
79
|
Spy.analyse.send_info_to([:government, :facebook, :google, :james_bond])
|
data/lib/rack/golem.rb
CHANGED
@@ -19,7 +19,7 @@ module Rack::Golem
|
|
19
19
|
@path_atoms = @r.path_info.split('/').find_all{|s| s!=''}
|
20
20
|
@action, *@action_arguments = @path_atoms
|
21
21
|
unless public_methods.include?(@action)||(@action&&public_methods.include?(@action.to_sym))
|
22
|
-
if public_methods.include?('index')||public_methods.include?(:index)
|
22
|
+
if public_methods.include?('index')||public_methods.include?(:index) # For different RUBY_VERSION(s)
|
23
23
|
@action, @action_arguments = 'index', @path_atoms
|
24
24
|
else
|
25
25
|
@action, @action_arguments = 'not_found', @path_atoms
|
@@ -28,7 +28,13 @@ module Rack::Golem
|
|
28
28
|
|
29
29
|
instance_eval(&self.class.before_block) unless self.class.before_block.nil?
|
30
30
|
|
31
|
-
|
31
|
+
begin
|
32
|
+
@res.write(self.__send__(@action,*@action_arguments))
|
33
|
+
rescue ArgumentError => e
|
34
|
+
failed_method = e.backtrace[0][/`.*'$/][1..-2]
|
35
|
+
raise unless failed_method==@action
|
36
|
+
@res.write(self.__send__('not_found', @path_atoms))
|
37
|
+
end
|
32
38
|
|
33
39
|
instance_eval(&self.class.after_block) unless self.class.after_block.nil?
|
34
40
|
}
|
@@ -36,19 +42,36 @@ module Rack::Golem
|
|
36
42
|
end
|
37
43
|
|
38
44
|
module InstanceMethods
|
45
|
+
|
46
|
+
DEV_ENV = [nil,'development','dev']
|
47
|
+
|
39
48
|
def initialize(app=nil); @app = app; end
|
40
49
|
def call(env); dup.call!(env); end
|
50
|
+
|
41
51
|
def call!(env)
|
42
52
|
@r = ::Rack::Request.new(env)
|
43
53
|
@res = ::Rack::Response.new
|
44
|
-
|
54
|
+
@session = env['rack.session'] || {}
|
55
|
+
begin
|
56
|
+
instance_eval(&self.class.dispatcher_block)
|
57
|
+
rescue => e
|
58
|
+
raise if DEV_ENV.include?(ENV['RACK_ENV'])
|
59
|
+
@res.write(self.__send__('error', e, @path_atoms))
|
60
|
+
end
|
45
61
|
@res.status==404&&!@app.nil? ? @app.call(env) : @res.finish
|
46
62
|
end
|
63
|
+
|
47
64
|
def not_found(*args)
|
48
65
|
@res.status = 404
|
49
66
|
@res.headers['X-Cascade']='pass'
|
50
|
-
"
|
67
|
+
"NOT FOUND: #{@r.path_info}"
|
68
|
+
end
|
69
|
+
|
70
|
+
def error(e, *args)
|
71
|
+
@res.status = 500
|
72
|
+
"ERROR"
|
51
73
|
end
|
74
|
+
|
52
75
|
def erb(template)
|
53
76
|
@@tilt_cache ||= {}
|
54
77
|
if @@tilt_cache.has_key?(template)
|
data/rack-golem.gemspec
CHANGED
@@ -1,6 +1,6 @@
|
|
1
1
|
Gem::Specification.new do |s|
|
2
2
|
s.name = 'rack-golem'
|
3
|
-
s.version = "0.0.
|
3
|
+
s.version = "0.0.4"
|
4
4
|
s.platform = Gem::Platform::RUBY
|
5
5
|
s.summary = "A Controller middleware that is euh... basic"
|
6
6
|
s.description = "A Controller middleware that is euh... basic. I would say it is a sort of Ramaze for kids"
|
@@ -9,6 +9,6 @@ Gem::Specification.new do |s|
|
|
9
9
|
s.author = "Mickael Riga"
|
10
10
|
s.email = "mig@campbellhay.com"
|
11
11
|
s.homepage = "http://www.campbellhay.com"
|
12
|
-
s.add_dependency(%q<tilt>, ["
|
12
|
+
s.add_dependency(%q<tilt>, [">= 1.2.2"])
|
13
13
|
s.add_development_dependency(%q<bacon>, "~> 1.1.0")
|
14
14
|
end
|
data/test/spec_golem.rb
CHANGED
@@ -52,44 +52,108 @@ class Indexed
|
|
52
52
|
end
|
53
53
|
IndexedR = ::Rack::MockRequest.new(::Rack::Lint.new(Indexed.new))
|
54
54
|
|
55
|
+
# ==================
|
56
|
+
# = Simply indexed =
|
57
|
+
# ==================
|
58
|
+
|
59
|
+
class SimplyIndexed
|
60
|
+
include Rack::Golem
|
61
|
+
def index; 'index'; end
|
62
|
+
def will_fail; please_fail; end
|
63
|
+
private
|
64
|
+
def please_fail(num); 'ArgumentError baby'; end
|
65
|
+
end
|
66
|
+
SimplyIndexedR = ::Rack::MockRequest.new(::Rack::Lint.new(SimplyIndexed.new))
|
67
|
+
SimplyIndexedUsedR = ::Rack::MockRequest.new(::Rack::Lint.new(SimplyIndexed.new(lambda{|env| [200,{},"#{3+nil}"]})))
|
68
|
+
|
69
|
+
# =============
|
70
|
+
# = Sessioned =
|
71
|
+
# =============
|
72
|
+
|
73
|
+
class Sessioned
|
74
|
+
include Rack::Golem
|
75
|
+
def set_val(val); @session[:val] = val; end
|
76
|
+
def get_val; @session[:val]; end
|
77
|
+
end
|
78
|
+
SessionedR = ::Rack::MockRequest.new(::Rack::Session::Cookie.new(::Rack::Lint.new(Sessioned.new)))
|
79
|
+
|
55
80
|
# =========
|
56
81
|
# = Specs =
|
57
82
|
# =========
|
58
83
|
|
59
84
|
describe "Golem" do
|
85
|
+
|
60
86
|
it "Should dispatch on a method with no arguments" do
|
61
87
|
BasicR.get('/no_arg').body.should=='nothing'
|
62
88
|
end
|
89
|
+
|
63
90
|
it "Should dispatch on a method with arguments" do
|
64
91
|
BasicR.get('/with_args/a/b').body.should=='a+b'
|
65
92
|
end
|
93
|
+
|
66
94
|
it "Should dispatch on a method with splat argument" do
|
67
95
|
BasicR.get('/splat_arg/a/b/c/d').body.should=='a+b+c+d'
|
68
96
|
end
|
97
|
+
|
69
98
|
it "Should not dispatch if the method is private or does not exist" do
|
70
99
|
r = BasicR.get('/no_way')
|
71
100
|
r.status.should==404
|
72
|
-
r.body.should=='
|
101
|
+
r.body.should=='NOT FOUND: /no_way'
|
73
102
|
r = BasicR.get('/no')
|
74
103
|
r.status.should==404
|
75
|
-
r.body.should=='
|
104
|
+
r.body.should=='NOT FOUND: /no'
|
76
105
|
end
|
106
|
+
|
77
107
|
it "Should follow the rack stack if response is 404 and there are middlewares below" do
|
78
108
|
r = BasicLobsterR.get("/no_way")
|
79
109
|
r.status.should==200
|
80
110
|
end
|
111
|
+
|
81
112
|
it "Should provide filters" do
|
82
113
|
FilterR.get('/wrapped').body.should=="before+wrapped+after"
|
83
114
|
end
|
115
|
+
|
84
116
|
it "Should provide arguments in filter when page is not_found" do
|
85
|
-
FilterR.get('/a/b/c/d').body.should=="a+b+c+
|
117
|
+
FilterR.get('/a/b/c/d').body.should=="a+b+c+dNOT FOUND: /a/b/c/d+after"
|
86
118
|
end
|
119
|
+
|
87
120
|
it "Should send everything to :index if it exists and there is no matching method for first arg" do
|
88
121
|
IndexedR.get('/exist/a/b/c/d').body.should=='a+b+c+d'
|
89
122
|
IndexedR.get('/a/b/c/d').body.should=='a+b+c+d'
|
90
123
|
IndexedR.get('/').body.should==''
|
91
124
|
end
|
125
|
+
|
126
|
+
it "Should send not_found if there is an argument error on handlers" do
|
127
|
+
SimplyIndexedR.get('/').status.should==200
|
128
|
+
SimplyIndexedR.get('/unknown').status.should==404
|
129
|
+
SimplyIndexedR.get('/will_fail/useless').status.should==404
|
130
|
+
lambda{ SimplyIndexedR.get('/will_fail') }.should.raise(ArgumentError)
|
131
|
+
end
|
132
|
+
|
133
|
+
it "Should handle errors without raising an exception unless in dev mode" do
|
134
|
+
lambda{ SimplyIndexedR.get('/will_fail') }.should.raise(ArgumentError)
|
135
|
+
ENV['RACK_ENV'] = 'development'
|
136
|
+
lambda{ SimplyIndexedR.get('/will_fail') }.should.raise(ArgumentError)
|
137
|
+
ENV['RACK_ENV'] = 'production'
|
138
|
+
res = SimplyIndexedR.get('/will_fail')
|
139
|
+
res.status.should==500
|
140
|
+
ENV['RACK_ENV'] = nil
|
141
|
+
end
|
142
|
+
|
143
|
+
it "Should not use the error handler if the error occur further down the rack stack" do
|
144
|
+
ENV['RACK_ENV'] = 'production'
|
145
|
+
lambda{ SimplyIndexedUsedR.get('/not_found') }.should.raise(TypeError)
|
146
|
+
ENV['RACK_ENV'] = nil
|
147
|
+
end
|
148
|
+
|
92
149
|
it "Should set dispatch-specific variables correctly when defaulting to :index" do
|
93
150
|
IndexedR.get('/a/b/c/d?switch=true').body.should=="action=index args=a,b,c,d a+b+c+d"
|
94
151
|
end
|
152
|
+
|
153
|
+
it "Should have a shortcut for session hash" do
|
154
|
+
res = SessionedR.get('/set_val/ichigo')
|
155
|
+
res_2 = SessionedR.get('/get_val', 'HTTP_COOKIE'=>res["Set-Cookie"])
|
156
|
+
res_2.body.should=='ichigo'
|
157
|
+
end
|
158
|
+
|
95
159
|
end
|
metadata
CHANGED
@@ -1,13 +1,13 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: rack-golem
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
hash:
|
4
|
+
hash: 23
|
5
5
|
prerelease:
|
6
6
|
segments:
|
7
7
|
- 0
|
8
8
|
- 0
|
9
|
-
-
|
10
|
-
version: 0.0.
|
9
|
+
- 4
|
10
|
+
version: 0.0.4
|
11
11
|
platform: ruby
|
12
12
|
authors:
|
13
13
|
- Mickael Riga
|
@@ -15,8 +15,7 @@ autorequire:
|
|
15
15
|
bindir: bin
|
16
16
|
cert_chain: []
|
17
17
|
|
18
|
-
date:
|
19
|
-
default_executable:
|
18
|
+
date: 2012-03-20 00:00:00 Z
|
20
19
|
dependencies:
|
21
20
|
- !ruby/object:Gem::Dependency
|
22
21
|
name: tilt
|
@@ -24,7 +23,7 @@ dependencies:
|
|
24
23
|
requirement: &id001 !ruby/object:Gem::Requirement
|
25
24
|
none: false
|
26
25
|
requirements:
|
27
|
-
- -
|
26
|
+
- - ">="
|
28
27
|
- !ruby/object:Gem::Version
|
29
28
|
hash: 27
|
30
29
|
segments:
|
@@ -64,7 +63,6 @@ files:
|
|
64
63
|
- lib/rack_golem.rb
|
65
64
|
- rack-golem.gemspec
|
66
65
|
- test/spec_golem.rb
|
67
|
-
has_rdoc: true
|
68
66
|
homepage: http://www.campbellhay.com
|
69
67
|
licenses: []
|
70
68
|
|
@@ -94,7 +92,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
94
92
|
requirements: []
|
95
93
|
|
96
94
|
rubyforge_project:
|
97
|
-
rubygems_version: 1.
|
95
|
+
rubygems_version: 1.8.15
|
98
96
|
signing_key:
|
99
97
|
specification_version: 3
|
100
98
|
summary: A Controller middleware that is euh... basic
|