Project

to_api

0.01
No commit activity in last 3 years
No release in over 3 years
Helper for simplifying JSON api creation.
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
 Dependencies

Development

>= 0
>= 0.8.7
= 2.14.1
 Project Readme

Description

A lightweight, format-agnostic API transformation gem.

Synopsis

Models shouldn't know about formats. Controllers shouldn't know what attributes your domain objects have. It should be trivial to serve the same data in multiple formats.

How do I use it?

Implement to_api on your data objects to return a hash with all needed attributes.

class Person
  attr_accessor :first_name, :last_name, :secret
  
  def to_api
    {:first_name => @first_name, :last_name => @last_name}.to_api
  end
end
    
class Group
  attr_accessor :name, :description, :people
      
  def to_api
    {:name => @name, :description => @description, :people => @people}.to_api
  end
end

Now just call your to_api method. You'll get back a hash of arrays, hashes, strings, numbers, and nil. All of these are very easily converted to a format of your choice.

JSON.generate(group.to_api)

If I have to implement to_api, what does the gem do for me?

The gem provides to_api for common Ruby classes, allowing simple conversion to JSON, XML, YAML, etc. Hash and Enumerable transform all their contents, allowing your data objects to simply call to_api on a hash of relevant attributes.

Fixnum, String, DateTime, Symbol, and NilClass are also provided.

What about Ruby on Rails?

If ActiveRecord is present, ActiveRecord::Base#to_api transforms and returns the attributes hash. It also supports an :includes option that mirrors the ActiveRecord finder usage.

person.to_api([{:groups => :members}, :interests])

What if I need support for other common classes?

For most classes, it only takes a couple specs and a few lines of code. Send a pull request; we'd love to take your additions.

Authors