Want to stay on top? Ruby Weekly is a once-weekly e-mail newsletter covering the latest Ruby and Rails news.
     Feed Icon

Make any Ruby object Rack-friendly with Rackable

By Ric Roberts / July 20, 2009

rackFran├žois Vaux has recently published a Ruby module called Rackable which allows you to make any Ruby object Rack-friendly, providing it with a REST-like interface.

What does this mean? Well, a Rack application is essentially a Ruby object that responds to call(). Rackable just gives your object a call method which uses the Rack environment to dispatch to a method.

So, you just need to include Rackable in your class and implement methods for the the appropriate REST verbs. This means you can create a hello_world.ru file like this:

require 'rackable'

class HelloWorld
  include Rackable

  def get()
    "Hello, world!"
  end

end

run HelloWorld.new

... start it with rackup, and then use something like curl (or even your browser) to call the methods.

Thanks to Alex Young for putting me on to this.

devver-icon.gifAlso.. Got a slow Test::Unit or RSpec suite? Run them up to three times faster on Devver's cloud! Setup is simple and requires no code changes. Request a beta invite today!

Comments

  1. ehsanul says:

    Interesting. But why not just use Sinatra?

  2. Matthew says:

    Ah beat me to it but I've actually been working on something similar recently. It takes the idea a bit further though, with support for content negotiation, caching, conditional requests, auth, subresource resolution, range requests and some other goodies. Hoping to develop it into something of a framework for restful API development.

    Just put it up on github now: http://github.com/mjwillson/rest_on_rack/tree/master

    Although the rdoc might be a better place to start (Rack::REST::Resource and Rack::REST::Entity being the most important things to look at)
    http://dev.playlouder.com/doc/rest_on_rack/rdoc/

    Still a work in progress but a lot is already in place and tested, interested to hear from anyone interested in helping out / potentially using something like this, it's born somewhat out of frustration at doing pure API work in rails/merb.

  3. Daniel Berger says:

    So, we can put this directly in our AR models and skip the views then right? ;)

Other Posts to Enjoy

Twitter Mentions