List

What is REST? Why is it Part of the Rails Way?

What is REST? Why is it Part of the Rails Way?

by Javier Ramirez

In the video titled "What is REST? Why is it Part of the Rails Way?" presented by Javier Ramirez at RailsConf 2014, the concept of REST (Representational State Transfer) is introduced as a foundational architectural style that revolutionized web services. Ramirez explains the significance of REST in comparison to earlier web service protocols such as XML and SOAP, highlighting how REST simplifies web development by allowing easier integration of services. He begins by outlining the evolution of web communication, detailing a shift from basic web pages towards a more dynamic web characterized by integrated services.

Key points discussed include:
- Definition of REST: REST is presented as an architecture for the web, emphasizing statelessness and the use of HTTP for communication between clients and servers.
- HTTP Protocol: Understanding REST begins with mastering HTTP, which is vital for any web application or service built with Rails.
- Uniform Interface: REST allows for a consistent way of accessing services, which is facilitated by clear definitions of resources, their representations, and metadata.
- Resources and Representations: Resources are anything that can be named (e.g., users, teams), while representations refer to the data format returned (e.g., JSON, XML).
- CRUD Operations: The four main operations in REST — Create, Read, Update, Delete — are standardized, making integration simpler.
- Routing in Rails: Ramirez illustrates how Rails uses routing to map URLs to controller actions, demonstrating the ease with which Rails can create RESTful applications.
- Best Practices: Emphasis is placed on using meaningful names for resources, avoiding verb-based naming, and considering the implications of nested resources for maintainability.
- Hypermedia: Although not fully implemented in Rails, he introduces hypermedia as a concept that enables navigation through APIs by providing links alongside resource representations, enhancing user experience.

The talk concludes by reinforcing the benefits of adopting REST principles not just for API development but also for traditional web applications, as it contributes to cleaner architecture and simplifies onboarding new team members. Ramirez encourages developers to embrace these conventions in both API design and overall application structure, thus enhancing scalability and maintainability.

By Javier Ramirez

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.

Web developer, daydreamer and all around happy person.

Founder of https://teowaki.com where we try to make developers happier by helping them share technical information, best practices, gossip and lifehacks with their developer friends.

Help us caption & translate this video!

http://amara.org/v/FG1y/

RailsConf 2014

