Attributy: id="Rack"
Odkazy:
Ukázky kódu:
Rack je konvence.
Máme li ruby object, který má metodu call
app.call(env)
a tato metoda vrací pole se třemi prvky
[200, {'Content-Type' => 'text/plain'}, 'Hello World!']
je možné ento objekt propojit s Rack
require 'thin' Rack::Handler::Thin.run(app, :Port => 4000)
A tímto propojením vznikne wbová aplikace.
app = Proc.new do |env|
[200, { 'Content-Type' => 'text/plain' },
'Hello World!']
end
require 'rubygems'
require 'thin'
Rack::Handler::Thin.run(app, :Port => 4000)Pole které metoda call vrací má tři prvky jenž mají tento váznam.
[200, {'Content-Type' => 'text/plain'}, "Hello #{@name}"]
[ 200, # HTTP status kód
{'Content-Type' => 'text/plain'}, # Hash obsahující HTTP hlavičky které chceme posílat
"Hello #{@name}" # tělo odpovědi, nemusí to být řetězec
]Tělo odpovědi může být objekt který odpovídá na zprávu :each (respond_to?(:each)). Příklad použití:
file = File('myfile.xml')
[200, {'Content-Type' => 'application/xml'}, file]class StreamingFile
def initialize(file)
@file = file
end
def length
@File.size(@file)
end
def last_modified
File.mtime(@file).tfc822
end
def each
File.open(@file, "rb") do |file|
while part = file.read(8192)
yield part
end
File.delete(@file)
end
end