Skip to content

Instantly share code, notes, and snippets.

@melborne
Created August 3, 2012 01:51
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save melborne/3243357 to your computer and use it in GitHub Desktop.
Save melborne/3243357 to your computer and use it in GitHub Desktop.
Joke Rack Web framework `Draque`
# config.ru
require "./draque"
class UpDown
def initialize(app)
@app = app
end
def call(env)
status, headers, body = @app.call(env)
[status, headers, body.reverse]
end
end
class Fire
def initialize(app, pattern)
@app = app
@pattern = pattern
end
def call(env)
status, headers, body = @app.call(env)
replace = ->pat{ "<em style='background-color:red'>#{pat}</em>" }
new_body = body.inject([]) { |m, part| m << part.gsub(@pattern) { replace[$&] } }
[status, headers, new_body]
end
end
use Fire, /rack|draque/i
use UpDown
run method(:draque)
module Draque
@@routes = { get:{} }
def draque(env)
path = env['PATH_INFO']
if res = @@routes[:get][path]
res.call(env)
else
[ 404, headers, not_found ]
end
end
def get(path, &blk)
@@routes[:get][path] = blk
end
end
Object.send(:include, Draque)
get '/draque' do
[ 200, headers, draque_body ]
end
get '/' do |env|
[ 200, headers, top_body(env) ]
end
def headers
{'Content-Type' => 'text/html'}
end
def top_body(env)
["<h1>Welcome to the World of Draque!!</h1>"] +
env.map { |k,v| "<p>%s => %s</p>" % [k, v] }
end
def draque_body
["<img src='http://www.dqx.jp/storage/img/top/main_visual.png'>"]
end
def not_found
["<img src='https://a248.e.akamai.net/assets.github.com/images/modules/404/parallax_octocat.png?1329921026'>", "<img src='https://a248.e.akamai.net/assets.github.com/images/modules/404/parallax_errortext.png?1329921026'>"]
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment