[FLOSS week]: classy_resources - resource_controller for sinatra
Posted by: James Golick on 2009-02-16
This release announcement is part of FLOSS week.
Sinatra is awesome. Lightweight web services have never been easier. But, even sinatra can get a little bit tedious when you have to build three or four full resources for consumption with active resource. Not anymore, though.
Introducing Classy Resources
Classy resources adds a tiny declarative API to sinatra for building active_resource compliant REST APIs. Exposing a model is as simple as:
define_resource :posts, :member => [:get, :put, :delete],
:collection => [:get, :post],
:formats => [:xml, :json, :yaml]
The above declaration gets you a REST API, with all of the actions that active resource expects, in as many formats as you list. You can customize which actions get created by changing the contents of the :member and :collection arrays.
It's also possible to override nearly all of classy_resources' behaviours by simply overriding methods. If you're familiar with resource_controller, you'll recognize this pattern.
For example, if your classes were namespaced in a module, you'd need to override the way that resource names, like :posts, are translated in to classes. It's easy.
def class_for(resource)
MyModule.const_get(resource.to_s.singularize.classify.constantize)
end
For more overrides, see the README.
Get It
$ sudo gem install giraffesoft-classy_resources
Or fork it at github.
This release announcement is part of FLOSS week. If you're interested in more open source software, consider subscribing or following us on twitter to get the next announcements asap.

