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 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, :on_request => proc{|req| req.post?} do
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, :on_response => proc{|resp| resp.status == 200} do
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
1
+ 0.0.2
@@ -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(&block)
7
- @meddler = Meddler.new(@app, rules && rules[:on_request], rules && rules[:on_response], @target)
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, before_rule, after_rule, wrapped_app)
9
- wrapped_app.run(PostInterceptor.new(app, after_rule))
10
- @app = PreInterceptor.new(wrapped_app.to_app, app, before_rule)
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(:skipped_middleware) do
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
- def initialize(app, rule = nil)
26
+ attr_reader :app, :rules, :filters, :signal
27
+ def initialize(app, rules, filters, signal)
23
28
  @app = app
24
- @rule = rule
29
+ @rules = rules
30
+ @filters = filters
31
+ @signal = signal
25
32
  end
26
33
 
27
34
  def call(env)
28
- response = @app.call(env)
29
- if @rule.nil? || @rule.call(Rack::Response.new(response))
30
- response
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 :skipped_middleware, response
41
+ throw signal, response.to_a
33
42
  end
34
43
  end
35
44
 
36
45
  end
37
-
46
+
38
47
  class PreInterceptor
39
- attr_accessor :skip_app
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
- @rule = rule
52
+ @rules = rules
53
+ @filters = filters
45
54
  end
46
-
55
+
47
56
  def call(env)
48
- response = @rule.nil? || @rule.call(Rack::Request.new(env)) ? @app.call(env) : @skip_app.call(env)
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
 
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: meddler
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.1
4
+ version: 0.0.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - Joshua Hull