Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Lightweight JSON builder for Rails
Ruby
Branch: master

Fetching latest commit…

Cannot retrieve the latest commit at this time

Failed to load latest commit information.
lib
README.textile
init.rb

README.textile

Argonaut is a lightweight JSON builder template handler for Rails

Installing it

It’s as easy as:

script/plugin install git://github.com/jbr/argonaut.git

Usage

It’s a whole lot like .xml.builder, only it’s .json.argonaut. Make an appropriately named view, like show.json.argonaut and put something in it like:

for idea in @ideas
  doc.ideas do |i|
    i.title idea.title
    for comment in idea.comments
      i.comments do |c|
        c.author comment.author.to_s
        c.text comment.text
      end
    end
  end
end

Which is identical to

{
  :ideas => @ideas.map do |idea|
    {
      :title => idea.title,
      :comments => idea.comments.map do |comment|
        {
          :author => comment.author.to_s,
          :text => comment.text
        }
      end
    }
  end
}.to_json
end

Just a little code kata, not really sure that the former is actually prettier.

XML too

The name makes less sense and it’s not really better than Builder, but you can do .xml.argonaut as well now. It’s the equivalent of calling to_xml(:root => 'response') on the above hash. In theory, the same file could be used for .xml.argonaut and .json.argonaut, but it’s not obvious how to tell the template handler system to reuse a template. Maybe soon.

Something went wrong with that request. Please try again.