Module Web
In: lib/web.rb
lib/web/writableio.rb
lib/web/wiki.rb
lib/web/traceoutput.rb
lib/web/testing.rb
lib/web/template.rb
lib/web/simpledispatcher.rb
lib/web/session.rb
lib/web/request.rb
lib/web/forms.rb
lib/web/buffer.rb
lib/web/action.rb

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
Methods
<<    []    charset=    content_type    content_type=    encode_objects    encoding=    escape    flush    html_encode    location=    print    print_template    puts    rfc1123_date    set_cookie    set_redirect    status    status=    typed_params    unescape    write   
Classes and Modules
Module Web::SimpleDispatcher
  ::Module Web::SimpleDispatcher::TemplateClassMixin
  ::Module Web::SimpleDispatcher::TemplateMixin
  ::Class Web::SimpleDispatcher::Link
  ::Class Web::SimpleDispatcher::Template
Module Web::Testing
  ::Class Web::Testing::FieldNotFoundException
  ::Class Web::Testing::FormNotFoundException
Class Web::Narflates
Class Web::Upload
Class Web::Wiki
  ::Module Web::Wiki::Store
  ::Class Web::Wiki::Request
  ::  ::Class Web::Wiki::Request::Action
Public Class methods
[](key)

Returns the value for key in the query string, or form submission. If the key ends in [], an array is returned.

print_template(template, vars, query = {})

Prints a Narflates template, given a template name and values to populate the template with.

<<(object)

Output to web browser

puts(*args)

Output to web browser

write(*args)

Output to web browser

print(*args)

Output to web browser

flush()

send headers to the client, and flush any buffered output

set_cookie( name, value, options={} )

Cookies require a name and a value. You can also use these optional keyword arguments:

:path => <i>string<i>:path (need better description)
:domain => string:domain (need better description)
:expires => date:date this cookie should expire
:secure => true || false :whether this cookie should be tagged as secure.
status()

the default status is 200

content_type()

the default content-type is "text/html"

set_redirect( new_location )

Sets the status and the location appropriately.

encoding=(encoding)
status=(status)
location=(location)
charset=(charset)
content_type=(content_type)
html_encode(string)

this is a bit incomplete — only encodes ampersands

escape(string)

Escape URL encode

    url_encoded_string = Web::escape("string")
unescape(string)

Unescape URL encoded

    string = Web::unescape("url encoded string")
rfc1123_date(time)

Make RFC1123 date string

    Web::rfc1123_date(Time.now) # => Sat, 01 Jan 2000 00:00:00 GMT
encode_objects(hash)
typed_params()