• No results found

CGI scripts using Ruby Language

N/A
N/A
Protected

Academic year: 2022

Share "CGI scripts using Ruby Language"

Copied!
21
0
0

Loading.... (view fulltext now)

Full text

(1)

CGI scripts using Ruby Language

Solanki Gautam V. (04305027)

Department of Computer Science

November 27, 2004

Solanki Gautam V. (04305027) Indian Institute Of Technology Bombay

CGI scripts using Ruby Language

(2)

Outline of Presentation

What is Ruby Language ? A simple Ruby Program

What is Common Gateway Interface (CGI)?

Why to use Ruby for writing CGI scripts ? Writing CGI Scripts using Ruby....

Examples References

Solanki Gautam V. (04305027) Indian Institute Of Technology Bombay

CGI scripts using Ruby Language

(3)

What is Ruby Language ?

Ruby is a pure object-oriented scripting language, developed by Yukihiro Matsumoto in Japan. It was primarily designed to handle text processing and systems management tasks. Not only can you write your own SMTP server, FTP daemon, or Web server in Ruby, but you can also use Ruby for more usual tasks such as CGI programming or as a replacement for PHP.

Solanki Gautam V. (04305027) Indian Institute Of Technology Bombay

CGI scripts using Ruby Language

(4)

A simple Ruby Program

#!/usr/bin/ruby

$var = "Ruby Languages\n"

print "CS701 Assignment\n"

print $var

Solanki Gautam V. (04305027) Indian Institute Of Technology Bombay

CGI scripts using Ruby Language

(5)

What is Common Gateway Interface (CGI)?

CGI is not a language. It’s a simple protocol that can be used to communicate between Web forms and your program. A CGI script can be written in any language that can read STDIN, write to STDOUT, and read environment variables, i.e. virtually any programming language, including C, Perl, or even shell scripting.

Solanki Gautam V. (04305027) Indian Institute Of Technology Bombay

CGI scripts using Ruby Language

(6)

Structure of a CGI Script

Here’s the typical sequence of steps for a CGI script:

1 Read the user’s form input.

When the user submits the form, your script receives the form data as a set of name-value pairs. The names are what you defined in the INPUT tags (or SELECT or TEXTAREA tags), and the values are whatever the user typed in or selected.

2 Do what you want with the data.

Solanki Gautam V. (04305027) Indian Institute Of Technology Bombay

CGI scripts using Ruby Language

(7)

Structure of a CGI Script

Here’s the typical sequence of steps for a CGI script:

1 Read the user’s form input.

When the user submits the form, your script receives the form data as a set of name-value pairs. The names are what you defined in the INPUT tags (or SELECT or TEXTAREA tags), and the values are whatever the user typed in or selected.

2 Do what you want with the data.

Solanki Gautam V. (04305027) Indian Institute Of Technology Bombay

CGI scripts using Ruby Language

(8)

Structure of a CGI Script(Cont.)

1 Write the HTML response to STDOUT. First, write the line Content-type: text/html

plus another blank line, to STDOUT. After that, write your HTML response page to STDOUT, and it will be sent to the user when your script is done. That’s all there is to it.

Solanki Gautam V. (04305027) Indian Institute Of Technology Bombay

CGI scripts using Ruby Language

(9)

Structure of a CGI Script(Cont.)

1 Write the HTML response to STDOUT. First, write the line Content-type: text/html

plus another blank line, to STDOUT. After that, write your HTML response page to STDOUT, and it will be sent to the user when your script is done. That’s all there is to it.

Solanki Gautam V. (04305027) Indian Institute Of Technology Bombay

CGI scripts using Ruby Language

(10)

Structure of a CGI Script(Cont.)

1 Write the HTML response to STDOUT. First, write the line Content-type: text/html

plus another blank line, to STDOUT. After that, write your HTML response page to STDOUT, and it will be sent to the user when your script is done. That’s all there is to it.

Solanki Gautam V. (04305027) Indian Institute Of Technology Bombay

CGI scripts using Ruby Language

(11)

Why to use Ruby for writing CGI scripts ?

Reasons

Because it has a rich set of libraries. There is support for threads, sockets, limited object persistence, CGI programs, server-side executables, DB files, and more. There is some support for Tk, with more on the way.

Solanki Gautam V. (04305027) Indian Institute Of Technology Bombay

CGI scripts using Ruby Language

(12)

Writing CGI Scripts using Ruby....

To have a Ruby script generate HTML output, all you need is

#!/usr/bin/env ruby print "HTTP/1.0 200 OK"

print "Content-type: text/html"

print "<html><body>CS-701 ASSIGNMENT</body></html>"

Solanki Gautam V. (04305027) Indian Institute Of Technology Bombay

CGI scripts using Ruby Language

(13)

Example 1

Creating Forms and HTML using Ruby Program

require "cgi"

cgi = CGI.new("html3") # add HTML generation methods 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}

} } }

Solanki Gautam V. (04305027) Indian Institute Of Technology Bombay

CGI scripts using Ruby Language

(14)

Output of Example 1

produces:

