ONLamp.com
oreilly.comSafari Books Online.Conferences.

advertisement


What Is Ruby on Rails
Pages: 1, 2, 3, 4, 5, 6, 7

Action Mailer

Action Mailer is a simple facility for sending and receiving email in your web application. Here's a method that sends an email with an attachment:

# send email with attachment
def signup_notification(recipient)
  recipients recipient.email_address_with_name
  subject "New account information"
  from "system@example.com"

  attachment :content_type => "image/jpeg", :body => File.read("an-image.jpg")

  attachment "application/pdf" do |a|
    a.body = generate_your_pdf_here()
  end
end

To learn more, see the Action Mailer API, and Chapter 19 of the book Agile Web Development with Rails.

Action Web Service

Action Web Service implements server-side support for the SOAP and XML-RPC web service protocols and makes it easy for you to create web service APIs and publish them via WSDL.

Here is part of the MetaWeblog API as implemented by Typo (open source weblog software written in Rails):

class MetaWeblogApi < ActionWebService::API::Base

  api_method :getRecentPosts,
    :expects => [ {:blogid => :string},
                  {:username => :string},
                  {:password => :string},
                  {:numberOfPosts => :int} ],
    :returns => [[MetaWeblogStructs::Article]]

  api_method :deletePost,
    :expects => [ {:appkey => :string},
                  {:postid => :string},
                  {:username => :string},
                  {:password => :string},
                  {:publish => :int} ],
    :returns => [:bool]
end

class MetaWeblogService < TypoWebService
  web_service_api MetaWeblogApi

  def getRecentPosts(blogid, username, password, numberOfPosts)
    articles = Article.find_all(nil, "created_at DESC", numberOfPosts)
    articles.to_a.collect{ |c| article_dto_from(c) }
  end

  def deletePost(appkey, postid, username, password, publish)
    article = Article.find(postid)
    article.destroy
    true
  end
end

This snippet shows only two of the seven API methods defined in this class by Typo.

To learn more, see the Action Web Service Manual.

Parting Thoughts

You can usually divide web application frameworks and the developers who use them into two distinct categories. At one end of the spectrum, you have the heavy-duty frameworks for the "serious" developers, and at the other end you have the lightweight, easy-to-use frameworks for the "toy" developers. Each of these groups generally regards the other with disdain.

One of the most interesting things is that Rails is attracting developers from both camps. The high-end developers are tired of the repetitive, low-productivity routine that they have been forced to endure, while the low-end developers are tired of battling a mess of unmanageable code when their web apps move beyond the simple. Both of these disparate groups find that Rails provides sustainable relief for their pain. I don't know about you, but I find this quite remarkable!

At the moment, Ruby on Rails barely captures a tiny percentage of web development projects. Yet it is rapidly gaining mind share, and many respected software development leaders have been testing the waters with Rails and publicly singing its praises.

Perhaps it's time that you too checked out Rails to see firsthand what the fuss is all about.

Acknowledgments

Most of the sample source code shown in this article came from the Rails API documentation, with permission.

Resources

Curt Hibbs has been a consultant to well-known companies like Hewlett Packard, Intuit, Corel, WordStar, Charles Schwab, Vivendi Universal, and more. He now works as a Senior Software Engineer for The Boeing Company in St. Louis.


Return to ONLamp.com.



Sponsored by: