Skip to content

Instantly share code, notes, and snippets.

@shikhalev
Last active August 29, 2015 14:17
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 shikhalev/8409eef4a4e66a003670 to your computer and use it in GitHub Desktop.
Save shikhalev/8409eef4a4e66a003670 to your computer and use it in GitHub Desktop.
Примеры для статьи про 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