Content-Type: text/html Content-Length: 302

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 3.2 Final//EN"><HTML><HEAD>

<TITLE>This Is a Test</TITLE></HEAD><BODY>

<FORM METHOD="post" ENCTYPE="application/x-www-form-urlencoded">

<HR><H1>A Form: </H1>

<TEXTAREA NAME="get_text" ROWS="10" COLS="70"></TEXTAREA>

<BR><INPUT TYPE="submit"></FORM></BODY></HTML>

Solanki Gautam V. (04305027) Indian Institute Of Technology Bombay

CGI scripts using Ruby Language

(15)

Example 2

Cookies

You can store all kinds of interesting stuff on an unsuspecting surfer’s machine using cookies. You can create a named cookie object and store a number of values in it. To send it down to the browser, set a “cookie” header in the call to CGI

#out.

require "cgi"

cookie = CGI::Cookie.new("rubyweb", "CustID=123", "Part=ABC");

cgi = CGI.new("html3")

cgi.out( "cookie" => [cookie] ){

cgi.html{

"\nHTML content here"

}

Solanki Gautam V. (04305027) Indian Institute Of Technology Bombay

CGI scripts using Ruby Language

(16)

Output of Example 2

produces:

Content-Type: text/html Content-Length: 86

Set-Cookie: rubyweb=CustID\%3D123&Part\%3DABC; path=

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 3.2 Final//EN"><HTML>

HTML content here</HTML>

Solanki Gautam V. (04305027) Indian Institute Of Technology Bombay

CGI scripts using Ruby Language

(17)

Example 3

Cookies by themselves still need a bit of work to be useful. What we really want is a session: a persistent state for some Web surfer.

Sessions are handled with CGI::Session , which uses cookies but provides a higher-level abstraction.

require "cgi"

require "cgi/session"

cgi = CGI.new("html3")

sess = CGI::Session.new( cgi, "session_key" => "rubyweb",

"session_id" => "9650", "new_session" => true, "prefix" => "web-session.") sess["CustID"] = 123

sess["Part"] = "ABC"

Solanki Gautam V. (04305027) Indian Institute Of Technology Bombay

CGI scripts using Ruby Language

(18)

Example 3(Cont.)

cgi.out{

cgi.html{"\nHTML content here"}

}

This will send a cookie to the user named “rubyweb” with a value of 9650. It will also create a disk file in TMP/web-session.9650 with the key, value pairs for CustID and Part.

Solanki Gautam V. (04305027) Indian Institute Of Technology Bombay

CGI scripts using Ruby Language

(19)

Reciveing set of saved session data

When the user returns, all you need is a parameter to indicate the session id. In this example, that would be rubyweb=9650. With that value in the parameters, you’ll be able to retrieve the full set of saved session data.

require "cgi"

require "cgi/session"

cgi = CGI.new("html3")

sess = CGI::Session.new( cgi, "session_key" => "rubyweb",

"prefix" => "web-session.")

Solanki Gautam V. (04305027) Indian Institute Of Technology Bombay

CGI scripts using Ruby Language

(20)

Reciveing set of saved session data(Cont.)

cgi.out{

cgi.html{

"\nCustomer #{sess[’CustID’]} orders an #{sess[’Part’]}"}

}

When the user returns, all you need is a parameter to indicate the session id. In this example, that would be rubyweb=9650. With that value in the parameters, you’ll be able to retrieve the full set of saved session data.

Solanki Gautam V. (04305027) Indian Institute Of Technology Bombay

CGI scripts using Ruby Language

(21)

References

http://www.rubycentral.com/book/web.html http://www.rubycentral.com/articles/cgi.html

http://www-106.ibm.com/developerworks/linux/library/l- ruby1.html

http://www.jmarshall.com/easy/cgi/

Solanki Gautam V. (04305027) Indian Institute Of Technology Bombay

CGI scripts using Ruby Language

References

Related documents

Trust, confidence, role models Same as what runs community services!.. What runs the

Clinico-mycological study on superficial fungal infections in tertiary care hospital and a profile of their antifungal susceptibility pattern. Hanumanthappa H, Sarojini K,

This is to certify that the Dissertation titled “A STUDY TO ASSESS THE EFFECTIVENESS OF ASSERTIVENESS TUTELAGE ON RAISING SELF ESTEEM AMONG ADOLESCENT GIRLS IN

This is to certify that Mr Ankur Thakur, from Centre for Management studies, Jamia Millia Islamia has completed Internship with Tata Power Solar Systems Limited, Bangalore for two

Jitendra Kumar, student of Dayalbagh Educational Institute, Agra completed a 6-week Internship Programme under Hankernest Technologies Pvt.. As part-fulfillment of the

Additionally, because product merchandising refers to both in-store and digital, it includes all promotional activities that take place in a store (such as shelf

3: Program to print static data member and method using class name and object.. 4: Program to show Private, Protected and public class

• Internet Personal Identification Number (IPIN) is a password to access your NPS account on CRA Website (www.cra-nsdl.com ).. • IPIN can be reset online using “One Time