what is rest. why is it part of the rails way?. railsconf 2014. javier ramirez

66
What is REST? Why is it part of the rails way? javier ramirez @supercoco9

Upload: javier-ramirez

Post on 27-Aug-2014

574 views

Category:

Software


2 download

DESCRIPTION

When David Heinemeier Hansson started talking about REST in 2006, little could we suspect it'd become such a central part of Rails (and of web development in general). Back then a web service meant something you coded using XML and SOAP. Those were dark times. REST changed it all. In this talk, I'll explain REST for beginners, I'll talk about why it is such an important architecture, and I'll show all the nice things Rails offers you for building a RESTful application or a RESTful API.

TRANSCRIPT

Page 1: What is rest. Why is it part of the Rails way?. Railsconf 2014. Javier Ramirez

What is REST?Why is it part of the rails way?

javier ramirez@supercoco9

Page 2: What is rest. Why is it part of the Rails way?. Railsconf 2014. Javier Ramirez

20 years ago

Page 3: What is rest. Why is it part of the Rails way?. Railsconf 2014. Javier Ramirez
Page 4: What is rest. Why is it part of the Rails way?. Railsconf 2014. Javier Ramirez
Page 5: What is rest. Why is it part of the Rails way?. Railsconf 2014. Javier Ramirez
Page 6: What is rest. Why is it part of the Rails way?. Railsconf 2014. Javier Ramirez

15 years ago

Page 7: What is rest. Why is it part of the Rails way?. Railsconf 2014. Javier Ramirez

the web of portals

Page 8: What is rest. Why is it part of the Rails way?. Railsconf 2014. Javier Ramirez

sharing via widgets

Page 9: What is rest. Why is it part of the Rails way?. Railsconf 2014. Javier Ramirez
Page 10: What is rest. Why is it part of the Rails way?. Railsconf 2014. Javier Ramirez

ScreenScrapping

Page 11: What is rest. Why is it part of the Rails way?. Railsconf 2014. Javier Ramirez
Page 12: What is rest. Why is it part of the Rails way?. Railsconf 2014. Javier Ramirez

SOAP Services

Page 13: What is rest. Why is it part of the Rails way?. Railsconf 2014. Javier Ramirez
Page 14: What is rest. Why is it part of the Rails way?. Railsconf 2014. Javier Ramirez
Page 15: What is rest. Why is it part of the Rails way?. Railsconf 2014. Javier Ramirez
Page 16: What is rest. Why is it part of the Rails way?. Railsconf 2014. Javier Ramirez
Page 17: What is rest. Why is it part of the Rails way?. Railsconf 2014. Javier Ramirez
Page 18: What is rest. Why is it part of the Rails way?. Railsconf 2014. Javier Ramirez

mashups andintegratedservices

Page 19: What is rest. Why is it part of the Rails way?. Railsconf 2014. Javier Ramirez
Page 20: What is rest. Why is it part of the Rails way?. Railsconf 2014. Javier Ramirez
Page 21: What is rest. Why is it part of the Rails way?. Railsconf 2014. Javier Ramirez
Page 22: What is rest. Why is it part of the Rails way?. Railsconf 2014. Javier Ramirez
Page 23: What is rest. Why is it part of the Rails way?. Railsconf 2014. Javier Ramirez

https://api.github.com/users/javier

Page 24: What is rest. Why is it part of the Rails way?. Railsconf 2014. Javier Ramirez

REST saved

theweb

Page 25: What is rest. Why is it part of the Rails way?. Railsconf 2014. Javier Ramirez

REST is anarchitecturefor the web

Roy Fielding's PHD Dissertation. 2000

Page 26: What is rest. Why is it part of the Rails way?. Railsconf 2014. Javier Ramirez

REST & RAILS

Page 27: What is rest. Why is it part of the Rails way?. Railsconf 2014. Javier Ramirez

Rails saved the

web

Page 28: What is rest. Why is it part of the Rails way?. Railsconf 2014. Javier Ramirez

Client – serverStatelessCacheableMulti-layered

REST constraints. HTTP basics

Page 29: What is rest. Why is it part of the Rails way?. Railsconf 2014. Javier Ramirez

uniforminterface*

REST constraints. The good parts

* Convention over configuration FTW

Page 30: What is rest. Why is it part of the Rails way?. Railsconf 2014. Javier Ramirez

resourcesrepresentationsmetadata

uniform interface. Elements

Page 31: What is rest. Why is it part of the Rails way?. Railsconf 2014. Javier Ramirez

resource:Anything thatcan be named

uniform interface

Page 32: What is rest. Why is it part of the Rails way?. Railsconf 2014. Javier Ramirez

resource identifiers:URI

uniform interface

https://api.teowaki.com/teams/teowaki-community

https://api.teowaki.com/teams/fosdem-2014

https://api.teowaki.com/users/javier

http://localhost:3000/posts/1

Page 33: What is rest. Why is it part of the Rails way?. Railsconf 2014. Javier Ramirez

representationsstandard formats/mime-types

uniform interface

Page 34: What is rest. Why is it part of the Rails way?. Railsconf 2014. Javier Ramirez

uniform interface: representations. HTML

Page 35: What is rest. Why is it part of the Rails way?. Railsconf 2014. Javier Ramirez

uniform interface: representations. JSON

Page 36: What is rest. Why is it part of the Rails way?. Railsconf 2014. Javier Ramirez

REpresentationalStateTransfer

Page 37: What is rest. Why is it part of the Rails way?. Railsconf 2014. Javier Ramirez

GETPOSTPUT/PATCHDELETE

