Ruby CGI Useful Methods

Advertisements


CGI Class Methods:

Here is a list of CGI Class methods:

SNMethods with Description
1CGI::new([ level="query"])
Creates a CGI object. level may be one of the following options. If one of the HTML levels is specified, the following methods are defined for generating output conforming to that level:
  • query: No HTML output generated

  • html3: HTML3.2

  • html4: HTML4.0 Strict

  • html4Tr: HTML4.0 Transitional

  • html4Fr: HTML4.0 Frameset

2CGI::escape( str)
Escapes an unsafe string using URL-encoding.
3CGI::unescape( str)
Expands a string that has been escaped using URL-encoding.
4CGI::escapeHTML( str)
Escapes HTML special characters, including: & < >.
5CGI::unescapeHTML( str)
Expands escaped HTML special characters, including: & < >.
6CGI::escapeElement( str[, element...])
Escapes HTML special characters in the specified HTML elements.
7CGI::unescapeElement( str, element[, element...])
Expands escaped HTML special characters in the specified HTML elements.
8CGI::parse( query)
Parses the query and returns a hash containing its key-value pairs.
9CGI::pretty( string[, leader=" "])
Returns a neatly formatted version of the HTML string. If leader is specified, it's written at the beginning of each line. The default value for leader is two spaces.
10CGI::rfc1123_date( time)
Formats the data and time according to RFC-1123 (for example, Tue, 2 Jun 2008 00:00:00 GMT).

CGI Instance Methods:

Assuming c is an instance created by CGI::new. Now, here is a list of methods, which can be applied to this instance:

SNMethods with Description
1c[ name]
Returns an array containing the value of the field name corresponding to name.
2c.checkbox( name[, value[, check=false]])
c.checkbox( options)

Returns an HTML string defining a checkbox field. Tag attributes may be specified in a hash passed as an argument.
3c.checkbox_group( name, value...)
c.checkbox_group( options)

Returns an HTML string defining a checkbox group. Tag attributes may be specified in a hash passed as an argument.
4c.file_field( name[, size=20[, max]])
c.file_field( options)

Returns an HTML string defining a file field.
5c.form([ method="post"[, url]]) { ...}
c.form( options)

Returns an HTML string defining a form. If a block is specified, the string produced by its output creates the contents of the form. Tag attributes may be specified in a hash passed as an argument.
6c.cookies
Returns a hash containing a CGI::Cookie object containing keys and values from a cookie.
7c.header([ header])
Returns a CGI header containing the information in header. If header is a hash, its key-value pairs are used to create the header.
8c.hidden( name[, value])
c.hidden( options)

Returns an HTML string defining a HIDDEN field. Tag attributes may be specified in a hash passed as an argument.
9c.image_button( url[, name[, alt]])
c.image_button( options)

Returns an HTML string defining an image button. Tag attributes may be specified in a hash passed as an argument.
10c.keys
Returns an array containing the field names from the form.
11c.key?( name)
c.has_key?( name)
c.include?( name)

Returns true if the form contains the specified field name.
12c.multipart_form([ url[, encode]]) { ...}
c.multipart_form( options) { ...}

Returns an HTML string defining a multipart form. If a block is specified, the string produced by its output creates the contents of the form. Tag attributes may be specified in a hash passed as an argument.
13c.out([ header]) { ...}
Generates HTML output. Uses the string produced by the block's output to create the body of the page.
14c.params
Returns a hash containing field names and values from the form.
15c.params= hash
Sets field names and values in the form using a hash.
16c.password_field( name[, value[, size=40[, max]]])
c.password_field( options)

Returns an HTML string defining a password field. Tag attributes may be specified in a hash passed as an argument.
17c.popup_menu( name, value...)
c.popup_menu( options)
c.scrolling_list( name, value...)
c.scrolling_list( options)

Returns an HTML string defining a pop-up menu. Tag attributes may be specified in a hash passed as an argument.
18c.radio_button( name[, value[, checked=false]])
c.radio_button( options)

Returns an HTML string defining a radio button. Tag attributes may be specified in a hash passed as an argument.
19c.radio_group( name, value...)
c.radio_group( options)

Returns an HTML string defining a radio button group. Tag attributes may be specified in a hash passed as an argument.
20c.reset( name[, value])
c.reset( options)

Returns an HTML string defining a reset button. Tag attributes may be specified in a hash passed as an argument.
21c.text_field( name[, value[, size=40[, max]]])
c.text_field( options)

Returns an HTML string defining a text field. Tag attributes may be specified in a hash passed as an argument.
22c.textarea( name[, cols=70[, rows=10]]) { ...}
c.textarea( options) { ...}

Returns an HTML string defining a text area. If a block is specified, the string produced by its output creates the contents of the text area. Tag attributes may be specified in a hash passed as an argument.

HTML Generation Methods:

You can create any HTML tag by using corresponding HTML tag name along with any CGI instance. For example:

#!/usr/bin/ruby

require "cgi"
cgi = CGI.new("html4")
cgi.out{
   cgi.html{
      cgi.head{ "\n"+cgi.title{"This Is a Test"} } +
      cgi.body{ "\n"+
         cgi.form{"\n"+
            cgi.hr +
            cgi.h1 { "A Form: " } + "\n"+
            cgi.textarea("get_text") +"\n"+
            cgi.br +
            cgi.submit
         }
      }
   }
}

CGI Object Attributes:

You can access any of the following attributes using a CGI instance:

AttributeReturned Value
acceptAcceptable MIME type
accept_charsetAcceptable character set
accept_encodingAcceptable encoding
accept_languageAcceptable language
auth_typeAuthentication type
raw_cookieCookie data (raw string)
content_lengthContent length
content_typeContent type
FromClient e-mail address
gateway_interfaceCGI version string
path_infoExtra path
path_translatedConverted extra path
Query_stringQuery string
refererPreviously accessed URL
remote_addrClient host address
remote_hostClient hostname
remote_identClient name
remote_userAuthenticated user
request_methodRequest method (GET, POST, etc.)
script_nameProgram name
server_nameServer name
server_portServer port
server_protocolServer protocol
server_softwareServer software
user_agentUser agent


Advertisements
Advertisements