What is a Rails API?
Application programming interface or API, is a software intermediary that allows for two applications to talk to each other. In other words, an API is the messenger that delivers your request and responses from the front end to the back end of your program (client side rendering).
API’s are designed to do different tasks, these tasks are decided based off of the business needs. API functionalities are independent of the respective implementations, developers can make something as simple as rendering some JSON data, to having to deal with authentication and authorization of data with login capabilities.
Rails API provides a set of defaults that allows developers to get up and running quickly, without having to make a lot of trivial decisions. According to the Ruby on Rails documentation:
For the handling of middleware layer:
- Reloading: Rails applications support transparent reloading. This works even if your application gets big and restarting the server for every request becomes non-viable.
- Development Mode: Rails applications come with smart defaults for development, making development pleasant without compromising production-time performance.
- Test Mode: Ditto development mode.
- Logging: Rails applications log every request, with a level of verbosity appropriate for the current mode. Rails logs in development include information about the request environment, database queries, and basic performance information.
- Security: Rails detects and thwarts IP spoofing attacks and handles cryptographic signatures in a timing attack aware way. Don’t know what an IP spoofing attack or a timing attack is? Exactly.
- Parameter Parsing: Want to specify your parameters as JSON instead of as a URL-encoded String? No problem. Rails will decode the JSON for you and make it available in
params. Want to use nested URL-encoded parameters? That works too.
- Conditional GETs: Rails handles conditional
Last-Modified) processing request headers and returning the correct response headers and status code. All you need to do is use the
stale?check in your controller, and Rails will handle all of the HTTP details for you.
- HEAD requests: Rails will transparently convert
GETones, and return just the headers on the way out. This makes
HEADwork reliably in all Rails APIs.
Handled at the Action Pack layer:
- Resourceful Routing: If you’re building a RESTful JSON API, you want to be using the Rails router. Clean and conventional mapping from HTTP to controllers means not having to spend time thinking about how to model your API in terms of HTTP.
- URL Generation: The flip side of routing is URL generation. A good API based on HTTP includes URLs (see the GitHub Gist API for an example).
- Header and Redirection Responses:
redirect_to user_url(current_user)come in handy. Sure, you could manually add the response headers, but why?
- Caching: Rails provides page, action, and fragment caching. Fragment caching is especially helpful when building up a nested JSON object.
- Basic, Digest, and Token Authentication: Rails comes with out-of-the-box support for three kinds of HTTP authentication.
- Instrumentation: Rails has an instrumentation API that triggers registered handlers for a variety of events, such as action processing, sending a file or data, redirection, and database queries. The payload of each event comes with relevant information (for the action processing event, the payload includes the controller, action, parameters, request format, request method, and the request’s full path).
- Generators: It is often handy to generate a resource and get your model, controller, test stubs, and routes created for you in a single command for further tweaking. Same for migrations and others.
- Plugins: Many third-party libraries come with support for Rails that reduce or eliminate the cost of setting up and gluing together the library and the web framework. This includes things like overriding default generators, adding Rake tasks, and honoring Rails choices (like the logger and cache back-end).
That is a lot that rails generates for you. It is much more than just a simple view file with your JSON data. You can customize it to do many different tasks, but it is a framework that provides a possibility to quickly build an API for your test and use cases.