Rails enables web services by default, which is pretty awesome, and I’ve been relying on that for a while. It is pretty nifty how Rails will magically parse XML post parameters, create an in-memory object and then save that object to the database without your having to write one line of code. However, when the magic fails it can be pretty hard to debug. I found it useful to run basic tests on the command line using curl (hearing the voice of Zach Moazeni in my head saying: “test your assumptions.”)

Below is a writeup of the set of curl commands and sample output for testing the default Rails XML REST APIs. This can serve as a cheat sheet for the experienced or an introduction for folks new to rails who want a basic understanding of the default webservice APIs.

Create an app, by typing the following commands into your terminal:


$ rails basic_app
$ cd basic_app
$ ./script/generate scaffold project title:string description:text
$ rake db:migrate
$ ./script/server

In Rails 2.3, you also need to added the following line to the top of app/controllers/projects_controller.rb (This will allow external access to the APIs.) You can make this change while the server is running, btw.


skip_before_filter :verify_authenticity_token

Leave that window open where you can see it, since it will output useful stuff from the log. Then in another terminal window, experiment with the following commands to interact with your application APIs.

blockquote.code {
font-style: normal;
font-family: Monaco,monospace;
font-size: 12px;
border: solid 1px #aaa;
padding: 6px;
background-color: #eee;
color: inherit;
overflow:auto;
margin: 10px 0px;
}

Create

POST /projects.xml

Create a project object based on the XML representation given in the post body and save in the projects database table.

$ curl -X POST -d "<project><title>Awesome</title><description>This is an awesome project.</description></project>" -H "Content-Type: application/xml" http://localhost:3000/projects.xml

<?xml version="1.0" encoding="UTF-8"?>
<project>
  <created-at type="datetime">2009-06-21T10:13:43-07:00</created-at>
  <description>This is an awesome project.</description>
  <id type="integer">6</id>
  <title>Awesome</title>
  <updated-at type="datetime">2009-06-21T10:13:43-07:00</updated-at>
</project>

Index

GET /projects.xml

This returns a list of all of the projects in the database with an automatically generated XML representation.

$ curl http://localhost:3000/projects.xml<?xml version="1.0" encoding="UTF-8"?>

<projects type="array">
<project>
  <created-at type="datetime">2009-06-21T10:13:19-07:00</created-at>
  <description>This is an awesome project.</description>
  <id type="integer">1</id>
  <title>Awesome</title>
  <updated-at type="datetime">2009-06-21T10:13:19-07:00</updated-at>
</project>
<project>
  <created-at type="datetime">2009-06-21T10:13:43-07:00</created-at>
  <description>New information here</description>
  <id type="integer">2</id>
  <title>Awesome</title>
  <updated-at type="datetime">2009-06-21T10:49:21-07:00</updated-at>
</project>
</projects>

Show

GET /projects/1.xml

This returns an xml representation of the project with id #1

$ curl http://localhost:3000/projects/1.xml<?xml version="1.0" encoding="UTF-8"?>

<project>
  <created-at type="datetime">2009-06-21T10:45:19-07:00</created-at>
  <description>This is an awesome project.</description>
  <id type="integer">8</id>
  <title>Awesome</title>
  <updated-at type="datetime">2009-06-21T10:45:19-07:00</updated-at>
</project>

Update

PUT /projects/1.xml

This modifies the project with id #1

curl -X PUT -d "<project><description>New information here</description></project>" -H "Content-Type: application/xml" http://localhost:3000/projects/1.xml

I believe that open source provides a new model for changing the way things are. It is exciting to work with so many people who are experienced with working together in eclectic collections of motivated individuals. It is a new way of getting things done and it is neat to see it applied to social challenges as well as technical ones.

It all started with an idea of how to take a few baby steps toward increasing the number of women in the SF Ruby community.  Sarah Mei and I got to talking about putting a workshop together to teach women Ruby on Rails.  We envisioned teaching 10-20 women once per month, betting that over time a few would decide to stick around.  With just a few more women we could double or triple the number of women at the typical SF Ruby Meetup!  As we acted on our idea, we were joined by many individuals, organizations and companies who wanted to help.  With the force of the community behind us, the first workshop became a bold move with 62 attendees, 15 volunteers, and 7 simulataneous classes.

This workshop represents a breakthrough transformation in several ways:

  1. It is entirely open source and designed to be replicated by anyone to any audience (not just women).
  2. There seems to be a large potential audience of women.  The event filled up in less than 3 days with very little publicity.
  3. There are a large number of people and companies who want to see diversity amongst programmers, making it easy to find volunteers, space, food & drink, equipment and everything else needed to make such an event happen.
  4. The Ruby and Rails community in the SF Bay Area is still small, so just a few additional women will make a huge difference.  We don’t need to change all of society for this effort to have a significant and noticeable effect.

Every single person involved made a difference, just like in an open source software project.  This was facilitated by organizations that provided resources.  Some provided private space (server space and physical meeting space) and others, such as github, with a policy of supporting open source projects for free, provided public space.

People stepped in and did what was needed.  Orange Labs was an incredible host, letting us use every conference room as well as providing breakfast, lunch, and copies.  Vodpod who couldn’t make it in person contributed money for food & drink.  Volunteers arrived with USB keys that had software installs to save download time. Participants helped with registration, setting up and tearing down chairs and tables, arrived early and stayed late to do what needed doing.  Remote folks reviewed courseware. I don’t have a list of every volunteer, but I wanted to especially thank the additional teachers who made us able to open the even to such a large group and still keep the classes small:  Austin Putman, Bosco So, Carmen Diaz, Jen-Mei Wu, Seth Walker, Wolfram Arnold, Ivan Storck and Will Sargent.

If you are interested in volunteering at a future workshop, you can join our google group.

Here’s a list of the many organizations who helped:

Special thanks to Heroku for providing a free-tier, not just for this workshop, but for everyone.