Skip to content

Instantly share code, notes, and snippets.

@adaedra
Created March 11, 2016 21:04
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save adaedra/ad2fe4e35db98e3fee04 to your computer and use it in GitHub Desktop.
Save adaedra/ad2fe4e35db98e3fee04 to your computer and use it in GitHub Desktop.
Rack full example
Application = proc do |env|
names = env['names'] || %w[world]
text = "Hello, #{names.join ', '}!"
[200, {'Content-Type' => 'text/plain'}, [text]]
end
class NameMiddleware
def initialize(app)
@app = app
end
def call(env)
names = env['REQUEST_PATH'].split('/').reject(&:empty?)
env['names'] = names unless names.empty?
@app.call env
end
end
class Blackhole
def initialize(app, regex)
@app = app
@regex = regex
end
def call(env)
if @regex =~ env['REQUEST_PATH']
[404, {'Content-Type' => 'text/plain'}, ['Not found']]
else
@app.call env
end
end
end
use Blackhole, %r{\A/favicon\.ico}
use NameMiddleware
run Application
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment