acts_as_sdata
Ruby implementation of SData (Sage Data) protocol. Rails plugin which enables SData syndication and publishing.
Demo
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
http://localhost:3000/presidents/!Wilson
<?xml version="1.0" encoding="UTF-8"?>
<entry xmlns="http://www.w3.org/2005/Atom" xmlns:attributes="http://sdata.sage.com/schemes/attributes">
<title>Wilson, Woodrow</title>
<summary>Wilson, Woodrow (1856-1924)</summary>
<attributes:born_at>1856</attributes:born_at>
<attributes:id>28</attributes:id>
<attributes:first_name>Woodrow</attributes:first_name>
<attributes:last_name>Wilson</attributes:last_name>
<attributes:party>Democrat</attributes:party>
<attributes:updated_at>Sat Jan 09 11:43:11 UTC 2010</attributes:updated_at>
<attributes:died_at>1924</attributes:died_at>
<attributes:order>28</attributes:order>
<attributes:term_started_at>1913</attributes:term_started_at>
<attributes:created_at>Sat Jan 09 11:43:11 UTC 2010</attributes:created_at>
<attributes:country>USA</attributes:country>
<attributes:term_ended_at>1921</attributes:term_ended_at>
</entry>
Collection path
http://localhost:3000/presidents
<?xml version="1.0" encoding="UTF-8"?>
<feed xmlns="http://www.w3.org/2005/Atom">
<id>urn:uuid:60a76c80-d399-11d9-b93C-0003939e0af6</id>
<title>List of US presidents</title>
<updated>2009-11-17T10:55:28+06:00</updated>
<link href="http://example.com/presidents"/>
<author>
<name>Sage</name>
</author>
<entry>
<title>Washington, George</title>
<summary>Washington, George (1732-1799)</summary>
</entry>
<entry>
<title>Adams, John</title>
<summary>Adams, John (1735-1826)</summary>
</entry>
.....
Predicate
http://localhost:3000/presidents(born_at gt 1900)
<?xml version="1.0" encoding="UTF-8"?>
<feed xmlns="http://www.w3.org/2005/Atom">
<id>urn:uuid:60a76c80-d399-11d9-b93C-0003939e0af6</id>
<title>List of US presidents</title>
<updated>2009-11-17T10:58:15+06:00</updated>
<link href="http://example.com/presidents"/>
<author>
<name>Sage</name>
</author>
<entry>
<title>Kennedy, John</title>
<summary>Kennedy, John (1917-1963)</summary>
</entry>
<entry>
<title>Johnson, Lyndon</title>
<summary>Johnson, Lyndon (1908-1973)</summary>
</entry>
.....
Publishing
SData protocol supports data publishing (as Atom Publishing Protocol does)
Example:
require 'atom/pub'
collection = Atom::Pub::Collection.new(:href => 'http://localhost:3000/presidents')
entry = Atom::Entry.load_entry <<XML
<?xml version="1.0" encoding="UTF-8"?>
<entry xmlns="http://www.w3.org/2005/Atom" xmlns:attributes="http://sdata.sage.com/schemes/attributes">
<attributes:born_at>1961</attributes:born_at>
<attributes:first_name>Barack</attributes:first_name>
<attributes:last_name>Obama</attributes:last_name>
<attributes:party>Democrat</attributes:party>
<attributes:died_at/>
<attributes:order>44</attributes:order>
<attributes:term_started_at>2009</attributes:term_started_at>
<attributes:country>USA</attributes:country>
<attributes:term_ended_at/>
</entry>
XML
collection.publish entry
The response will be:
<?xml version="1.0" encoding="UTF-8"?>
<entry xmlns="http://www.w3.org/2005/Atom" xmlns:attributes="http://sdata.sage.com/schemes/attributes">
<attributes:id>46</attributes:id>
<attributes:created_at>Sat Jan 09 14:29:57 UTC 2010</attributes:created_at>
<attributes:first_name>Barack</attributes:first_name>
...
</entry>
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://github.com/joshbuddy/usher.git
Install acts_as_sdata
$ script/plugin install git://github.com/DanielVartanov/acts_as_sdata.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).
Change
ActionController::Routing::Routes.draw do |map|
to
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.
Initializer
In config/initializers/mime_types.rb add the following:
ActionController::Base.param_parsers[Mime::Type.lookup('application/atom+xml')] = Proc.new do |data|
{ :entry => Atom::Entry.load_entry(data) }
end
Removing ambiguity
Problem
There is an ambigious path in case of exposing same resource in both REST and SData ways:
/presidents
It might be treated either as #index (REST way) or as #sdata_collection (SData way).
Solution
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
Then,
/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