Skip to content

Instantly share code, notes, and snippets.

@shikhalev

shikhalev/demo01.rb

Last active Aug 29, 2015
Embed
What would you like to do?
Примеры для статьи про Rack
# encoding: utf-8
require 'pp'
require 'rack'
app = proc do |env|
[
200,
{ 'Content-Type' => 'text/plain' },
[ env.pretty_inspect ]
]
end
Rack::Handler::WEBrick.run app
require 'pp'
app = proc do |env|
[
200,
{ 'Content-Type' => 'text/plain' },
[ env.pretty_inspect ]
]
end
run app
require 'pp'
app = proc do |env|
req = Rack::Request.new env
[
200,
{ 'Content-Type' => 'text/plain' },
[ req.params.pretty_inspect ]
]
end
run app
require 'pp'
app = proc do |env|
req = Rack::Request.new env
res = Rack::Response.new
res['Content-Type'] = 'text/plain'
res.write req.params.pretty_inspect
res.finish
end
run app
require 'pp'
require 'json'
class Log
def initialize app, output = $stderr
@app = app
@output = output
end
def call env
@output.puts env.pretty_inspect
@app.call env
end
end
json = proc do |env|
[
200,
{ 'Content-Type' => 'application/json' },
[ JSON.generate(env) ]
]
end
txt = proc do |env|
[
200,
{ 'Content-Type' => 'text/plain' },
[ env.pretty_inspect ]
]
end
app = Rack::Builder.app do
use Log
map '/js/' do
run json
end
run txt
end
run app
require 'pp'
require 'json'
class Log
def initialize app, output = $stderr
@app = app
@output = output
end
def call env
@output.puts env.pretty_inspect
@app.call env
end
end
json = proc do |env|
[
200,
{ 'Content-Type' => 'application/json' },
[ JSON.generate(env) ]
]
end
txt = proc do |env|
[
200,
{ 'Content-Type' => 'text/plain' },
[ env.pretty_inspect ]
]
end
use Log
map '/js/' do
run json
end
run txt
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment