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

Post on 27-Aug-2014

574 Views

Category:

Software

2 Downloads

Preview:

Click to see full reader

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

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

javier ramirez@supercoco9

20 years ago

15 years ago

the web of portals

sharing via widgets

ScreenScrapping

SOAP Services

mashups andintegratedservices

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

REST saved

theweb

REST is anarchitecturefor the web

Roy Fielding's PHD Dissertation. 2000

REST & RAILS

Rails saved the

web

Client – serverStatelessCacheableMulti-layered

REST constraints. HTTP basics

uniforminterface*

REST constraints. The good parts

* Convention over configuration FTW

resourcesrepresentationsmetadata

uniform interface. Elements

resource:Anything thatcan be named

uniform interface

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

representationsstandard formats/mime-types

uniform interface

uniform interface: representations. HTML

uniform interface: representations. JSON

REpresentationalStateTransfer

GETPOSTPUT/PATCHDELETE

uniform interface: transfer

uniform interface: transfer. GET (no id)

uniform interface: transfer. POST (no id)

uniform interface: transfer. GET (with id)

uniform interface: transfer. DELETE (with id)

HTTP as usual +resourcesrepresentationsmetadata

So REST is basically

Routes: resources,member,collection

Controller: respond_to,format,status,headers

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

Rest in Rails

Rest in Rails: basic routes

Rest in Rails: advanced routes

Rest in Rails: advanced routes

Rest in Rails: members and collections

tips on routes: don't overload

GET /users/1-javier/messages

get :messages on: :member

tips on routes: don't overload

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

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

tips on routes: don't overload

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

members? No.

Naming is hard.

But very important

tips on routes

Resources are not modelsDon't expose your implementation details

tips on routes

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

Rest in Rails: controllers. Basic actions

respond_to & format

Rest in Rails: controllers

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

Rest in Rails: controllers. headers

representationsRest in Rails: views. templates

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

Hypermedia

Navigable APIs

API pagination

API links------Next steps

Several resources in a single request

API/Hypermedia Ruby gems

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

https://github.com/intridea/grape

https://github.com/apotonick/roar

RESTful web service/application

client-server,stateless,layered,cacheable Resources

Resource IdentifiersUniform interface

operationsRepresentations

MetadataHypermedia

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.

top related