Status Codeを設定するstatusヘルパー、headerを設定するheadersヘルパーも用意されています。
1234567
get'/foo'dostatus418headers\"Allow"=>"BREW, POST, GET, PROPFIND, WHEN","Refresh"=>"Refresh: 20; http://www.ietf.org/rfc/rfc2324.txt"body"I'm a tea pot!"end
get'/'dostreamdo|out|out<<"It's gonna be legen -\n"sleep0.5out<<" (wait for it) \n"sleep1out<<"- dary!\n"endend
以下は、ストリーミングAPIのサンプルです。
123456789101112131415161718192021222324252627
# app.rb# long pollingset:server,:thinconnections=[]get'/subscribe'do# register a client's interest in server eventsstream(:keep_open)do|out|connections<<out# purge dead connectionsconnections.reject!(&:closed?)endendpost'/message'doconnections.eachdo|out|# notify client that a new message has arrivedout<<params['message']<<"\n"# indicate client to connect againout.closeend# acknowledge"message received"end