RailsConf 2014

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.

00:00:16.720 Okay, so my name is Javier Ramirez, and my Twitter handle is super9, in case you want to ask anything during the talk or later. I'm one of the founders of Teowaki.
00:00:25.400 Teowaki is a small startup where software developers can communicate with each other, share links, information, and documentation, among a few other things. On the technical side, Teowaki is a service built with Rails and is completely RESTful.
00:00:39.000 But what is this REST thing? When I say something is RESTful or it utilizes REST, what does it mean? Why is it so important for Rails? The moment you start using Rails, you must begin thinking in terms of REST and all the associated concepts. That’s what I want to discuss with you today.
00:01:05.159 Before we dive into REST, let me take you back to how the internet was about 20 years ago. Web pages were quite basic. If you visited Yahoo, Amazon, or Apple around that time, you would generally find just static information and maybe some banners—that was about it.
00:01:17.240 However, around 15 years ago, things started to change. Suddenly, we had more interactive pages. You could integrate information from other services like weather updates or news content. The way developers accomplished this was through widgets, where external services provided JavaScript you would embed into your page.
00:01:50.439 The downside was that if these widgets appeared terribly ugly, you often had no control over their design. Additionally, for developers, this approach was frustrating because integrating different services was cumbersome. Screen scraping became a popular resort to obtain data from various sites. Screen scraping involves downloading a web page, parsing the HTML to locate the desired data, and extracting it from a specified position in the structure of the page. This method was error-prone, as any changes to the web page structure could render your code useless.
00:02:26.040 Fortunately, some developers recognized the need for APIs, allowing access to their services without resorting to screen scraping or using unattractive widgets. However, back in those days, many APIs were built using a protocol called SOAP, which while functional, was not user-friendly. The complexity of SOAP meant that crafting a simple request could quickly become a nightmare, often requiring intricate XML structures to perform even basic tasks.
00:03:10.159 SOAP was so complicated that it required something called WSDL (Web Services Description Language) to define all possible service operations. Different services would differ dramatically in naming conventions for their operations, making cross-service integration particularly challenging. This often resulted in cryptic error messages if anything went awry, leaving developers frustrated with the experience.
00:03:49.000 Despite these challenges, today we live in a different internet, with a plethora of apps and integrated services. Now it’s possible to assemble complex features with just a few lines of code. For instance, a mashup might involve pulling in images from Instagram and displaying them in a seamless infinite scroll on a web page. Whether it’s displaying geographical data or querying various services, today’s integration capabilities make it significantly easier to interact with various APIs compared to the past, and that is largely due to the advent of REST.
00:04:47.800 REST stands for Representational State Transfer, and it's an architectural style for designing networked applications. Roy Fielding, one of the co-authors of the HTTP specification and the co-founder of the Apache web server, introduced REST in a doctoral dissertation in 2000. While the dissertation was complex and not easily digestible, it effectively encapsulated the foundational concepts of REST, which gained greater traction after David Heinemeier Hansson incorporated REST principles into Rails in 2006.
00:05:37.160 When Rails adopted REST, it transformed the way developers thought about building applications. It allowed for a standardized method of building APIs, which had been absent until then. REST utilizes HTTP as its core protocol—meaning when you design applications and services, you should adopt HTTP as your foundation. Each request rests statelessly on the server, meaning that each request honestly stands alone without the server holding any context of earlier requests. As you build applications, understanding these principles becomes paramount.
00:07:26.439 REST introduces a uniformity to the interface of web services, facilitating ease of understanding and predictability for developers. By knowing the basic HTTP methods, like GET, POST, DELETE, and PUT, developers can design applications that will function similarly, no matter the specific service. All they really need to know are the resource names specified in their API and which HTTP method to use in conjunction with them. This approach significantly simplifies working across diverse APIs.
00:09:25.840 In Rails, defining routes and actions allows you to map these REST principles directly to how users interact with your application. For example, you can define resources easily within the routing file. When defining routes, Rails will generate the CRUD operations automatically tied to the corresponding URLs for resource management—this greatly simplifies the workflow for developers, allowing them to focus less on configuration and more on building features.
00:10:39.120 RESTful design influences your controller setup as well. When scaffolding a new resource in Rails, you receive standard actions—index, create, edit, update, destroy—that are universally applicable across your application. This uniformity enhances the development experience, leading to a more robust and predictable application architecture.
00:11:48.799 As for representations, Rails facilitates generating different formats for responses. Depending on the request from the client, you can easily vary the representation sent back—HTML, JSON, etc. This flexibility allows your application to serve diverse clients efficiently, whether they need a webpage or a JSON API response.
00:12:54.440 Despite its advantages, REST is not solely limited to APIs, as it promotes a structured way of thinking for all web applications. In fact, adhering to RESTful principles in web applications fosters scalability and maintainability, allowing future developers to onboard quickly and understand your service’s architecture.
00:14:07.840 While developing applications, it's important to understand some advanced REST concepts too, such as hypermedia. Hypermedia is crucial for enhancing navigability within your API, providing clients the links to related resources. Unlike traditional web pages, RESTful APIs can dynamically share these interconnections to streamline user interactions.
00:15:27.000 By incorporating hypermedia elements into your API responses, users are empowered to navigate via links just as they do on a website. This can significantly lower the barrier to entry for new users of the API and create a seamless experience when accessing various functionalities. Ultimately, the flexibility REST provides—alongside its guiding principles—forms the backbone of a robust, efficient web application structure reliant on HTTP where resource management and accessibility are at the forefront. Thank you very much for your attention. I'm happy to answer any questions you might have.