Desenvolvendo APIs usando Rails - Guru SC 2012

1,157 views

Published on

My talk at the GuruSC (Santa Catarina's ruby user group) 2012

Published in: Technology
0 Comments
0 Likes
Statistics
Notes
  • Be the first to comment

  • Be the first to like this

No Downloads
Views
Total views
1,157
On SlideShare
0
From Embeds
0
Number of Embeds
92
Actions
Shares
0
Downloads
7
Comments
0
Likes
0
Embeds 0
No embeds

No notes for slide

Desenvolvendo APIs usando Rails - Guru SC 2012

  1. 1. Construíndo APIs usando Rails Rafael Felix
  2. 2. Rafael quem?
  3. 3. Rafael quem?Rafael Felix Software Developer
  4. 4. Aplicação Servidor Externo
  5. 5. XMLAplicação Servidor Externo WS - SOAP
  6. 6. Aplicação Servidor Externo
  7. 7. Aplicação Servidor Externo app-servidor
  8. 8. Aplicação Servidor Externo JSON XML RESTfull WS - SOAP app-servidor
  9. 9. ClienteAplicação Servidor Externo JSON XML RESTfull WS - SOAP app-servidor
  10. 10. HTTP Cliente HTML API - JSON RESTfullAplicação Servidor Externo JSON XML RESTfull WS - SOAP app-servidor
  11. 11. class XptoController < ApplicationController def show @xpto = Xpto.find params[:id] endend resource :xpto, :only => [:show]
  12. 12. class XptoController < ApplicationController def show @xpto = Xpto.find params[:id] respond_to do |format| format.html format.json{ render :json => @xpto } end endend
  13. 13. @xpto.to_json { "id": 1, "text": "Foo Bar", "created_at": "2012-04-12T01:37:27Z",class XptoController < ApplicationController def show"updated_at": "2012-04-12T01:37:27Z" } @xpto = Xpto.find params[:id] respond_to do |format| format.html format.json{ render :json => @xpto } end endend
  14. 14. 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
  15. 15. class XptoController < ApplicationController def show@xpto.to_json @xpto = Xpto.find params[:id] JSON::ParserError: 756: unexpected token at respond_to do |format| format.html format.json{ render :json => @xpto.to_json(:include =>[:children]) } end endend
  16. 16. E agora quem poderá nos defender?
  17. 17. 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
  18. 18. 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
  19. 19. class XptoController < ApplicationController def show @xpto = Xpto.find params[:id] respond_to do |format| format.html format.json{ render :json => @xpto } end endend
  20. 20. class Xpto def as_json(options={}) super(options.merge(:include => [:children])) endend
  21. 21. class YptoController < ApplicationController respond_to :html, :json def show @xpto = Xpto.find params[:id] respond_with @xpto endend
  22. 22. https://github.com/nesquena/rabl
  23. 23. app/views/xpto/show.json.rablobject @xptochild :children do attributes :id, :name, :descriptionend
  24. 24. app/views/ypto/show.json.rabl object @xpto child :owner do attributes :id, :name end
  25. 25. https://github.com/rails/jbuilder
  26. 26. app/views/xpto/show.json.jbuilderjson.(@xpto, :id, :text, :created_at, :updated_at)json.children @xpto.children, :id, :name, :description
  27. 27. app/views/ypto/show.json.jbuilderjson.(@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
  28. 28. class XptoController < ApplicationController respond_to :html, :json def show @xpto = Xpto.find params[:id] respond_with @xpto endend
  29. 29. Obrigado! @rs_felixfelix.rafael@gmail.com

×