Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/mdub/representative
XML/JSON representations of your Ruby objects
https://github.com/mdub/representative
Last synced: 1 day ago
JSON representation
XML/JSON representations of your Ruby objects
- Host: GitHub
- URL: https://github.com/mdub/representative
- Owner: mdub
- License: mit
- Created: 2009-09-04T12:02:14.000Z (over 15 years ago)
- Default Branch: master
- Last Pushed: 2019-01-09T15:17:45.000Z (almost 6 years ago)
- Last Synced: 2024-04-25T19:21:21.279Z (8 months ago)
- Language: Ruby
- Size: 137 KB
- Stars: 78
- Watchers: 4
- Forks: 12
- Open Issues: 0
-
Metadata Files:
- Readme: README.markdown
- License: LICENSE
Awesome Lists containing this project
README
Representative
==============[![Gem Version](https://badge.fury.io/rb/representative.png)](http://badge.fury.io/rb/representative)
[![Build Status](https://travis-ci.org/mdub/representative.svg?branch=master)](https://travis-ci.org/mdub/representative)"Representative" makes it easier to create XML or JSON representations of your Ruby objects.
It works best when you want the output to roughly follow the object structure, but still want complete control of the result.
Generating XML
--------------Given a Ruby data-structure:
@books = [
Book.new(
:title => "Sailing for old dogs",
:authors => ["Jim Watson"],
:published => Publication.new(
:by => "Credulous Print",
:year => 1994
)
),
Book.new(
:title => "On the horizon",
:authors => ["Zoe Primpton", "Stan Ford"],
:published => Publication.new(
:by => "McGraw-Hill",
:year => 2005
)
),
Book.new(
:title => "The Little Blue Book of VHS Programming",
:authors => ["Henry Nelson"],
:rating => "****"
)
]Representative::Nokogiri can be used to generate XML:
xml = Representative::Nokogiri.new do |r|
r.list_of :books, @books do
r.element :title
r.list_of :authors
r.element :published do
r.element :by
r.element :year
end
endend
puts xml.to_s
which produces:
Sailing for old dogs
Jim Watson
Credulous Print
1994
On the horizon
Zoe Primpton
Stan Ford
McGraw-Hill
2005
The Little Blue Book of VHS Programming
Henry Nelson
Notice that:
- The structure of the output mirrors the structure described by the nested Ruby blocks.
- Representative walks the object-graph for you.
- Using `list_of` for a collection attribute generates an "array" element, which plays nicely
with most Ruby XML-to-hash converters.
- Where a named object-attribute is nil, you get an empty element.Generating JSON
---------------Representative::Json can be used to generate JSON, using exactly the same DSL:
json = Representative::Json.new do |r|
r.list_of :books, @books do
r.element :title
r.list_of :authors
r.element :published do
r.element :by
r.element :year
end
endend
puts json.to_s
producing:
[
{
"title": "Sailing for old dogs",
"authors": [
"Jim Watson"
],
"published": {
"by": "Credulous Print",
"year": 1994
}
},
{
"title": "On the horizon",
"authors": [
"Zoe Primpton",
"Stan Ford"
],
"published": {
"by": "McGraw-Hill",
"year": 2005
}
},
{
"title": "The Little Blue Book of VHS Programming",
"authors": [
"Henry Nelson"
],
"published": null
}
]Installation
------------Representative is packaged as a Gem. Install with:
gem install representative
Ruby on Rails integration
-------------------------A separate gem, [RepresentativeView](https://github.com/mdub/representative_view), integrates Representative as an ActionPack template format.
Tilt integration
----------------Representative includes integration with [Tilt](https://github.com/rtomayko/tilt), which can be enabled with:
require "representative/tilt_integration"
This registers handlers for "`.xml.rep`" and "`.json.rep`" templates.
Copyright
---------Copyright (c) 2009-2018 Mike Williams. See LICENSE for details.
Similar projects
----------------If Representative is not your cup of tea, you may prefer:
* [Tokamak](https://github.com/abril/tokamak)
* [Builder](http://rubygems.org/gems/builder)
* [JSONify](https://github.com/bsiggelkow/jsonify)
* [Argonaut](https://github.com/jbr/argonaut)
* [JSON Builder](https://github.com/dewski/json_builder)
* [RABL](https://github.com/nesquena/rabl)Just don't go back to using "`this_thing.to_xml`" and "`that_thing.to_json`", m'kay?