meddler 0.0.1 → 0.0.2
Sign up to get free protection for your applications and to get access to all the features.
- data/README.rdoc +4 -2
- data/VERSION +1 -1
- data/lib/meddler/builder.rb +28 -3
- data/lib/meddler.rb +32 -17
- metadata +1 -1
data/README.rdoc
CHANGED
@@ -9,14 +9,16 @@ This lets you have on_request and on_response wrappers around a builder, and opt
|
|
9
9
|
In your rackup file, do this:
|
10
10
|
|
11
11
|
require 'meddler'
|
12
|
-
use Meddler::Builder
|
12
|
+
use Meddler::Builder do
|
13
|
+
on_request {|req| req.post?}
|
13
14
|
use MungePostMiddleware
|
14
15
|
end
|
15
16
|
|
16
17
|
Or, on response
|
17
18
|
|
18
19
|
require 'meddler'
|
19
|
-
use Meddler::Builder
|
20
|
+
use Meddler::Builder do
|
21
|
+
on_response {|resp| resp.status == 200}
|
20
22
|
use MiddlewareThatLikesOkThings
|
21
23
|
end
|
22
24
|
|
data/VERSION
CHANGED
@@ -1 +1 @@
|
|
1
|
-
0.0.
|
1
|
+
0.0.2
|
data/lib/meddler/builder.rb
CHANGED
@@ -3,10 +3,35 @@ class Meddler
|
|
3
3
|
|
4
4
|
def initialize(app, rules = nil, &block)
|
5
5
|
@app = app
|
6
|
-
@target = Rack::Builder.new
|
7
|
-
|
6
|
+
@target = Rack::Builder.new{}
|
7
|
+
instance_eval(&block)
|
8
|
+
@meddler = Meddler.new(@app, @on_request, @on_response, @before, @after, @target)
|
8
9
|
end
|
9
|
-
|
10
|
+
|
11
|
+
def method_missing(method, *args, &block)
|
12
|
+
@target.send(method, *args, &block)
|
13
|
+
end
|
14
|
+
|
15
|
+
def on_request(&block)
|
16
|
+
@on_request ||= []
|
17
|
+
@on_request << block
|
18
|
+
end
|
19
|
+
|
20
|
+
def on_response(&block)
|
21
|
+
@on_response ||= []
|
22
|
+
@on_response << block
|
23
|
+
end
|
24
|
+
|
25
|
+
def before(&block)
|
26
|
+
@before ||= []
|
27
|
+
@before << block
|
28
|
+
end
|
29
|
+
|
30
|
+
def after(&block)
|
31
|
+
@after ||= []
|
32
|
+
@after << block
|
33
|
+
end
|
34
|
+
|
10
35
|
def call(env)
|
11
36
|
@meddler.call(env)
|
12
37
|
end
|
data/lib/meddler.rb
CHANGED
@@ -5,13 +5,17 @@ require 'meddler/builder'
|
|
5
5
|
|
6
6
|
class Meddler
|
7
7
|
|
8
|
-
def initialize(app,
|
9
|
-
wrapped_app.run(PostInterceptor.new(app,
|
10
|
-
@app = PreInterceptor.new(wrapped_app.to_app, app,
|
8
|
+
def initialize(app, on_request, on_response, before, after, wrapped_app)
|
9
|
+
wrapped_app.run(PostInterceptor.new(app, on_response, after, signal))
|
10
|
+
@app = PreInterceptor.new(wrapped_app.to_app, app, on_request, before)
|
11
|
+
end
|
12
|
+
|
13
|
+
def signal
|
14
|
+
self.object_id.to_s.to_sym
|
11
15
|
end
|
12
16
|
|
13
17
|
def call(env)
|
14
|
-
response = catch(
|
18
|
+
response = catch(signal) do
|
15
19
|
@app.call(env)
|
16
20
|
end
|
17
21
|
response
|
@@ -19,33 +23,44 @@ class Meddler
|
|
19
23
|
|
20
24
|
|
21
25
|
class PostInterceptor
|
22
|
-
|
26
|
+
attr_reader :app, :rules, :filters, :signal
|
27
|
+
def initialize(app, rules, filters, signal)
|
23
28
|
@app = app
|
24
|
-
@
|
29
|
+
@rules = rules
|
30
|
+
@filters = filters
|
31
|
+
@signal = signal
|
25
32
|
end
|
26
33
|
|
27
34
|
def call(env)
|
28
|
-
|
29
|
-
|
30
|
-
|
35
|
+
raw_response = app.call(env)
|
36
|
+
response = Rack::Response.new(raw_response[2], raw_response[0], raw_response[1])
|
37
|
+
if rules.nil? || rules.all?{|r| r.call(response)}
|
38
|
+
filters && filters.each{|f| f.call(response)}
|
39
|
+
response.to_a
|
31
40
|
else
|
32
|
-
throw
|
41
|
+
throw signal, response.to_a
|
33
42
|
end
|
34
43
|
end
|
35
44
|
|
36
45
|
end
|
37
|
-
|
46
|
+
|
38
47
|
class PreInterceptor
|
39
|
-
|
40
|
-
|
41
|
-
def initialize(app, skip_app, rule = nil)
|
48
|
+
attr_reader :app, :skip_app, :rules, :filters
|
49
|
+
def initialize(app, skip_app, rules, filters)
|
42
50
|
@app = app
|
43
51
|
@skip_app = skip_app
|
44
|
-
@
|
52
|
+
@rules = rules
|
53
|
+
@filters = filters
|
45
54
|
end
|
46
|
-
|
55
|
+
|
47
56
|
def call(env)
|
48
|
-
|
57
|
+
request = Rack::Request.new(env)
|
58
|
+
if rules.nil? || rules.all?{|f| f.call(request)}
|
59
|
+
filters && filters.each{|f| f.call(request)}
|
60
|
+
app.call(env)
|
61
|
+
else
|
62
|
+
skip_app.call(env)
|
63
|
+
end
|
49
64
|
end
|
50
65
|
end
|
51
66
|
|