100 likes | 216 Views
Ruby Web. Philip Repsher October 29 th , 2008 Or Maybe November 3 rd , 2008. Intro. 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
E N D
Ruby Web Philip Repsher October 29th, 2008 Or Maybe November 3rd, 2008
Intro • 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 • 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
Webrick Servers/Servlets • 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.
Things to Know - Servlets • 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 Standard 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 • 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’
Net/http Client • 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.
Advanced Ruby Web • 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.
Resources • http://segment7.net/projects/ruby/WEBrick/servlets.html • http://microjet.ath.cx/webrickguide/html/html_webrick.html • http://www.ruby-doc.org/stdlib/libdoc/net/http/rdoc/index.html • http://www.webrick.org/ • http://www.ruby-doc.org/docs/ProgrammingRuby/