uniform interface: transfer

Page 38: What is rest. Why is it part of the Rails way?. Railsconf 2014. Javier Ramirez

uniform interface: transfer. GET (no id)

Page 39: What is rest. Why is it part of the Rails way?. Railsconf 2014. Javier Ramirez

uniform interface: transfer. POST (no id)

Page 40: What is rest. Why is it part of the Rails way?. Railsconf 2014. Javier Ramirez

uniform interface: transfer. GET (with id)

Page 41: What is rest. Why is it part of the Rails way?. Railsconf 2014. Javier Ramirez

uniform interface: transfer. DELETE (with id)

Page 42: What is rest. Why is it part of the Rails way?. Railsconf 2014. Javier Ramirez

HTTP as usual +resourcesrepresentationsmetadata

So REST is basically

Page 43: What is rest. Why is it part of the Rails way?. Railsconf 2014. Javier Ramirez

Routes: resources,member,collection

Controller: respond_to,format,status,headers

Views: show.html.erb, show.json.jbuilder

Rest in Rails

Page 44: What is rest. Why is it part of the Rails way?. Railsconf 2014. Javier Ramirez

Rest in Rails: basic routes

Page 45: What is rest. Why is it part of the Rails way?. Railsconf 2014. Javier Ramirez

Rest in Rails: advanced routes

Page 46: What is rest. Why is it part of the Rails way?. Railsconf 2014. Javier Ramirez

Rest in Rails: advanced routes

Page 47: What is rest. Why is it part of the Rails way?. Railsconf 2014. Javier Ramirez

Rest in Rails: members and collections

Page 48: What is rest. Why is it part of the Rails way?. Railsconf 2014. Javier Ramirez

tips on routes: don't overload

GET /users/1-javier/messages

get :messages on: :member

Page 49: What is rest. Why is it part of the Rails way?. Railsconf 2014. Javier Ramirez

tips on routes: don't overload

GET /users/1-javier/messagesPOST /users/1-javier/messages

get :messages on: :memberpost :messages on: :member

Page 50: What is rest. Why is it part of the Rails way?. Railsconf 2014. Javier Ramirez

tips on routes: don't overload

GET /users/1-javier/messagesPOST /users/1-javier/messages

members? No.

Page 51: What is rest. Why is it part of the Rails way?. Railsconf 2014. Javier Ramirez

Naming is hard.

But very important

tips on routes

Page 52: What is rest. Why is it part of the Rails way?. Railsconf 2014. Javier Ramirez

Resources are not modelsDon't expose your implementation details

tips on routes

Page 53: What is rest. Why is it part of the Rails way?. Railsconf 2014. Javier Ramirez

Easier to understand

Change the internals without breaking the contract

Resources based on business objects are more resistant to versioning

More opacity means more security

Page 54: What is rest. Why is it part of the Rails way?. Railsconf 2014. Javier Ramirez

Rest in Rails: controllers. Basic actions

Page 55: What is rest. Why is it part of the Rails way?. Railsconf 2014. Javier Ramirez

respond_to & format

Rest in Rails: controllers

Page 56: What is rest. Why is it part of the Rails way?. Railsconf 2014. Javier Ramirez

huddle200 OK 201 Created202 Accepted400 Bad Request401 Unauthorized403 Forbidden404 Not Found410 Gone

twitter200 OK Success!304 Not Modified400 Bad Request401 Unauthorized403 Forbidden404 Not Found406 Not Acceptable420 Enhance Your Calm500 Internal Server Error502 Bad Gateway503 Service Unavailable

Useful Status429 Too many requests204 No Content

teowaki200 OK Success!201 Created304 Not Modified401 Unauthorized403 Forbidden404 Not Found422 Unprocessable Entity406 Not Acceptable500 Internal Server Error

Page 57: What is rest. Why is it part of the Rails way?. Railsconf 2014. Javier Ramirez

Rest in Rails: controllers. headers

Page 58: What is rest. Why is it part of the Rails way?. Railsconf 2014. Javier Ramirez

representationsRest in Rails: views. templates

Page 59: What is rest. Why is it part of the Rails way?. Railsconf 2014. Javier Ramirez

But I'm not building an API.Do I need REST?

Page 60: What is rest. Why is it part of the Rails way?. Railsconf 2014. Javier Ramirez

Hypermedia

Navigable APIs

Page 61: What is rest. Why is it part of the Rails way?. Railsconf 2014. Javier Ramirez

API pagination

Page 62: What is rest. Why is it part of the Rails way?. Railsconf 2014. Javier Ramirez

API links------Next steps

Page 63: What is rest. Why is it part of the Rails way?. Railsconf 2014. Javier Ramirez

Several resources in a single request

Page 64: What is rest. Why is it part of the Rails way?. Railsconf 2014. Javier Ramirez

API/Hypermedia Ruby gems

https://github.com/rails-api/rails-api

https://github.com/intridea/grape

https://github.com/apotonick/roar

Page 65: What is rest. Why is it part of the Rails way?. Railsconf 2014. Javier Ramirez

RESTful web service/application

client-server,stateless,layered,cacheable Resources

Resource IdentifiersUniform interface

operationsRepresentations

MetadataHypermedia

Page 66: What is rest. Why is it part of the Rails way?. Railsconf 2014. Javier Ramirez

Find related links at:https://teowaki.com/teams/javier-community/link-categories/rest

Thank you

Javier Ramírez@supercoco9

https://airpair.me/javier

teowaki is the place to share links, best practices, documentation, gossip and lifehacks with other developers.