An externally usable API for a basic to-do list application built using Rails 5.0 API Mode.
Welcome to my to-do-list-api, an API that allows users to modify user accounts and to-do items from the command line.
This is something I’ve wanted to jump into for a long time. Using Rails 5.0 to build an API would further develop my backend skills, and in my opinion build a backend how it should be built, entirely separate from the frontend and fully agnostic.
This is not supposed to be a finished product but was part of the learning process of my web development course with Bloc covering core programming topics.
To create this application as a sole developer, working remotely, completing specific tasks outlined by Bloc.
It was time to delve further into the world of backend development. Become more familiar with servers, databases, MVC1, routes, JSON, API’s and HTTP requests. And overall develop a far more holistic understanding of web applications.
I was provided with User Stories 2 that I had to comprehend and complete.
- As a user, I want the To-do API, to return JSON representations of users, lists, and items
- As a user, I want to authenticate myself from the command line, using a username and password
- As a user, I want to create new users, lists, and items from the command line
- As a user, I want to remove users and lists from the command line
- As a user, I want to update list and item attributes from the command line
I used these User Stories to map out the workings of the backend.
On top of the above I was expected to implement TDD3 using RSpec.
I used Git to maintain a local repository of the project and a remote repository on GitHub, and used feature branches to ensure a smooth workflow and secure version control.
- When the API returns the desired data, it returns all the data, some of which is sensitive.
- Allow users to authenticate with the API.
- Give the user the ability to update items using the api.
- I needed a way to filter the JSON that was being sent to the user in the response body. For example, if the user requested to see all users in the databse, this should only return the username and email and not the password.
It was time to leverage the power of a Ruby Gem called Active Model Serializers.
Using relatively simple code I could ensure for all my objects that only certain data would be returned.
Now in my controller when I would render the JSON of an object which shared the same name as a Serializer:
the JSON being returned would automatically be filtered according to my preferences.
2. This was far more tricky than it seemed.
I had built authentication systems before, one without any helper gems and one using a great little gem called Devise. However, when it comes to API’s, Rails standard format for authentication runs into some issues.
As far as my understanding goes, this is based around the problem of CSRF4 attacks. A standard non-API rails projects handles this well by obtaining the users session token from the HTML. This means that only someone who is physically making a request from the sites view will be authenticated and not a hacker from another computer.
However, this is an API, so our users wouldn’t have any HTML and thus would always fail this form of authentication.
In fact in Rails API mode this default form of authentication
:verify_authenticity_token is already turned off.
Time to build our own form of authentication:
In every controller I simply added the line:
With every request made to the API a user would have to authenticate with their own username and password. Arguably, this doesn’t offer the most user-friendly access, but it does ensure better security.
3. Requests and Responses are the life blood of any backend application.
I first created a route to allow access to the relevant resource through an HTTP request.
Before I wrote the
update method, I wrote my tests inline with TDD.
Now time to write the actual method:
Great all my tests were passing, so for my own gratification I could now actually make the HTTP request using a really great platform called Postman.
And there you have it, the ability to update items using the API.
Note: These were not necessarily the hardest problems or challenges faced when building this application, for more complex functionality please feel free to look over the source code on Git Hub. However, the idea is that you can get a feel for some of the problems and my systematic approach to them.
My main form of testing for my rails API was using RSpec. This helped me to catch any bugs far quicker and meant I didn’t need to be overly concerned when refactoring my code or changing other features in the application. I also tested things using
rails console and Byebug, which proved invaluable.
I was able to achieve the desired outcomes of the course project and more importantly I was really able to further my understanding of Ruby on Rails and all the accompanying technologies, RVM, RSpec, servers, HTTP Requests, Ruby Gems, Params etc…
Feedback from my codementor was positive.
Going into this project, I was doubtful over the ability to get to grips with the workings of an API, especially as this was real backend work without any views (unlike a standard rails project where you do have views).
However, although it took me time to get my head around this, it just feels like the right way to have a backend functioning. It makes the frontend far more flexible and allows the backend to be truly agnostic.
Looking back, this was a great start for getting to grips with the general make up of an API. However, I would like to have understood more about user sessions and authentication, to have this set up in a more user-friendly way. Moreover, I would like to actually have a frontend which connects to the API, just to give me a better feel of the entire full-stack process.
Having said this, one of the most valuable things I learnt from this project was getting a better understanding of JSON, API’s and how Rails works under the hood. This project forced me to play around with HTTP requests and especially the
params hash which rails utilises.
Going forward, it would be great to develop more complex and robust API’s. I would really like to become more comfortable with this form of development and actually deploy an API, and then go on to build a frontend to interact with this API. Personally, I feel it makes sense to build out applications this way because it allows far more flexibility on the frontend side of these things. Although, admittedly it is harder work than just doing it all in rails with rails views.
1: The Model-View-Controller (MVC) is an architectural pattern that separates an application into three main logical components: the model, the view, and the controller. Each of these components are built to handle specific development aspects of an application.↩
2: A user story is a tool used in Agile software development to capture a description of a software feature from an end-user perspective. The user story describes the type of user, what they want and why. A user story helps to create a simplified description of a requirement.↩
4: Cross-Site Request Forgery (CSRF) is an attack that forces an end user to execute unwanted actions on a web application in which they’re currently authenticated. CSRF attacks specifically target state-changing requests, not theft of data, since the attacker has no way to see the response to the forged request.↩