meddler 0.0.1
Sign up to get free protection for your applications and to get access to all the features.
- data/README.rdoc +22 -0
- data/Rakefile +17 -0
- data/VERSION +1 -0
- data/lib/meddler/builder.rb +15 -0
- data/lib/meddler.rb +52 -0
- metadata +59 -0
data/README.rdoc
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
= Meddler
|
2
|
+
|
3
|
+
== Hey, someone meddled with my middleware!
|
4
|
+
|
5
|
+
This lets you have on_request and on_response wrappers around a builder, and optionally skip them in either case.
|
6
|
+
|
7
|
+
== Usage
|
8
|
+
|
9
|
+
In your rackup file, do this:
|
10
|
+
|
11
|
+
require 'meddler'
|
12
|
+
use Meddler::Builder, :on_request => proc{|req| req.post?} do
|
13
|
+
use MungePostMiddleware
|
14
|
+
end
|
15
|
+
|
16
|
+
Or, on response
|
17
|
+
|
18
|
+
require 'meddler'
|
19
|
+
use Meddler::Builder, :on_response => proc{|resp| resp.status == 200} do
|
20
|
+
use MiddlewareThatLikesOkThings
|
21
|
+
end
|
22
|
+
|
data/Rakefile
ADDED
@@ -0,0 +1,17 @@
|
|
1
|
+
# encoding: utf-8
|
2
|
+
|
3
|
+
begin
|
4
|
+
require 'jeweler'
|
5
|
+
Jeweler::Tasks.new do |s|
|
6
|
+
s.name = "meddler"
|
7
|
+
s.description = s.summary = "Hey, someone meddled with my middleware!"
|
8
|
+
s.email = "joshbuddy@gmail.com"
|
9
|
+
s.homepage = "http://github.com/joshbuddy/meddler"
|
10
|
+
s.authors = ["Joshua Hull"].sort
|
11
|
+
s.files = FileList["[A-Z]*", "{lib}/**/*"]
|
12
|
+
end
|
13
|
+
Jeweler::GemcutterTasks.new
|
14
|
+
rescue LoadError
|
15
|
+
puts "Jeweler not available. Install it with: sudo gem install technicalpickles-jeweler -s http://gems.github.com"
|
16
|
+
end
|
17
|
+
|
data/VERSION
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
0.0.1
|
@@ -0,0 +1,15 @@
|
|
1
|
+
class Meddler
|
2
|
+
class Builder
|
3
|
+
|
4
|
+
def initialize(app, rules = nil, &block)
|
5
|
+
@app = app
|
6
|
+
@target = Rack::Builder.new(&block)
|
7
|
+
@meddler = Meddler.new(@app, rules && rules[:on_request], rules && rules[:on_response], @target)
|
8
|
+
end
|
9
|
+
|
10
|
+
def call(env)
|
11
|
+
@meddler.call(env)
|
12
|
+
end
|
13
|
+
|
14
|
+
end
|
15
|
+
end
|
data/lib/meddler.rb
ADDED
@@ -0,0 +1,52 @@
|
|
1
|
+
require 'rack'
|
2
|
+
|
3
|
+
$: << File.dirname(__FILE__)
|
4
|
+
require 'meddler/builder'
|
5
|
+
|
6
|
+
class Meddler
|
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)
|
11
|
+
end
|
12
|
+
|
13
|
+
def call(env)
|
14
|
+
response = catch(:skipped_middleware) do
|
15
|
+
@app.call(env)
|
16
|
+
end
|
17
|
+
response
|
18
|
+
end
|
19
|
+
|
20
|
+
|
21
|
+
class PostInterceptor
|
22
|
+
def initialize(app, rule = nil)
|
23
|
+
@app = app
|
24
|
+
@rule = rule
|
25
|
+
end
|
26
|
+
|
27
|
+
def call(env)
|
28
|
+
response = @app.call(env)
|
29
|
+
if @rule.nil? || @rule.call(Rack::Response.new(response))
|
30
|
+
response
|
31
|
+
else
|
32
|
+
throw :skipped_middleware, response
|
33
|
+
end
|
34
|
+
end
|
35
|
+
|
36
|
+
end
|
37
|
+
|
38
|
+
class PreInterceptor
|
39
|
+
attr_accessor :skip_app
|
40
|
+
|
41
|
+
def initialize(app, skip_app, rule = nil)
|
42
|
+
@app = app
|
43
|
+
@skip_app = skip_app
|
44
|
+
@rule = rule
|
45
|
+
end
|
46
|
+
|
47
|
+
def call(env)
|
48
|
+
response = @rule.nil? || @rule.call(Rack::Request.new(env)) ? @app.call(env) : @skip_app.call(env)
|
49
|
+
end
|
50
|
+
end
|
51
|
+
|
52
|
+
end
|
metadata
ADDED
@@ -0,0 +1,59 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: meddler
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.1
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Joshua Hull
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
|
12
|
+
date: 2009-11-30 00:00:00 -05:00
|
13
|
+
default_executable:
|
14
|
+
dependencies: []
|
15
|
+
|
16
|
+
description: Hey, someone meddled with my middleware!
|
17
|
+
email: joshbuddy@gmail.com
|
18
|
+
executables: []
|
19
|
+
|
20
|
+
extensions: []
|
21
|
+
|
22
|
+
extra_rdoc_files:
|
23
|
+
- README.rdoc
|
24
|
+
files:
|
25
|
+
- README.rdoc
|
26
|
+
- Rakefile
|
27
|
+
- VERSION
|
28
|
+
- lib/meddler.rb
|
29
|
+
- lib/meddler/builder.rb
|
30
|
+
has_rdoc: true
|
31
|
+
homepage: http://github.com/joshbuddy/meddler
|
32
|
+
licenses: []
|
33
|
+
|
34
|
+
post_install_message:
|
35
|
+
rdoc_options:
|
36
|
+
- --charset=UTF-8
|
37
|
+
require_paths:
|
38
|
+
- lib
|
39
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
40
|
+
requirements:
|
41
|
+
- - ">="
|
42
|
+
- !ruby/object:Gem::Version
|
43
|
+
version: "0"
|
44
|
+
version:
|
45
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
46
|
+
requirements:
|
47
|
+
- - ">="
|
48
|
+
- !ruby/object:Gem::Version
|
49
|
+
version: "0"
|
50
|
+
version:
|
51
|
+
requirements: []
|
52
|
+
|
53
|
+
rubyforge_project:
|
54
|
+
rubygems_version: 1.3.5
|
55
|
+
signing_key:
|
56
|
+
specification_version: 3
|
57
|
+
summary: Hey, someone meddled with my middleware!
|
58
|
+
test_files: []
|
59
|
+
|