Ruby Internet Services

The most popular Internet service is, of course, the Web, and Ruby's most popular Internet library is the Rails framework. Apart from Rails, most of the interesting stuff you can do with Ruby happens on the client end.

Retrieving a Web Page from a URL

Suppose we want to retrieve an HTML document from a particular site. Here's the code:

The Net:HTTP library provides your program functions to access WWW documents via HTTP version 1.1. We open a connection and fetch a page, displaying the response code and message, header information, and some of the body. The start method opens a TCP connection and a HTTP session. When this method is called with a block, it gives a HTTP object to the block and closes the TCP connection / HTTP session after the block is executed. The get method gets data from the path on the connected host. This method returns a Net::HTTPResponse object. code returns the HTTP result code string, for example, '302'. message returns the HTTP result message, for example, 'Not Found'. The body method returns the body of the page.

The open-uri library extends Kernel#open, allowing it to accept URIs for FTP and HTTP as well as local filenames. Once opened, these resources can be treated as if they were local files, accessed using conventional IO methods. The URI passed to open is either a string containing an HTTP or FTP URL, or a URI object. When opening an HTTP resource, the method automatically handles redirection and proxies. When using an FTP resource, the method logs in as an anonymous user. The IO object returned by open in these cases is extended to support methods that return meta-information from the request: content_type, charset, content_encoding, last_modified, status, base_uri, meta.

Note: The Ruby Logo is Copyright (c) 2006, Yukihiro Matsumoto. I have made extensive references to information, related to Ruby, available in the public domain (wikis and the blogs, articles of various Ruby Gurus), my acknowledgment and thanks to all of them. Much of the material on and in the course at is drawn primarily from the Programming Ruby book, available from The Pragmatic Bookshelf.