Last active
August 29, 2015 14:17
-
-
Save shikhalev/8409eef4a4e66a003670 to your computer and use it in GitHub Desktop.
Примеры для статьи про Rack
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require 'pp' | |
app = proc do |env| | |
[ | |
200, | |
{ 'Content-Type' => 'text/plain' }, | |
[ env.pretty_inspect ] | |
] | |
end | |
run app |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require 'pp' | |
app = proc do |env| | |
req = Rack::Request.new env | |
[ | |
200, | |
{ 'Content-Type' => 'text/plain' }, | |
[ req.params.pretty_inspect ] | |
] | |
end | |
run app |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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