How to build an API using Rails – Part 1

The Rails 5 is out, and of course it came with a lot of new features and improvements, on this tutorial I’m going to cover the Rails API feature.

With all the Javascript stuff coming out, we have a lot of tools for building APIs these days, we have Express, Sails Js and many others. How about Ruby Developers? The most cool feature of Rails 5 is Rails API.

Rails::API is a subset of a normal Rails application, created for applications that don’t require all functionality that a complete Rails application provides. It is a bit more lightweight, and consequently a bit faster than a normal Rails application. The main example for its usage is in API applications only, where you usually don’t need the entire Rails middleware stack nor template generation.

Before you starting doing something you should download the most recent version of Ruby and Rails. I’d recommend using RVM for managing many versions of Ruby.

With all set the first command you should run is:

rails new rails_api_tutorial –api

This command will generate only the basic structure for building an API. It will make the ApplicationController inherit from ActionController::API instead of ActionController::Base. It will also configure the generators to skip generating views, helpers and assets when you generate a new resource.

The next step is to setup the testing stuff like: Rspec, FactoryGirl, Shoulda-Matchers and Rails Controller Testing.

After you adding all those gems your Gemfile should looks like this:

bildschirmfoto-2017-02-02-um-08-22-34

Hit bundle install to install those gems, and check the documentation of each one for more information about putting them to work.

After that you should create a folder called models inside of the spec/ folder, which will contain all the model specs of the API.

With all set, now we will create the first model spec of the application. Inside the spec/models folder create a Ruby file called post_spec.rb

The things the should be tested on model are: Table Fields, Validations, Relationships and any method you’d create. 

We are going to create a simple table for handling our posts, but first we need to create the spec for it:

spec/models/post_spec.rb

bildschirmfoto-2017-02-02-um-09-00-14

If we run this spec it will shows an error, because we haven’t implemented the Post model yet. Let’s do it:

bildschirmfoto-2017-02-02-um-09-13-08

We also need to create a migration with all the fields. For creating a migration run:

rails g migration post

After that, open the migration and add the fields:

bildschirmfoto-2017-02-02-um-09-39-24

Now you can migrate:

rails db:migrate

And prepare the migration for the tests:

rails db:test:prepare

After all you can run the command:

rspec

And all the tests should  pass:

bildschirmfoto-2017-02-02-um-09-39-53

The first part of this tutorial we covered:

  • The creation of an API only Rails Application
  • The setup of all the testing stuff
  • The creating of the Post model

On the next part of this tutorial I’m going to cover about controllers, how to test them and so on.

The source code of this tutorial is available on my Github: https://goo.gl/7Gr8WC

Do you like this post ? Would you mind to follow it ?

See you guys on the next part.

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s