Ruby on Rails: API Template (Part 1)

Tram Ho

In this article, I would like to share about the template that I am working at the current company step by step to help you have the standard template when building an API App.

Oh forget, in this article also talked about what it means to install gems

I. Setting up API Rails App

First, initialize the project with API with the –api option

Note that this option is only supported from Ruby> = 2.2.2 and Rails> = 5.0.0.

In this article, I use the rails version 6.0.1

II. Using RSpec for Testing

1. Install RSpec & Simplecov:

  • The reason for installing RSpec first is because it will save us time by using the RSpec generator, it will automatically generate test controller and model files when we use the rails g scaffold command to manually Create resoures quickly.
  • To install RSpec, add gem rspec-rails to Gemfile in group : development ,: test

Gem factory_bot_rails helps me create the necessary object for testing. Combine with gem ffaker to create objects with random values.

Gem shoulda-matchers provide a method for writing concise test cases. See more syntax here

Gem simplecov helps to statistically% the coverage of unit tests that you write.

Proceed to update the bundle, then install RSpec

2. Config factory_bot

  • To use factory_bot’s methods, you need to configure rspec to get factory_bot’s syntax.

In the file spec / rails_helper.rb:

3. Config shoulda-matchers

At the ngay đầu spec / rails_helper.rb file add:

At the cuối spec / rails_helper.rb file add:

4. Config simplecov

At the ngay đầu spec / spec_helper.rb file add:

5. Config ffaker

In the directory spec / factories / create a file with a name corresponding to the object to create.

For example, create users.rb file in the following format:

After running the test, you can open file coverage/index.html in your browser to view it.

Put the coverage folder in .gitignore

III. Integration Rubocop & CI

1. Set up Rubocop:

1.1. Install rubocop gem into Gemfile

With Ruby 2.5.x and earlier

With Ruby 2.6.x onwards

1.2. Download the zip file corresponding to the rubocop version installed in step 1:

With Ruby 2.5.x and earlier Download the file

With Ruby 2.6.x and above Download the file

Then copy 3 files in the downloaded zip file:

Paste into the project directory, on par with Gemfile.

1.3. Run rubocop before each commit sends pull with the command:

2. Set up CI

Depending on the specific CI requirements of each company, you can install it according to the company’s requirements

2.1. Run a CI test

2.2. Authorize the CI report file

2.3. Review the reports of CI just run in the report folder corresponding to CI just installed:

2.4. Put some Ci report files in .gitignore

IV. SETUP DATABASE

Add dotenv-rails gem to set .env environment variable

In database.yml, config is as follows:

Then create the DB

V. Building Your API

When the app is initialized with the –api option, we can use the default generator scaffold to generate API resources as usual without any special parameters.

When finished, it will create the following file structure:

Run the migrate DB and run the app:

BECAUSE. Serializing API Output

In view we often use jbuilder to manage the data returned as JSON, but in API app we will use AMS (Active Model Serializers) to manage this. AMS provides a layer between the model and controller by calling to_json or as_json for the object / collection ActiveRecord, while still exporting the format the API wants.

Add Gemfile:

Update bundle:

Create a serializer for the User model:

It will create file:

By default there will be attr : id , adding attr if you want to display more attributes of object / collection in API as follows:

This part is quite long, so I divided it into 2 parts, see you later

If there is anything missing or additional comments, please read the comment so I can improve it. Peace!

Share the news now

Source : Viblo