 All right. Thank you for coming in My name is Nick. I'm from Germany. I came all the way just to talk to you guys about hyper media and rest I'm a Ruby guy. So there's nothing special to tell about me like I have a couple of jams I have a couple of projects and stuff like that if people ask me what I'm working I usually say I'm a self-employed scientist That means nothing That sounds cool and so usually companies hire me for doing like Improvement on their software architecture So I usually go there write ten frameworks and then they kick me because I didn't write any application code But then I publish the frameworks on github and I get famous and can do talks in Rocky Mountains, which is cool So I'll stop talking about myself. I think you guys came here in order to get some rest Got the word play All right, so we got a 20 27.8 minutes to go. I will talk a little bit about rest and about My view of the main constraints and rest. So who's using who's doing a rest project here? Who did a rest API or come on guys come on guys who's using rails? Who's using Facebook? This is impressive three people All right, then I will talk a little bit about representations and documents Which is the same hypermedia and my own gem which I'm going to try to sell. It's called raw and so rest That's representational state transfer Interesting so I took what I took did is and I took right Roy Fielding's thesis He's the author of rest who read the thesis This is impressive like 10 people. Did you learn anything about rest from the thesis because I didn't Sorry Roy if this is on video So the the main the main point about rest is in chapter 5, which is about rest in line 1 rest is an architectural style For distributed hypermedia systems. So keep in mind. It's architectural style and it's about distributed systems The problem with architectural styles is you cannot Simply write a gem Included in your rails application, and then it's restful The problem is with architectural styles. You have to be you as the architect or as the developer You have to worry about how do I introduce rest? How do I make my application restful and I'm trying to in my talk. I'm trying to Help you guys Getting on the right way about rest because what rails does is not really restful. I'm sorry. So rest Is about machines basically. It's about machines talking to each other like they Ask things and they would answer with machine readable answers the center of rest is the resource a resource can be just anything like I don't know maybe some beer presentation or some news feed or the current currently locked in user or whatever and Sorry, I got a drink some Every resource in the rest system has a unique URL. So a network address which is identifying their resource And you can manipulate that resource by using the uniform interface. Let's get post put delete and it's It's roughly defined what these operations do on resource And you can also manipulate resources using representations. That's the representational part of rest so rest is basically sending and Retreating documents representations are documents. All right. I Said all right There you go All right, so rest the rest approach and the rails rest approach is about them Usually is about having a monolithic application. You got pretty URLs. They call it restful. I like that and You use get in order to retrieve the representation of models maybe Jason maybe maybe XML and They also use get in order to make so the resource is not only serving Jason but also HTML in order to provide some user interface and Yeah, basically you use post to create or update models So they do some kind of stuff like faking put request and so on and so on and so on the rest approach I'm trying to Show today is about componentized applications. That's the distributed part So you don't have one monolithic application But you may have several separate applications and you still got pretty URLs. So how cool is that? And I'm talking about real rest services like you use get put post delete the uniform interface all the operations HTTP gives us to To manipulate resources and usually a rest service is not supposed to provide HTML Maybe I'm wrong, but that's my that's my impression about rest and one crucial point about rest is that you use hypermedia in order to connect services and This is something rails Didn't teach us So in order to show you hypermedia a little bit. I got some imaginary system. It's called burp It's some beer ordering service So maybe we got some order and then we can browse beers and then we can choose beers Put them in the order and in the end we'll say order and the waiter will come and bring us beers. That's nice By the way burp is the abbreviation for a beer shop system using restful backend services and a separate presentation layer. I Knew you're gonna like that. So, let's see a typical rest session And on the next slide, there will be HTTP requests On the left side is the is it left for you as well? Yeah on the left side is the rest service on the right side is the rest client alright, so Example I do a get to to arrest service and I The request might also contain some document This doesn't make sense to get but maybe input and I get back some response or some document from the rest service. All right, good So what if I get? beers slash one usually I get back some Jason representation, so I'm using a simplified Jason version because all the quotes and stuff they make things more complicated I guess you know what this is like it's Jason for for presentations So the rest approach is like you allow gets for getting resource representations and you allow Like you can do post requests to oh You still can read it you can do post requests to manipulate a resource like sorry to create a resource So in this case, I'd like to create some new beers and some new beer for my burp system and After you post you get some redirect telling you hey cruel you created a new beer and it's located at beers slash Yaver who knows you ever by the way. It's a good beer from Germany You should try more beer from the other side of the world. I Like the IPA stuff the guys have here So Rest in in the rails view is like get post put delete map to create show create update and delete Yeah, and so this is basically crud and so I say crud is not rest Rest is more so let's have a look at some typical hyper media driven Rest session because you remember that quote from Roy Fielding. He says rest is an architectural style for distributed systems So let's check out the hyper media part of that again, I'm using the burp system and the first step when doing a hyper media system like a real rest restful software system is You have you can you should Like separate the front end and the back end services So on that on the left side I have my graphical user interface, which is just a rest client a client and on the right side I have my rest services and Another step in real restful systems is like you you don't have to use one application as a front end one application As a back end you can even split up your back end into completely separated applications They you don't have to use one application for rest you can use multiple and they are interconnected using hyper media So let's start with a get so I'm trying to get the order slash one and I get back some Jason like the typical stuff ID created at and so on and I put in some some pseudo stuff. So I put in some hyper media. So that's hyper media. It's basically some kind of link and Some kind of URL so in order to proceed my order. I Would use the proceed link which is orders slash one slash pay So whenever I decide to pay my order I would I would send a put request to this URL and to update my order I would use the order slash one and I would do post put request on this URL. So I embed Sorry, and I embed links in my representation This is what they called. I don't know how you pronounce it like hate OS or whatever It's called it's hyper media as the engine of application state. So did anyone stumble upon that kind of term so far? Because I think it's a funny abbreviation, but it says me nothing So what I think about hyper hate hate OS or whatever is to embed actions or links in your representations That make sense in the current application state So proceeding my paying my order makes sense or updating my order makes sense So I just put links in the order representation. All right and hyper media Has different forms so you can embed hyper media in different ways So the typical way is like and this is again some pseudo draw Jason, but it's a little bit more Jason like The typical way is to have some links key in your representation in your two-dimensional representation and well links is an array of hypermedia links pointing to some resources Typically hypermedia link has a rail attribute and an href attribute. So the rail is the is telling me Roughly what to do and the href is the url In xml this looks like this so you can also use xml us For providing representations in your rest Assistant that I'm I did this talk a couple of times of course for you I'm improved it and throughout all the complicated stuff just to make it easier to follow Because in the in the first talk I used XML as an example for representations And people would kick my ass because nobody's using XML obviously Yeah, so let's go to Jason All right, so keep in mind Every hypermedia link in a restful representation has the rail attribute in my examples Which is like the link semantic proceed update items, whatever like telling me the meaning What what what what this resources is about and then we got the resource url. We are actually actually Requesting or setting a request So how do I create a new order? for my beer placement Well, I would post to orders And I can also put maybe some stuff in the in the request document body like the client ID or whatever just so to provide some initial initial values and What I get back in the end is some Enriched document telling me okay your order was created and there are Several links already embedded in my representation But the main point here is in a real hypermedia driven system My front end Knows one single entry point url nothing more. So we know one url in this case. This is hgp colon slash slash orders There is no more url computation For if I want to add an order if I want to browse order Sorry, if I want to add a beer if I want to browse beers if I want to place my order I don't do any url computation. So the rest client just knows one single entry point url So in my in my eyes, this is one one of the key criteria in order to make your system restful And um in usually have some something called the self link, which is just um Um a link pointing to the resource that just provided me the representation So this is another best practice. I won't go into the details here, but this is like um Uh things you should do in your representations. So in my order representation. I already I have some Link called the self link. I I I call it the clipboard link just so we can refer to it And um, yeah, we got some other link called the beers link, which is pointing to orders slash one slash beers and it's basically Um the link to all the items placed in my order All right, so when I place beers in my order, I can look up these items in the beers link So the icon for the beers link is like these three Small glasses We'll come to that in a minute So Now I created some order using post What if I want to check my order like the state how many beers are in there? What's the total price or whatever? Well, of course, I use get order slash one And this will give me like the representation um of my order sorry and So please notice that there is also hypermedia in my representation So I got the link to my to to the order itself and I got the links the link to the beers like how many beers are in my order So if I follow this beers link from my rest client I do orders slash one as get orders slash one slash beers And I get back. So this is a different resource like The resource is like telling me what beers are in my order and I get back some empty list of beers Of course because I didn't place any order right right now and I also have again hypermedia in that representation And this is interesting. So we we got the self link which is pointing to the resource itself boring and we got the um order link like this Um items collection is pointing to the order it belongs to All right. So I could theoretically Theoretically go back to the order which contains these kind of items or these kind of beers So How do I do how do I add some beer or some some some item to my order? I do in my in my example. I do a post to orders slash one slash beers and I just post Some beer name like using jason again in the document body oops Oh, I thought there is some some more um Animation, but it it got lost. So usually when I when I post to some resource it will redirect me to the newly created resource I just I just um Try to create So, okay, I post it to orders. I created a new order And then I added some items. So let's reload the order like getting the original representation again it's again getting orders slash one and cool So there are all these Properties we already had and now the beers array contains An item because I placed the anchor steam beer into that order Is anchor steam any popular beer here because I had it in San Francisco. It's quite good People are nodding I couldn't find it on any menu here in the bar. So maybe it's not popular in Boulder All right, so let's look at this um Let's look at our order representation after I placed a beer into it It's a massive document And we are not going to study all of the all of that stuff. So we still got the order ID We got the client ID. Then we got the beers array. We got links to the order and we got links to the order beers oops So we got the the self link pointing to the order We got the beers link pointing to the items in the order and we got some new link I call it the Plus or yeah, like the anchor steam link which is pointing to um to the item. I just pushed into the order So this url looks funny, right? Order slash one slash beers slash anchor steam. So what is what is this url referring to is it referring to a resource? Of course, it is referring to resource because every url in the rest system is a resource But it's not pointing to the anchor steam article It's pointing to the anchor steam beer put into the orders into order number one. All right So it's a matter of it's a matter of your choice. How you expose resources in your rest system So, yeah, that slide was massive. I need to take a short break So, do you know what what's funny? And usually when I do talks I'm pretty hung over But yesterday the United States of America the greatest country in the world, by the way Saved me from getting drunk and you know how this works I forgot my idea at home So thank you federal laws Okay, so So now we we did create an order. We placed some items in it Using hypermedia and htdp requests. So what if I want to remove the beer I just put into the order of the anchor steam beer what if I want to remove it again like um, yeah making it Making more order empty again I had a hard time finding finding some picture for this slide So I was looking for something like a person talking to a waitress and saying no, sorry So I typed in no sorry and on google an image search and what I got was this picture No, sorry I'm not sure what are they doing like is it some kind of meditation or anyway So in order to remove that anchor steam from my order I would use the uniform interface again and I would do a delete on my order slash one slash beers slash anchor steam in Just in order to delete this resource and if I delete this resource it means Take this beer out of my order because I don't want it. I want local IPA stuff How many breweries you got here in Boulder like 10 or 11 All right, so Let's try them tonight. All right. So I removed the beer from my order Let's check the order again. So I do another get on my order slash one And wow, this is fascinating my beers. Um, my beer's array is empty. So this seemed to work Okay, this was a lot of um this was a lot of HTTP requests and it's no problem if you couldn't really follow Because I couldn't follow myself as well But what I'm trying to what I'm was trying to point out is that I was posting to the single entry url slash orders HTTP orders, sorry Then I was doing a get request to orders slash one because The post to orders told me hey, you got a new order. It's located at orders slash one So they will get request to get that order In that in that representation of my created order. There was some link to the beers Resource and I didn't get just to just to check out how many beers on my order and there were no beers at all So that was boring. So I posted to the beers link Which was embedded as hypermedia in my orders representation I posted to create some to add some new item to my order I didn't do any url computation at all Then I reloaded the order again And I already have the url order slash one And then I decided okay. I don't want anchor steam. So I do a delete request To that strange long url In order to take out the beer of the the anchor steam from my order And I didn't do any computation of urls in my in my whole system. So at any time I could extract the urls I need in order to do things I could extract these urls from my representations And this is the point about hypermedia in In rest system. So it's like it's like a little bit defining your api Or exposing your api using your representations. And this is um Something that rails didn't teach us. I don't know why because every time I'm I'm refactoring some rest And some rest system people do url computations everywhere and this of course makes your system buggy So what what if you change? What if I decide okay the beer sling is too long? I don't want the beer name in it I want the id in it. So you have to change all your front and all your client code as well And do we like that? No Okay, so what I I just showed a small workflow of my order system What I didn't do was stuff like I could also update my order sending a put request to order slash one Or I could remove all items at once when I do a delete request to order slash one slash items and so on So this was the a conceptual part and I just have three more slides to show you how um How this kind of stuff can be done in ruby because it's a ruby con for not a rest con So I have an uh, I have a small gem called roar. I already said the name It's the abbreviation for resource oriented architectures in ruby. How cool is that? It's a github A pot of nick. That's me. All right. So in my gem roar. I have some new abstraction layer. It's called representers and So I could base I can basically define documents or representations by using uh by deriving from the class representer And then I can define plain properties like the name or the amount of alcohol or whatever you need in your document and I can also use The link methods in order to provide um hyper to embed hyper media in my document And the cool thing is so roar is not working in rails only it's working in Each in ruby but in rails you can use the url helpers in order to compute um hyper media links So the this is just another example. So you can you can also do like Okay, thank you You can also do like a nest Represenders and stuff like that and you can use you can use link like multiple times in order to embed More links in your representation And the funny thing is it's very simple to use in for to use in rails So you just have the new method called represents Which tells my controller which represented to use And then you use respond with and it won't render the Document using the typical stuff like 2j's and or whatever, but it will use my representer And the same stuff works. Um, so this was rendering but you can also use representers for um parsing incoming documents This is done by using the representation method, which is just a parsed um hash of um Of my incoming document And again, I use a representer to render so this is just a very brief overview if you don't understand no problem Come to me ask me. I can show you some code The point about representers is it's framework agnostic. It's not tied to rails or something you can use it everywhere It's um, it's basically object oriented documents And it's the point is it's bidirectional. So it's used for rendering and parsing representations incoming and outgoing documents And what I forgot on this list is like You can also use your you can use the representers in the client layer in your rest client and you can use them in your Services layer. So in the back end And um, we we got some more stuff for example in if you're using a representer in your client layer you can do like a HTTP you can extend your object dynamically and you can do get request in order to get some some document and then you can modify that document object oriented and then you can Send it back using put request so representers come with all that kind of stuff And they also have some very good hypermedia support So you can extract hypermedia links from your documents using object oriented approaches and not rails Magic parsing and stuff like that. So if you go on the on the github page There is a complete example how to use that kind of new abstraction layer If you like rest if you want to go further and learn more about hypermedia I recommend you the following books rest and practice who read it Anybody okay two two people and I recommend I really recommend the restful web services cookbook So this is a great book. You can just skim over and read Two pages and you're still and you get enlightened and on every page So it's like showing you the tips and tricks of rest design And So raw feeling did a very interesting blog post. It's called hyper rest apis must be hypertext driven Where he claims that rest systems Must be hyper hypertext or hypermedia driven. That means No url computation anywhere in the rest client Only hypermedia extraction and following these links. It's very funny. If you read the comment section There are lots of people who disagree So, okay, two things I gotta ask First time the first thing is if you guys are hiring and you want to have some handsome german programmer For like maybe four four months to one year in boulder or in san francisco or any other cool area Ask me I would be interested and I'm planning to do some hiking the next day So if anyone could lend me some tent and cooking equipment, it would be great All right, um, so again, this is the link to my to my gem the hypermedia rest thing and I'm on twitter I'm apotonic, so I just started using twitter Follow me or not Tell me that you like me or not And um, so this was my talk about rest and hypermedia I hope you got some little insight and I think we don't have time for questions or maybe there are no questions One question in the front Are Repeat the question so when I do a post to create some resource and it takes a long time to create that resource How do I handle that? How do I handle that timing issue? Right? I don't have any clue So this is um, I mean there are of course a couple of approaches, but um How do you come up with that question? I mean You've done it So this talk is about hypermedia and not about performance scaling So I'd like to I'd like to discuss that but I'm right now. I don't have like a Magic rule of thumb. So I'm sorry Anything else the guy in the blue shirt? Authentication is again Um, this is like one of the things I really hate about rest that you have to do like a stupid Authentication stuff and you have to resend that authentication errors always in your in each request Is that what you're planning to? Again, there are a couple of approaches, but this is not connected to hypermedia How do you do it do you have any I mean I would be interested in how you do it So You're referring to a resource, okay And the resource is an actual glass of beer you can drink I'm not sure how to drink HTTP colon slash beer slash one That is a resource, but if you're getting it then you're getting a representation of that glass and I wouldn't drink that representation because it's a document Thank you guys. You've been a wonderful audience Yeah, I thought the whole example was you're placing an order with a waiter And the beer gets delivered to you charlie So you will get that beer and you will really drink it Unless you forgot your ID