Ruby, or how I learned to stop caring and love the gun I
Download
Report
Transcript Ruby, or how I learned to stop caring and love the gun I
Philip Repsher
October 29th, 2008
Or Maybe
November 3rd, 2008
In this presentation, I will guide you through
how to create a simple ruby web server and a
simple ruby web client to get information from
the server.
Webrick
Simplified web server library included in ruby since
Ruby 1.8.0
net/http class
Basic Ruby framework for HTTP interaction, we will
use it for our client.
Webrick is a library included in the Ruby
standard library dedicated to creating HTTP
servers
Webrick is a very easy way to create HTTP
servers.
A few commands Require “webrick”
WEBrick::HTTPServer.new(address, port)
Trap “INT”
Let’s go make a simple web server now
The server we just made doesn’t truly do
anything
After creating a server, servlets are mounted
onto the server as subdirectories
(www.server.com/servlet) to provide more
functionality
Each time a servlet is called, Ruby creates a
new instance of the servlet in its own thread
and runs it.
Mount – after you make a servlet, it must be
mounted on the server before the server is
started.
do_GET – A method with this name will
service all GET requests.
do_GET methods have two arguments, the
request and the response, or req and resp.
Let’s make some servlets!
Webrick comes installed with several useful
Servlets
Filehandler – if the server is fed a path when
starting it automatically sets up a filehandler to
serve that directory
CGIhandler-If the directory a filehandler is set
up for contains any CGI files, the server will
automatically set up a CGIhandler to handle
the execution of these files.
Net/http is a ruby class that provides access to
World Wide Web documents and information
via HTTP.
As such, it is the class to use to make a client
that will access the web server and report back.
Require ‘net/http’
Next we need to set up the client. This client
will send a request to the server for info, and
then print the info out when it is received.
This client also needs to include the uri library
to parse the URL for the net/http command.
We could also add some time commands to see
how long the get takes.
There are many more advanced Ruby server
frameworks out there
In Particular, Ruby on Rails is very popular
Ruby on Rails is a Ruby development
framework set up to assist in the making of
ruby web applications, particularly database
driven ones.
http://segment7.net/projects/ruby/WEBrick/
servlets.html
http://microjet.ath.cx/webrickguide/html/ht
ml_webrick.html
http://www.rubydoc.org/stdlib/libdoc/net/http/rdoc/index.ht
ml
http://www.webrick.org/
http://www.rubydoc.org/docs/ProgrammingRuby/