00:00:16.720 okay so uh my name isier r and Twitter super9 in case you want to ask anything
00:00:25.400 during the talk or later and uh I'm one of the founders of t
00:00:30.599 which is a small startup where software developers can communicate with each other sh links information documentation
00:00:39.000 and a few other things and uh on the technical side chaki is a service built
00:00:44.600 with rails and is totally restful but what what is this rest thing why when I
00:00:51.879 say something is restful or it's rest what does it mean and why it is something so important for rails why the
00:00:58.920 moment you start doing rails you have to start thinking of rest and all the things so that's what I want to tell you
00:01:05.159 today but before that I want to tell you how it was to make a web or how how was
00:01:11.280 the internet 20 years ago and 20 years ago web pages were quite easy you were
00:01:17.240 if you went to Yahoo or to Amazon or to Apple like about 20 years ago you will
00:01:24.119 get this you have only information about Apple and maybe some banners and that's
00:01:29.360 it but bit later like 15 years ago something started to change suddenly we
00:01:36.600 have like corals and you have like pages with information coming from other pages
00:01:42.000 so it meant you have a web page and you could integrate information from other services like weather information or I
00:01:50.439 don't know lot of things so the way of serving these things was with widgets so
00:01:56.920 basically if the other service was giving you a they will give you a JavaScript and you will put it on your
00:02:03.159 page and then it will appear on your page exactly the way the other service
00:02:08.759 intended so if it was terribly ugly and they were you didn't have any control
00:02:14.080 over that some of them were kind of customizable but it was quite ugly so as a
00:02:19.840 developer this this didn't make me exactly happy because you know I could
00:02:26.040 integrate like quite quite easily and sometimes I needed to integrate things from other services and the only way you
00:02:33.800 could do that was with screen scrapping screen scrapping is something really amazing in which you basically download
00:02:40.720 the other web page then you par the HTML try to find the information you want
00:02:47.319 exactly in the second div below I don't know which whever and then extract this
00:02:55.319 uh information here and yes I had to do a screen scrapping from lot of sites for getting weather information or whatever
00:03:02.959 and it was really easy to make mistakes if they were changing the pages it will be it will be broken and this no this
00:03:10.159 didn't make me happy either uh if you were really lucky some people were thinking you know what we are going to
00:03:16.680 give you an API so you can you can use information from my website without
00:03:22.879 screen scrapping or without an ugly widget but tring is at the moment like
00:03:28.280 15 30 years ago even today in some places uh the API was built with
00:03:34.159 something called soap which is a protocol for communicating information between services and it works but it's
00:03:42.319 not really easy to use this is for example uh the example for creating a
00:03:49.040 new ad in AdWords using the soap API so if you want to create a new ad you have
00:03:55.360 to compose something like this which I I don't I know you don't see anything but
00:04:01.519 it's better that way believe me because it's it's really it's really really ugly so soap it's okay for creating an app
00:04:09.439 with a name and some URL I have to compose something like this and call
00:04:15.040 some operation which is called in this case create or mutate or I don't know what and at operation at okay and soap
00:04:24.199 was kind of kind of complex it was so complex that for yeah it was complex I
00:04:29.800 mean that's that's the face H you get when you see soap it was so complex that
00:04:34.919 you actually need to have something called like a web service definition language and you will have to specify
00:04:41.320 all the operations that were available in your service and in this case for adwards one operation is called get
00:04:49.000 another is called mutate another is called query but in different service the name of the operations will be like
00:04:55.120 totally different so there was no way you could integrate a service straight away way without reading lot of
00:05:01.360 documentation if you were lucky or reading this if you were not lucky and
00:05:06.680 when you were not lucky you will get errors so you have another funny XML
00:05:12.080 with all the different errors and telling you what can be wrong and blah blah blah blah blah I wasn't really
00:05:19.680 happy with the services either it was not ideal to inter it was much better than the other tools is still not
00:05:25.840 exactly the same but today we are living a different a different internet
00:05:30.880 different web today we have mass apps and integrated Services everywhere today you can have with a few lines of code
00:05:37.840 yes you know getting instacat this page is for real you are getting pages from Instagram you getting all the pictures
00:05:44.639 with the T of a cat and you have like infinite scrolling why I don't know it's kind of fun and yeah and this is like a
00:05:51.960 try like the typical Mass up you have like the sensus information uh from the you know from the US office whatever and
00:05:59.400 then you are you can put that information on a map so you are basically using two different services and composing a website which is like
00:06:05.960 using services from different places or you can have like a search for hotels
00:06:11.160 give me availability and price and show a map of all of the ones that are like uh in a public transport distance like
00:06:18.400 15 minutes from here that is quite amazing it's like quite interesting or even if you hungry you can put here the
00:06:23.919 post Cod of this hotel and run the Wheel of Lunch and it's going to tell you today we are having lunch at frona fres
00:06:30.039 and they get the information from jelp and you know so the good thing of this is integrating the services is really
00:06:36.560 easy all you need to do is point to this URL this is my profile in GitHub you go
00:06:42.360 to this URL in your browser or in any client line terminal and you are getting JavaScript back with my information
00:06:50.039 really easy to integrate you don't need to do soap you don't need to compose anything like weird yes call a URL get
00:06:57.000 something back so by having the is really easy way of integrating things
00:07:03.720 the web is much more powerful now you can use information from other places and you can p your information so people
00:07:10.319 can find it and yeah in that way Dr save the web because now it's much more
00:07:16.160 Dynamic and it's much much cheaper to build Services uh what is dress then dress is
00:07:23.039 an architecture for the web Roy Fielding which is this guy uh he is one of the uh
00:07:29.800 creators of uh the HTTP protocol he's one of the co-founders of the Apache web
00:07:34.960 server so you know he's been around the web for a long time and he wrote a dissertation for know the PHD in 20 in
00:07:41.039 the year 2000 explaining what is rest there was one trolling it was a PhD
00:07:47.680 dissertation meaning is written so no one can ever understand what it's saying
00:07:53.199 no one at all if if someone can understand it you are not going to pass so you know you have to be careful with
00:07:58.440 that when you are doing a PhD and he did he was really good and encrypting everything so if you try to read the
00:08:05.159 original dissertation good luck with that I mean I I I do it a few times I'm
00:08:10.319 not recommending it so this was in 2000 I'm telling you that even I don't know
00:08:16.120 10 years ago it was not really common to H to see dress Services services in the
00:08:22.960 internet withing rest why was that because until 2006 basically no no one
00:08:30.039 out of the academic War knew what was rest and it was thanks to rails that the
00:08:36.200 world basically knew about rest 2006 ra com David harir Hansson by the by that
00:08:42.680 time he was really into tdd but you know to he he was he was I mean I I respect
00:08:49.600 people changing opinions but you know yes so at that time he said hey you know
00:08:55.040 I read about this thing called bre and it's quite amazing and I'm going to to put rest into rails so we are going to
00:09:02.440 build our applications and our services in a totally new way that no one knows what it is but it's kind of cool and the
00:09:09.000 thing is the rails Community Ruby community and also rails it's quite amazing that way we like trying out new
00:09:15.680 things we like like breaking because if you were using rest your all applications wouldn't be working I mean
00:09:22.079 it was like basically bad Wass incompatible so what it was fun so yeah it was like let's make rest and the
00:09:28.640 other community started paying attention and shortly after that everybody was
00:09:33.959 doing restful Frameworks and rest compliant blah blah so in that H in that
00:09:40.040 sense rails said the web because if it wasn't for rails we wouldn't have well
00:09:45.480 probably probably will have I don't know someone else will have made this popular but it was us so that's cool uh so
00:09:52.200 what's rest so rest I told you it's an architecture for the web the basics are really easy the basics is are uh the web
00:10:00.640 is HTTP so if you are doing any Dr application or any Dr service the first
00:10:07.079 thing you need to to do is uh just Embrace HTTP you are going to have a
00:10:12.480 client and a server server you know and the client is going to be the browser or your phone or any Library you used for
00:10:19.519 HTTP it's going to be stateless stateless means every time I'm sending
00:10:25.399 information to the server I need to send the whole information that the server
00:10:31.120 needs this this H what it means is I can't assume the server knows anything
00:10:38.240 about the request I sent before so when you are doing a a website if you're
00:10:44.480 using website in rails you have the session you have the cookies so in every session in every request you are sending
00:10:50.920 the session the cookie session so H the server can read them the information from database but you need to pass the
00:10:57.320 information every time if you're using rails you have already you should be it's important that you design your
00:11:03.360 services thinking that the web is a place where things can be cash in different places so so think about that
00:11:10.200 too and multi-layer means that toally between the client and the server you're
00:11:15.560 going to have like different layers like a proxy or something like that so this
00:11:21.399 is like basic HTTP and is if you are doing rails you have already discovered
00:11:26.560 without doing any effort at all so I'm not going to talk a lot about this but I'm going to talk about the other part
00:11:33.240 of rest the most important in my opinion which is the uniform interface one of the beautiful things
00:11:40.519 about rest is it's going to give you a uniform way of doing services so if you
00:11:48.120 know rest and you are going to consume a service from I don't know any company if
00:11:55.959 you have already used rest any other time you already know how to use it you don't need to read lot of
00:12:01.040 documentation see which operations are supported which are the possible errors you know there is a uniform interface
00:12:07.639 which is what I'm going to tell you now basically and it's quite easy and this is what we are talking all the time
00:12:13.279 Yehuda today in the morning was saying conventional configuration in rails we have a lot of conventions and when you have
00:12:19.320 conventions it's quite easy to advance in the world because since everybody else is doing the same thing is very
00:12:25.399 very easy to understand things and to build on top of others so what is this uniform interface it has just three
00:12:32.480 elements it's really really easy it has something called resources it has something called
00:12:38.079 representations and it has something called metadata so what's a resource a resource is anything that can be named
00:12:45.519 like for example in my application we have users and teams and links and
00:12:52.279 messages so yeah user will be a resource team will be another resource message
00:12:57.480 will be another resource so anything that can be a name if if you are thinking of Ruby or of rails like
00:13:05.160 anything that you could create a class 4 could be a resource so anything with a name that's a resource and since you
00:13:11.279 need to identify different resources if I have users I want to have like know to know which is user a and user B in rest
00:13:19.880 you always need to have a resource identifier when you are working in r with active record the identifier is the
00:13:26.680 ID on your on your table in rest the identifi the UR I Ur I and URL are kind
00:13:33.680 of the same if you are like techn like really technical some people are like no URL is different than URI I but you know
00:13:39.399 for this to they are like exchangeable so if you have a URL that's it you have
00:13:45.199 an identifier so in rest this uh this URL identifies a resource this URL
00:13:53.759 identifies another this URL identifies another and this URL which is like the typical URL when you make an escaf
00:14:01.120 identifies another so it means this is the ID for my resource like the whole
00:14:06.720 URL okay that's easy so for every resource in your system you need to have
00:14:12.519 a unique URL it's nothing new but just the basic of of rest the other part of the the
00:14:20.000 second element is the representations representen me means okay I have a URL which is the IDE of
00:14:26.880 one resource and now I'm going to H ask for this resource and I'm going to get
00:14:33.000 some information back the information I get for the resource is called representation and you can have
00:14:38.279 different ones for example here I'm doing get you know yes HTTP get the same thing you'll do in the browser so get a
00:14:45.240 URL and I'm asking for HTML so I'm getting HTML like I don't know H body
00:14:51.920 head whatever okay I can ask for the same URL but just asking for Json and
00:14:58.519 the same the same URL which is exactly this the one I had before this and this one are the same so the same URL can
00:15:06.680 have multiple representations in this case I'm getting Json back so one thing
00:15:11.839 is the ID the resource and the other is the representation I have from the resource and the presentation can be
00:15:17.399 like different formats like XML Json which is the most popular right now but
00:15:22.519 could be a PDF could be an HTML you could have like a long Json and a short
00:15:27.959 Json for the same resour because sometimes maybe you want to have like the the full information for a user but
00:15:33.920 sometimes you want to have only the nickname and that's it and the picture whatever so representation is just the
00:15:39.600 information that you are going to show about your resources so that's it and by
00:15:46.319 the way the name rest it comes from this it's because the way of working with rest is you transfer representations
00:15:53.959 between server and client if you want to create a new user what you do is is you
00:15:59.360 send a representation you send a Json or a SML or just a form post from the from
00:16:05.600 the client to the server and that's how you are creating things you are transferring a representation when you
00:16:10.880 want to get information you do the same thing you ask for a representation from the server and you are getting a
00:16:16.959 representation back and that's the it's an ugly name but rest comes from that like representation transfer and how you
00:16:23.519 transfer representation in HTTP with these operations that's it in HTTP you
00:16:28.680 know you can get a page you can post like when you post a form you can delete
00:16:34.839 or you can put or patch which are that to be for updating information and the
00:16:40.959 beauty of rest is like in any rest service you can only do these four or
00:16:48.279 five things nothing else these are all the operations you can do so when you
00:16:53.639 are going to interact with a rest application or a rest service the only need the the only thing you need to know
00:17:00.160 is which are the resources which are the names in the systems if if you are going to work with an application which has
00:17:07.600 users and teams if you know okay this is the URL for users this is the URL for teams you already know how to create
00:17:14.559 things you just post information how to read things you just get information how to put and how to delete and that's true
00:17:20.880 for every API the only thing you need to know is exactly which Fields they are
00:17:25.959 accepting but you know that should be like quite easy you you you get a resource you get you know the resource
00:17:32.080 and then you can figure out what you need to send back for changing information so that that's it that's how
00:17:37.679 you are creating things in a in rest like in theory so for example how I how how can
00:17:44.919 I get information from a rest service I just get a URL in this case I get in the
00:17:50.840 URL you TR don't read it because I basically don't read it but it's like URL without any ID so I will have an a
00:17:57.960 list of resources if I post to the same URL and I post a Json here what I'm going to do is create
00:18:05.080 a new resource so if I get from one URL and just getting the list if I post to
00:18:10.720 the same URL what I'm doing is creating if I do the same thing with an URL with an ID if I get something here I
00:18:18.880 get the information and if I do delete for example the send URL and just deleting
00:18:24.280 the resource so that's how you operate with a bre service and how how is your
00:18:29.760 that's how your H application your explication is going to work so the summary for now because now I'm going to
00:18:36.280 tell you how to how to do this front rails is rest it sounds scary but it's
00:18:42.039 really easy it's just HTTP plus resources which are URLs plus
00:18:47.159 representations which is like XML HTML whatever Plus optionally metadata some
00:18:53.559 metadata about H asking okay the client can tell I want this information in Json
00:18:58.919 I want it in XML whatever how you do this in rails so in rails there are
00:19:05.080 three points in which you can do rest the first part is the road file the road
00:19:11.520 file is where you are going to Define which are the possible URLs these are my resources and these are the possible
00:19:17.880 URLs in my system the second part is the controller which is going is where you are going to be able to control the
00:19:25.159 metadata and where you are going to be able to decide which format to present
00:19:30.640 and the last part is the view which is the one dealing with their presentation itself so routin in rails quite easy in
00:19:40.520 rails you do something like if I want to have a resource which is post you just write resources post that's it you don't
00:19:47.400 do anything else and you already have in your application these URLs which are like
00:19:53.320 the URL for getting all the posts that is going to map to the index action in
00:19:58.520 the in the controller post I have the URL for creating a new post which is the
00:20:03.679 create action in the controller I have the URL for having an empty post for
00:20:09.799 creating a new one the one with the form I have one for editing an existing
00:20:16.039 resource I have one for getting the information from one resource two for updating and one for deleting so only
00:20:23.520 with one line you already have all the roots here that's quite easy even if your application is bigger that's the
00:20:29.000 real code for all the things that are below users in my in in our startup so
00:20:36.440 even if you have like you know quite a complex SC with exceptions and whatever
00:20:42.120 you will still get lot of roots but they are all the same they are always like get put post delete edit whatever so
00:20:48.919 it's uniform the moment you know which are the names you can already operate with the system you don't need to know
00:20:54.280 anything else so it's just more names but the operations are totally a
00:21:00.080 standard there are some things that you can do some things uh sometimes and to me it was a a bit confusion at first
00:21:07.799 which are members and collections sometimes the basic roots are not enough because sometimes you
00:21:14.799 want to have for example uh imagine a user can have a profile so I want to
00:21:19.840 have a URL like this for the users for this user ID I want to have a profile
00:21:25.799 how you do this so in real is called a member a member basically means okay I
00:21:31.080 have a URL with an ID already and I want to add in that controller I want to have
00:21:37.080 an extra action in this case we are calling it profile so I'm going to have an extra action which is a going to have
00:21:45.159 information about this resource so it's like extra information about this user
00:21:50.679 in particular so I create a member if you want to create if you want to have an action which applies to all the
00:21:57.640 resources like all the users that are that are public it doesn't have an ID so
00:22:03.960 what you do in real is creating a collection so you can do this and in this way you can have like URLs a bit
00:22:10.400 more complex than the basic ones of just having like ID and that's it
00:22:16.360 uh you can do that but uh I wouldn't recommend to do it like too much like
00:22:22.320 for example imagine you have uh One controller with your users and you want
00:22:27.799 to have a a list of all the messages that a user has received you could decide okay I can create a member for
00:22:35.200 this uh resource for users I will have a an action in the control in the usage controller that will be called messages
00:22:42.400 and by having this action and this member I will have this route what if I want to create also messages for the
00:22:48.640 user I could add a second member and I could just post to this URL so I can
00:22:54.320 create messages the moment you start doing things like this like post in to
00:22:59.440 things in the resources in my opinion most of the times you don't want to have
00:23:05.279 members for this what you want to have is in rails you can Nest resources so
00:23:11.039 you can have a resource users and inside a resource messages only for indexing and creating for example and if you want
00:23:18.480 you could have another collection or member inside so every time you are
00:23:24.279 thinking of creating a member or a collection in your resources just think twice if you really
00:23:32.720 want to create a nested resource not a member of collection but a full resource inside because the moment you start
00:23:40.000 overloading ER members and collections your controller is getting bigger and bigger and bigger and if your controller
00:23:46.720 is getting bigger they're going to happen two things first thing is it's more difficult to
00:23:51.799 maintain and second thing is it's getting further from the uniform
00:23:57.440 interface if you have Nest resources you're going to have a controller for users a controller for user messages and
00:24:04.760 the in both controllers you have the same methods index create update delete that's it the moment you start creating
00:24:11.600 members and collections you are going to start adding more methods so when someone else in your team is going to
00:24:17.279 work with a controller they need to start thinking oh I have extra methods what are they going to do so you know if
00:24:24.480 you if you find yourself defining too many members of collections think that maybe you should be talking about
00:24:29.840 nesting resources and in really is quite easy as you can see you don't have to do anything just you know nest and that's
00:24:36.000 it the other thing is naming is something really really really really
00:24:42.080 hard but when you are doing a service or when you're doing an API it's probably
00:24:47.559 the most important thing that you are doing because the names that you are putting it's how the external services
00:24:54.120 or the users are going to see your uh uh your service or your application
00:25:01.159 so I'm telling you this because H in rest when you define a resource you
00:25:07.360 should be using actually using names not using verbs like for example if you want to have like uh you want to create a
00:25:14.760 send an email for a user you sometimes you will do something like users ID H
00:25:21.919 slash send for example so it's much easier to do something like users ID
00:25:28.360 message and just post to the message as I as I did before so think of know having just names in the URLs never
00:25:34.640 having verbs and spend a a little time you know naming the things because it's
00:25:39.679 it's it's difficult at first but it really pays and I have another tip interesting for resources is your
00:25:45.440 resources are not models like no one needs to know how things are working on
00:25:50.720 the inside of your application if you have an application or a service from
00:25:55.919 the point of view from the point of view of the user or or the consumer all they need to know is I put this inside I I'm
00:26:03.360 I'm sending this I'm getting this representation I don't know what's inside and I don't need to know it like
00:26:09.399 for example uh in my application I have links and the links can have like tax
00:26:14.760 internally I have different tables links team links tax whatever that's
00:26:19.840 internally but I'm not exposing H all the resources by all the models with
00:26:25.559 URLs I have a resour which is link and in the link I have an attribute which is
00:26:31.240 the tax and some other things so when you are sending me the representation for a link when I send it back what I'm
00:26:37.679 doing internally is I'm getting information from different tables but that's cool because you don't care you
00:26:43.559 only care about what is a link from the conceptual point of view and know how I'm Story how I'm I'm storing the
00:26:50.640 information physically on database and that's something that in rails is very
00:26:55.679 easy to get wrong because in rails you like like generate escaf and you have
00:27:01.000 for every table for every active record model you have a resource that's cool
00:27:06.399 when you are starting when you are learning for doing things like Fast the moment you are doing like a real world
00:27:11.919 application a bit more complex you are going to get into problems if you always map one resource or one table uh to one
00:27:19.399 model of the of the application so try to think of the external interface and
00:27:24.559 not how you stowing the information if you do this the application is going to be easier to understand a user can
00:27:30.520 understand that a link is something with tax but if they have to send me like different request for adding first
00:27:37.640 create the link then create the tax then associate one with the other it doesn't add it's like it's like too difficult so
00:27:43.760 it's easier to understand and the other thing is today I have my data most of is in post andred but maybe tomorrow I move
00:27:50.880 into the moment I move to different database data store I'm probably going to change tables divide
00:27:56.640 in different blah blah if find mapping my physical data store to the URLs I'm going to break all the clients that are
00:28:03.279 using the APR or every who using the application if I'm not mapping directly
00:28:08.799 every resource to my tables I have all the freedom for changing the data store every time I want as long as I don't
00:28:15.720 change the URLs things are going to keep working so try to make that effort not mapping one model to one URL all the
00:28:22.760 time and also if you are versioning it's much easier to uh make a new version if
00:28:28.320 you are if you don't have like a one to one correspondence between models and
00:28:34.720 URLs uh second part so routine is for the URLs the second part you can do in
00:28:41.120 rails for the for rest is just the controllers in the controllers when you create an MTS scaold you always have
00:28:47.480 these basic op these basic actions index so new edit create update and Destroy
00:28:52.679 and they are always the same so the unifor interface in the controllers you don't have to do anything with that
00:28:59.760 and the second thing you have in the controllers is the respond to and the format respond to is like okay uh
00:29:07.679 depending on which format I'm getting see if I'm getting the okay I want to
00:29:13.559 have this uh user or this post in this case on HTML then I'm going to use this
00:29:20.399 line for HTML I'm going to do this for Json I want to do this if you asking XML
00:29:28.240 I don't have the format here so I'm going to give you an error so basically in the controllers you have the control
00:29:34.760 of which format which representations you are going to show to the user and something really interesting too that
00:29:40.919 sometimes is Overlook is the status code HTTP has a lot of interesting
00:29:46.919 status Cote not just 200 and 404 and
00:29:52.080 internal error you have like more status for being quite expressive if you remember when I was telling at the
00:29:58.320 beginning about soap I saw you a really ugly XML with all the possible errors blah blah blah that's because in soap
00:30:05.240 you don't have a standard way of representing different statuses but in HTTP you have it in
00:30:11.360 HTTP you have like lot of different status these are like the interesting ones so okay means you're asking for
00:30:18.960 something that's fine ER 404 is this resource doesn't
00:30:24.200 exist uh 2011 for example is you to create something I have created it if
00:30:30.720 you are going to create something but not now but you are going to send it to a background process and it will be created later then you don't answer
00:30:38.120 created you you just answer accept it it's like okay you sent up a request here apparently everything is fine I
00:30:45.240 accept the request I still didn't create it I will create it later so the the nice thing is all these status codes are
00:30:51.640 standard you look for them on the internet all the websites that use HTTP
00:30:57.080 properly that going to use exactly the same set of statuses so you don't need to tell the users hey I have this really
00:31:03.360 special code which is 12754 25 which means wherever no then the status are
00:31:10.639 here already and actually in rails you have symbols for them so for each of
00:31:15.960 these statuses you can do a status created a status and processable entity
00:31:22.320 a status okay and you're going to return directly the proper status code back to the client so it's quite easy to keep
00:31:30.000 the uniform interface and you know and and and make your service easy to understand for external
00:31:37.200 users um the other thing you can do in the controllers is controlling the metadata metadata when you are speaking
00:31:43.880 in rest you put that on the HTTP headers and HTTP headers in in rails are really
00:31:50.039 easy to control you have the header here what is it headers you know
00:31:57.360 keyword and the headers are going to give you access to all the HTTP headers so if I want to send metadata for
00:32:03.120 example in our case we are sending information about if you are using the
00:32:08.200 service for free or which is your plan when it's going to expire whatever so you can just add a metadata here for
00:32:14.200 okay format whatever you want and it's quite easy to use metadata here so if you want to send information which is
00:32:19.919 not part of the representation you can put it here people like sapos for example they are sending here
00:32:25.200 information like if you are reading this header then you should apply for a job with us so you know people are using
00:32:31.760 headers in really interesting ways uh in Twitter for example in the headers you
00:32:36.960 have the information about the API quot since you can't H call the Twitter API
00:32:42.159 more than 90 times per sec per minute something like that they tell you on the
00:32:47.279 headers hey you still have uh 10 requests left if you do more than 10 you
00:32:53.360 are going to so you know every time you want to send meta information to the request you put it on the header and
00:32:59.039 it's releas in rails yes you know headers name of the header value we are good to go you don't you don't have to
00:33:04.960 do anything else and for the representations which was the last element we already do the resources and
00:33:12.559 the metadata and representations in rails you basically have templates so
00:33:18.639 the same action can respond to HTML or to Json so in HTML you put the
00:33:25.320 information coming from the post in you put second information with J builder in the last latest versions of rails and
00:33:33.120 that's how you get different representations in rails so with this you're good to go too and you're going to tell me you know
00:33:41.320 everything you're telling me it sounds kind of good I hope you are thinking it sounds kind of good but uh if I'm not
00:33:49.039 building an API if I'm not building a service if I'm just doing a web application do I still need to care
00:33:54.960 about rest well yes for two reasons first because it's like part of R already so if you don't if you're not
00:34:00.840 going to use rest it's going to be actually more difficult that no trying to use it but the second thing is uh
00:34:08.159 rest give you a lot of constraints conventions so by using Rest by
00:34:14.119 separating resources in different controllers but making all your controllers respond to the same exactly
00:34:20.560 the same set of actions you're are going to have if you need to design your
00:34:26.599 application it's going to be much easier iier to design if you already have some some constraints because when you need to design something you have like a lot
00:34:33.079 of freedom it's quite difficult to know where to start from so if you're using the rest constraints it's really really
00:34:39.320 much easier to H design the system properly to make it easier to a scale
00:34:44.679 and to make it cleaner and the other thing is if you are incorporating new people to the team it's really easy for
00:34:50.879 them to understand what's going on so you know even if you're not building an API having a restful architecture is
00:34:57.720 quite interesting and since I still have a bit of time like five minutes left I'm going
00:35:03.160 to tell you another part of rest which I didn't tell you before not because I'm aad person but because in rails we don't
00:35:10.800 have this part of rest I am sure sometimes you've heard people discussing
00:35:17.040 about this stupid thing hey my API is fully restful no it's not restful it's
00:35:22.599 not exactly 100% restful because of blah blah blah blah blah the blah blah blah
00:35:28.240 blah blah is usually this part the hyper media so when Roy Fielding was designing
00:35:34.240 was describing what is the rest architecture hper media was a mandatory part of the architecture what means
00:35:41.280 hyper media it means when I'm building a website if I go to
00:35:47.000 google.com I get an HTML and if I want to go to another page from there I just
00:35:53.000 click on a link and I go to another page so on the internet you move around by clicking on links that's quite obvious
00:36:01.359 when you are building an API you should put links also into your API and most
00:36:06.440 people are not doing it so that means hyper media hyper media means along with
00:36:11.800 the representation you need to send links so I can navigate the API for
00:36:17.760 example if you're using GitHub in GitHub they are doing it in GitHub every time you are getting uh the information from
00:36:24.480 a user from wherever you have the information and you have also links for navigating the API so for example since
00:36:31.720 I know I don't work for G for tiaki this is our example and in tiaki if you are
00:36:36.920 asking for example for the information about one team I give you about all the team sorry I give you here the list of
00:36:43.640 teams but I'm going to give you which is the link that you should call on the API for getting the next page or the
00:36:49.960 previous page so instead of telling you hey for do pagination make this call and
00:36:55.640 then add a parameter and if you were in the page in the page number two remember to put page number three blah blah blah
00:37:02.760 I I I don't want you to do that I can do that for you so what I'm doing is I give you the information and I give you the
00:37:08.319 link so if you want to get the next page yes call this link for next page and I give you all the parameters in it so
00:37:15.200 that's quite it's it's easier to navigate so the same way that you can navigate HTML you should be able to
00:37:20.640 navigate apis and that way someone that is using the service for the first time it's much easier for them to use your
00:37:27.160 API because they only need to go to the homepage and they're going to have all the names of the available resources
00:37:33.160 they can just navigate to another resource they can see from there what they can go like for example here when
00:37:38.319 I'm asking for the information about a team an individual team it's like okay these are the public links for the team
00:37:44.680 these are the private links for the team this is your own URL this is the organization you belong to these are
00:37:50.760 your best practices these are your categories this is uh these are your users so if you want to see which users
00:37:57.079 are this team I only have to go to this URL I don't need to go to the documentation and read things so that's
00:38:02.400 the a media part it's quite interesting but also you can do something like sending a list with URLs to individual
00:38:07.960 resources whatever it's quite interesting but unfortunately it's not part of rails but you have interesting
00:38:15.079 gems that you can use for that if you want to incorp if you building an API
00:38:20.160 and you want to incorporate hyper media so you can boast with your friends my API is fully restful it's yours so yeah
00:38:27.839 so you can uh use any of these uh gems they are slightly different like uh each
00:38:34.560 of them in a in a different way uh my favorite will be Roar prob but I don't know it keeps changing because you know
00:38:40.800 they are like they are quite active so sometimes I like grave sometimes like Ro whatever so if you do this you're going
00:38:46.760 to be able to incorporate hyper media also to your application and that's basically rest rest is just uh
00:38:54.800 architecture for the web in which you have H HTTP then you have resources which are just URL with a fancier name
00:39:01.800 but it's just URLs nothing else unique URLs for unique things you have a uniform interface which is I always send
00:39:09.480 the same operations for against this URLs I don't have to know anything else
00:39:14.760 about the system and then you exchange representations and representations are just representations they are not models
00:39:21.160 they are representations uh you can have metadata and iper media but not with rails the
00:39:27.400 that's what I wanted to tell if you want to find a few related links you can find them here which is uh also in chaki
00:39:34.000 which is my startup and if you have any questions yes feel free to ask me thank
00:39:39.200 you very much