Purpose
Web::CGI provides all the low level infrastructure for building and testing
Web applications. Use ’/usr/bin/narf/’
instead of using the ruby executable directly to provide error reporting to
the the Web browser.
Usage
All methods for the current CGI are available on the Web module. It is suggested that you use the narf
runner, which provides error reporting in the browser.
#!/usr/bin/narf
Web["field"] # access parameters
Web.cookies["muppet"] # access cookies
Web.session["key"] ||= "value" # get and set session values
Web.set_cookie( "muppet", "cookie monster" ) # set cookies
Web.puts "something" # print 'something' out
If the request contained multipart/form-data, uploaded files are returned
as Web::Upload objects. Other parameters are
treated as strings.
Output
You write output as if it were an IO:
Web << "<h1>Hello World!</h1>"
Web.print "<p>We have lots of tricks for you.<br>"
Web.write "From kungfu to jiujitsu<br>"
Web.puts "anything and everything works</p>."
Narf buffers output by default. Buffered output allows you to set headers
(cookies, redirects, status, etc.) at any point before the cgi is flushed.
You can force this by calling:
Web.flush
Once the headers are sent to the client, you cannot set any more headers.
Sessions:
Sessions provide a way of storing data between Web
requests. Anything that can be marshalled can be stored in the session.
Getting and Setting values in a session
Web.session["key"] = "value"
Web.session["key"] # => "value"
Deleting session values
Web.session["key"] = "value"
Web.session["key"].delete # => "value"
Web.session["key"] # => "nil"
Iterating through session keys
Web.session.each do |key, val|
Web.puts "#{key} => #{val}<br>"
end
Resetting the Session
Reseting the session to an empty hash is simple:
Web.session.reset