Post on 15-Jan-2015
description
Construíndo APIs usando Rails
Rafael Felix
Rafael quem?
Rafael FelixSoftware Developer
Rafael quem?
Aplicação Servidor Externo
Aplicação Servidor Externo
XML
WS - SOAP
Aplicação Servidor Externo
Aplicação Servidor Externo
app-servidor
Aplicação Servidor Externo
app-servidor
JSON RESTfull
XMLWS - SOAP
Aplicação Servidor Externo
app-servidor
JSON RESTfull
XMLWS - SOAP
Cliente
Aplicação Servidor Externo
app-servidor
JSON RESTfull
XMLWS - SOAP
HTTPHTML
API - JSONRESTfull
Cliente
class XptoController < ApplicationController def show @xpto = Xpto.find params[:id] endend
resource :xpto, :only => [:show]
class XptoController < ApplicationController def show @xpto = Xpto.find params[:id] respond_to do |format| format.html format.json{ render :json => @xpto } end endend
class XptoController < ApplicationController def show @xpto = Xpto.find params[:id] respond_to do |format| format.html format.json{ render :json => @xpto } end endend
{ "id": 1, "text": "Foo Bar", "created_at": "2012-04-12T01:37:27Z", "updated_at": "2012-04-12T01:37:27Z"}
@xpto.to_json
class XptoController < ApplicationController def show @xpto = Xpto.find params[:id] respond_to do |format| format.html format.json{ render :json => @xpto.to_json(:include => [:children]) } end endend
class XptoController < ApplicationController def show @xpto = Xpto.find params[:id] respond_to do |format| format.html format.json{ render :json => @xpto.to_json(:include => [:children]) } end endend
@xpto.to_json
JSON::ParserError: 756: unexpected token at
E agora quem poderá nos defender?
class XptoController < ApplicationController def show @xpto = Xpto.find params[:id] respond_to do |format| format.html format.json{ render :json => @xpto.to_json(:include => [:children]) } end endend
class XptoController < ApplicationController def show @xpto = Xpto.find params[:id] respond_to do |format| format.html format.json{ render :json => @xpto.as_json(:include => [:children]) } end endend
class XptoController < ApplicationController def show @xpto = Xpto.find params[:id] respond_to do |format| format.html format.json{ render :json => @xpto } end endend
class Xpto def as_json(options={}) super(options.merge(:include => [:children])) endend
class YptoController < ApplicationController respond_to :html, :json def show @xpto = Xpto.find params[:id] respond_with @xpto endend
app/views/xpto/show.json.rabl
object @xptochild :children do attributes :id, :name, :descriptionend
app/views/ypto/show.json.rabl
object @xptochild :owner do attributes :id, :nameend
https://github.com/rails/jbuilder
json.(@xpto, :id, :text, :created_at, :updated_at)json.children @xpto.children, :id, :name, :description
app/views/xpto/show.json.jbuilder
app/views/ypto/show.json.jbuilder
json.(@xpto, :id, :text, :created_at, :updated_at)json.owner do |json| json.id @xpto.owner.id json.name @xpto.owner.name json.description @xpto.owner.descriptionend
class XptoController < ApplicationController respond_to :html, :json def show @xpto = Xpto.find params[:id] respond_with @xpto endend
Obrigado!@rs_felix
felix.rafael@gmail.com