Specialized Networking APIs for Ruby

 < Day Day Up > 



Ruby also includes a number of specialized APIs for a number of Application layer protocols. These include SMTP, HTTP, POP3, FTP, Telnet, and others. In this section, we look at examples of the SMTP and HTTP client-side classes.

Ruby Net::SMTP Class

SMTP is the Simple Mail Transfer Protocol and is used to transfer e-mail to a mail server that delivers it to the final destination. Ruby provides a very simple SMTP interface, as is illustrated in Listing 13.2 (smtpc.rb).

Listing 13.2 Sample Net: :SMTP client example.

start example
require 'net/smtp' recip = "you@yourdomain.com" from = "me@mydomain.com" server = "yourdomain.com" message = "From: me\nSubject: Hi\n\nHello\n\n" smtpSession = Net::SMTP::new( server ) smtpSession.start smtpSession.sendmail( message, from, recip ) smtpSession.finish 
end example

The very simple source in Listing 13.2 illustrates sending a short e-mail. We make the Net::SMTP class visible using require and then set up our recipient (recip) and source e-mail (from) addresses. Next, we define the server (where the SMTP client will connect to send the e-mail). Then, we define the e-mail message to be sent in the message string.

The complete SMTP process is then shown in the final four lines. We create a new instance of an SMTP client using Net::SMTP::new, defining the server to which we’ll connect to send our e-mail. We use the start method to start the actual SMTP session and then send our e-mail using the sendmail method. Finally, to end the session, we use the finish method that permits the remote SMTP server to deliver the message.

Ruby Net::HTTP Class

HTTP is the classic Web transport protocol used to transfer Web pages (and other content) across the Internet. The Ruby HTTP class provides a very simple and powerful client-side interface that can be used for a variety of purposes. A sample usage of the HTTP client is shown in Listing 13.3.

Listing 13.3 Sample Net: :HTTP client example (httpc.rb).

start example
 require 'net/http' hcli = Net::HTTP::new( "www.mtjones.com" ) resp, data = hcli.get( "/index.html" ) print data
end example

The result of the example shown in Listing 13.3 is the HTML source retrieved (the index.html file from host www.mtjones.com. We first make the HTTP network library visible using the require method and then create a new HTTP client using Net::HTTP:new method, specifying the host to which we want to connect. Next, we request the file using the get method. Finally, we emit the page (in HTML format) using print.

That’s simple, compared to what actually goes on underneath the covers of HTTP. Therefore, Ruby provides some very useful and simplifying abstractions for higher-level protocols.



 < Day Day Up > 



BSD Sockets Programming from a Multi-Language Perspective
Network Programming for Microsoft Windows , Second Edition (Microsoft Programming Series)
ISBN: 1584502681
EAN: 2147483647
Year: 2003
Pages: 225
Authors: Jim Ohlund

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net