Find file
Fetching contributors…
Cannot retrieve contributors at this time
200 lines (145 sloc) 6.56 KB


Ruby implementation of SData (Sage Data) protocol. Rails plugin which enables SData syndication and publishing.


Let’s consider we want to expose the list of US presidents in SData way (this is exactly what is done in presidents application in acts_as_sdata-examples repository).

From client side it will look like this:

Instance path


<?xml version="1.0" encoding="UTF-8"?>
<entry xmlns="" xmlns:attributes="">
  <title>Wilson, Woodrow</title>
  <summary>Wilson, Woodrow (1856-1924)</summary>
  <attributes:updated_at>Sat Jan 09 11:43:11 UTC 2010</attributes:updated_at>
  <attributes:created_at>Sat Jan 09 11:43:11 UTC 2010</attributes:created_at>

Collection path


<?xml version="1.0" encoding="UTF-8"?>
<feed xmlns="">
  <title>List of US presidents</title>
  <link href=""/>
    <title>Washington, George</title>
    <summary>Washington, George (1732-1799)</summary>
    <title>Adams, John</title>
    <summary>Adams, John (1735-1826)</summary>


http://localhost:3000/presidents(born_at gt 1900)

<?xml version="1.0" encoding="UTF-8"?>
<feed xmlns="">
  <title>List of US presidents</title>
  <link href=""/>
    <title>Kennedy, John</title>
    <summary>Kennedy, John (1917-1963)</summary>
    <title>Johnson, Lyndon</title>
    <summary>Johnson, Lyndon (1908-1973)</summary>


SData protocol supports data publishing (as Atom Publishing Protocol does)


require 'atom/pub'

collection = => 'http://localhost:3000/presidents')

entry = Atom::Entry.load_entry <<XML
<?xml version="1.0" encoding="UTF-8"?>
<entry xmlns="" xmlns:attributes="">

collection.publish entry

The response will be:

<?xml version="1.0" encoding="UTF-8"?>
<entry xmlns="" xmlns:attributes="">
  <attributes:created_at>Sat Jan 09 14:29:57 UTC 2010</attributes:created_at>

with status 201 (Created)

How to install

Install Usher

(Usher is an alternative router)

It is better to refer to Usher’s page, but usually installation of Usher plugin is just one commad:

$ script/plugin install git://

Install acts_as_sdata

$ script/plugin install git://

How to use

In order to make application act as SData you should write the following.

In router

map.sdata_resource :presidents
this line will enable all SData-related paths for /presidents

Also you should include special routing delimiters which are used in SData paths (remember, we are using Usher, this will NOT work with native Rails Router).

ActionController::Routing::Routes.draw do |map|
ActionController::Routing::Routes.draw(:delimiters => ['/', '.', '!', '\(', '\)' ]) do |map|

In controller

acts_as_sdata :model => President,
              :feed => { :id => 'urn:uuid:60a76c80-d399-11d9-b93C-0003939e0af6',
                          :author => 'Sage',
                          :path => '/presidents',
                          :title => 'List of US presidents' }

:feed options defines fields of Atom feed

In model

acts_as_sdata :title => lambda { "#{last_name}, #{first_name}" },
              :summary => lambda { "#{last_name}, #{first_name} (#{born_at}-#{died_at})" },
              :instance_id => :last_name

Note #1: these lambda’s will be executed in context of the actual model instance.

Note #2: SData specifications say that human-readable instance id’s are preferred. The :instance_id option helps to enable search by any unique attribute.


In config/initializers/mime_types.rb add the following:

ActionController::Base.param_parsers[Mime::Type.lookup('application/atom+xml')] = do |data|
  { :entry => Atom::Entry.load_entry(data) }

Removing ambiguity


There is an ambigious path in case of exposing same resource in both REST and SData ways:


It might be treated either as #index (REST way) or as #sdata_collection (SData way).


In order to remove this ambiguity, a :formatted_paths option can be given as a second parameter to #sdata_resource in routes.rb:

map.sdata_resource :presidents, :formatted_paths => true

/presidents routes to Presidents#index and returns HTML
/presidents.sdata routes to Presidents#sdata_collection and returns Atom/XML

without any changes in the controller code!

Copyright Sage 2009
Released under the MIT license