Leave a comment

Implementing Rails APIs like a professional

Josh Software - Where Programming is an Art!

Today, a web-portal that does not support APIs is not even considered a web portal! This post explains how we can implement Rails APIs in our application. Rails APIs require the following minimum implementation:

  • API keys.
  • API Versioning.
  • API Request Authentication.
  • Request throttling (optional).

Let’s see how this can be done.

API key generation

Lets generate the model.

or if the model already exists,

Now lets edit this model and add the logic to generate the API key when a record is created using the method generate_api_key.

In this code we use SecureRandom.base64.tr('+/=', 'Qat') to generate a token. Since, it may contain the special characters like +/=, we shall replace them with some random alphabets using tr, so that the API key looks good. Now lets add the before_create callback.

API controller and routes versioning

Now, lets generate the controller called Api in the api namespace. This…

View original post 575 more words

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

%d bloggers like this: