Skip to content

Instantly share code, notes, and snippets.

@RyanScottLewis
Created March 8, 2014 07:51
Show Gist options
  • Save RyanScottLewis/9426937 to your computer and use it in GitHub Desktop.
Save RyanScottLewis/9426937 to your computer and use it in GitHub Desktop.
require 'pp'
class Middleware
def initialize(app)
@app = app
puts "=-=-=- Initializing #{self.class}"
pp app
end
def call(environment)
puts "=-=-=- Calling #{self.class}"
end
end
# Bold body
class FirstMiddleware < Middleware
def call(environment)
super
status, headers, body = @app.call(environment)
body = ["<b>#{body.first}</b>"]
[ status, headers, body ]
end
end
# Italic body
class SecondMiddleware < Middleware
def call(environment)
super
status, headers, body = @app.call(environment)
body = ["<i>#{body.first}</i>"]
[ status, headers, body ]
end
end
class Application
def initialize
puts "=-=-=- Initializing #{self.class}"
end
def call(environment)
puts "=-=-=- Calling #{self.class}"
[ 200, {}, ['Hello!'] ]
end
end
use FirstMiddleware
use SecondMiddleware
run Application.new
Puma starting in single mode...
* Version 2.8.1 (ruby 2.1.0-p0), codename: Sir Edmund Percival Hillary
* Min threads: 0, max threads: 16
* Environment: development
=-=-=- Initializing Application
=-=-=- Initializing SecondMiddleware
#<Application:0x000001013ef4e8>
=-=-=- Initializing FirstMiddleware
#<SecondMiddleware:0x000001013ef380 @app=#<Application:0x000001013ef4e8>>
* Listening on tcp://0.0.0.0:9292
Use Ctrl-C to stop
(in another terminal) $ curl http://localhost:9292
=-=-=- Calling FirstMiddleware
=-=-=- Calling SecondMiddleware
=-=-=- Calling Application
127.0.0.1 - - [08/Mar/2014 02:48:44] "GET / HTTP/1.1" 200 - 0.0006
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment