Unlimited Plugins, WordPress themes, videos & courses! Unlimited asset downloads! From $16.50/m
Lessons:14Length:2.1 hours

Next lesson playing in 5 seconds


Free Preview: Create a Rails API


  • Overview
  • Transcript

Since its release many years ago, Ruby on Rails has become a staple of the web development industry. Today however, web architectures tend to favour the integration of separate services, or front-end code connecting to the back-end through JSON APIs.

Rails hasn't always had strong support for API development. However, community-built gems have filled the gap, and Rails is now an excellent choice for developing a server-side API. In this course, Envato Tuts+ instructor José Mota will show you how to get started with API development in Ruby on Rails to deliver data through JSON.

1. Introduction

1.1 Introduction

Throughout the years Ruby on Rails has become quite the piece of software. It has been adopted by many different developers around the world. It has turned out to be one of the staples in this industry. Now due to advances in technology and due to the way things are now done, services can be integrated with each other. Ruby on Rails didn't have API development in mind. After all, it was supposed to be the all-in-one framework. However, people had been discovering the great potential of using Ruby on Rails as an API server. Hi, I'm Jose Mata. I will be your instructor for the Build a Rails API course. You will learn how to use Rails as an API server. As a standalone gem, it has been adopted by a large group of people in order to extract the best parts of Rails that will suit the best case scenario for building APIs. You will learn how to keep your normal development while still noticing the differences. As always, we will take this as a practical approach. We will build an exercise, namely a restaurant manager. Something that's really simple, but it has a slightly more complex business model. For this course, I assume you have Ruby installed. You don't need to have Ruby on Rails installed already, because we're gonna use a separate gem. So don't worry about that as long as you have Ruby 2.2 installed. If you don't, check the installation notes that I put in the description below. There's a link in there you can visit in order to install it. If you're unfamiliar with Ruby check the beginner course. It's also in the links below. Feel free to jump out to that course and then come back in. So I hope you're excited about this course. Let's jump to the next lesson to learn about the context in which we'll cover the demo application. I'll see you soon.

1.2 Model Our Business Data

Before we jump straight to the code, I want to give you a small introduction. I want to cover the data entities that we're going to need in order to develop our API. After all, we're gonna consider just a small server that will render plain JSON. We're not gonna handle templates or views or anything like that, just data. And since data is the primary attribute of our application, we're gonna need to make sure that it's thought out at least a little bit. So for a restaurant application I'm consider several entities. The first one I'm gonna call it table. A table is actually very simple to consider because a restaurant has a lot of tables. Each one of them usually has a number, and perhaps a number of seats. That's good for a reservation purposes. If you wanna book a table that has at least a certain number of seats available, you wanna make sure you book it. So it just makes it a lot easier for the owners of the restaurant to run their business. So we're gonna have at least a table entity. As for using the tables in our business, we're also going to consider an order data entity. Each order matches one client visit. Someone comes by the restaurant and books a table right away. The order is what's going to hold all of the information regarding that customer. For example, to keep track of which table he sat on as well as different dishes that he ordered, so that he or his family can enjoy the meal. The order will be the responsible for holding all of that information. Now associated to the order, we're gonna have at least two more. The items and a special entity that I'm going to grey out, it's going to be order items. Let me just put this in italic. Remember that this is not as important as these three. So the items will basically be a dish or something like that. Each dish that the chef can prepare an order by the customer. It's going to reside in an items table. Now the way you relate the items with the order is through this order_items pseudo entity. This will basically be a support table in our database that will hold how many portions of one specific item, or dish, was ordered by the customer. So it will hold a relationship between the order, the item, and, for example, a quantity. If I order two desserts instead of just one, then this is going to come really in handy. Instead of having duplication and stuff like that. So this will be very important as well. At the end, we want to make sure that we create a receipt. The receipt will have more than just the order. I mean of course it will relate to the order, but it will also have other kinds of information. For example, the amount of tip, as well as the amount of tax you had to pay for that meal. Also, you might want to register what kind of method the customer used to pay, either through a credit card, a standard debit card, or plain cash. That kind of information shouldn't really be placed in the order. The order respects the entire process of ordering food and managing its relationship with the table as well. The receipt is kind of a different universe than the actual process, the receipt is there maybe for administrative purposes or for tax paying. Still we're gonna keep it in there. So this will be our main order of business. So we're gonna keep the table, order, items, and order items. So let's just put those in order. Remember the table will be the physical instantiation of a place for the customer to sit and eat his meal. Then the order will have information on the customer, at least just a small detail, maybe, a name and email or something like that. Of course you can extract that kind of information into a separate entity called customer. Let's actually type that in. For this exercise we're gonna keep it simple and really not include this customer information like so. We're gonna shove it into the order. So, as I was saying, the customer info would beyond order as well as for example, when the order was placed, which is actually going to mean when the customer arrived. And then, this is going to have a relationship between the items and the order. This order_items pseudo entity will hold information on how many items of one dish were ordered by this customer. At the end, we're gonna have order related to a receipt. Again this is just for maybe tax paying purposes and for holding specific kinds of information related to perhaps the payment method or a tip that was given, stuff like that. So this is just a general overview that I wanted to provide. This will give you a much broader perspective on the whole picture. With that said, let's jump into the next lesson and learn how to install Rails API.