Ruby on Rails is a popular choice for development of web applications due to it’s simplicity and prototyping speed. Rails has a very complex and all inclusive toolkit out of the box which fulfills most requirements arising during regular web application development cycles. However, when requirement becomes more specific, even Rails’ enormous inbuilt tools seem inadequate. But thanks to the huge Ruby community, there are thousands of ruby gems out of which you can find the perfect gem to suit your requirements without having to code everything yourself.
Today, I will be introducing you to some of the most useful and well made gems in the Ruby language that are useful for Web Development with the Rails framework. I have been using these gems for quite a while now and have found them to be really valuable.
1. Sidekiq – Background Processing
Having to run all your code in the controller during the request – response cycle might not be ideal for a lot of cases especially if the code is time consuming. It is easily possible for such a request to just throw a timeout error to the user. Essentially you’d want to move the time consuming code to a separate process that runs in the background. Sidekiq runs on a separate process which will run any rails code contained in “sidekiq workers”.
The idea is that you’d call the sidekiq worker from a controller / model which will execute on the separate sidekiq process. Sidekiq uses Redis to enqueue and maintain job statuses. Sidekiq comes with a fancy dashboard that allows you to view all the running sidekiq instances, enqueued jobs and various other reports which is unique to sidekiq.
Sidekiq is extremely fast and has a huge set of tools & plugins to enhance its utility.
2. Pry – Debugging
Pry is a very intricate and utilitarian debugging tool for all kinds of ruby applications including Rails web applications. Pry allows you create breakpoints in your code and stop the execution at the breakpoints to inspect the code at the time of execution giving you access to all the application runtime binding, arguments and variables.
To create a break point in your code, you just need to add the following:
binding.pry calling the pry function on the binding object, will halt the execution and open up a pry repl interface (similar to rails console / irb) at that point at runtime.
The Pry console has many added methods that you can call to see a variety of information regarding the runtime environment and binding of the program.
3. Dalli – Caching
The Dalli gem is a perfect replacement for the memcached-client and is 20% faster than memcached-client gem. Dalli interacts with the memcached server to provide a hassle free caching experience. You can set it up as the default caching option in Rails in the environments/production.rb file and call the Rails’ built in methods for cache management.
Dalli offers a variety of options for logging the cache related events to make debugging easier.
4. Exception Notification – Error Notifier
When your application is in production, any errors that arise need to be looked into immediately. It is not practically possible to keep monitoring logs manually for issues, which is why you need the exception notification gem. This gem will trigger a mailer whenever your application encounters an exception, sending a mail to the pre-configured set of email ids.
The application has 8 different kinds of notifiers at present which will trigger different kind of notification systems. they include:
Campfire Notifier Webhook Notifier Slack Notifier Amazon SNS Google Chat Notifier Data Dog Notifier Email Notifier IRC Notifier Mattermost Notifier HipChat Notifier Teams Notifier
5. FCM – Web, Android & iOS Push Notifications
Push notifications are essential for any web application these days. While creating APIs for mobile device applications using Rails, the Push Notification system can be used to either communicate with the users of the application or to provide a channel for Server Side Communication for your application.
The FCM gem is a simple solution for creating push notification systems on your Rails web application. It is a set of ruby bindings for the Firebase Cloud Messaging API hosted by Google.
6. Wicked PDF – PDF Generation
Wicked PDF is the best gem for creating PDF files from HTML views on your Rails application. It can be used to create receipts, invoices, reports and a variety of other documents.
Creating a PDF normally would require you to posses the knowledge of the PDF generation DSL which is intricate. Instead, Wicked PDF allows you to create documents from HTML views which is much simpler and less time consuming. Wicked PDF allows you to use all static media types like images and CSS graphics to make the PDF look prettier. It utilizes the wkhtmltopdf shell utility to generate PDF files.
7. Chartkick – Data Visualization & Infographics
Data Visualization is important for all web applications. Chartkick is a gem that integrates Chartkick.js library with your Rails application. It can be used to create all types of Infographic Elements like graphs, charts and pie diagrams.
Chartkick offers many view helper methods in Rails to simplify the process and to avoid code clutter in the view files.
8. AWS-SDK S3 – Cloud File Storage
Amazon S3 is a part of the AWS Cloud Computing Platform. S3 offers an API to upload and download files to and from the cloud securely at supersonic speeds. To use this gem you should have an AWS account with subscription to the S3 service.
The AWS-SDK S3 Gem offers bindings that helps integrate your Rails web application to the Amazon S3 API. It creates an Object Oriented Interface to interact with the AWS S3 APIs securely and easily.
9. Will Paginate – Pagination
Pagination is an unavoidable part of any web application not just Rails. Will Paginate gem offers a neat solution to paginate your data without complications. It can be used in both microservice (APIs) applications and monolithic applications and has many options like Serializer Integration.
10. ActiveModelSerializers – JSON Serialization
While writing API based application in rails, JSON serialization is a painful process. ActiveModelSerializers gem offers a great solution to serialize all your ActiveRecords by integrating it with a class that inherits from the ActiveModel::Serializer class contained in this gem.
You can customize the data output of the attributes contained in your ActiveRecords by adding your own methods. With ActiveModelSerializers, building JSON structures for API call responses, is a simple and organized. You can integrate this gem with the will_paginate gem to attain the desired output.
These are the gems that I’ve found most interesting and useful for development of Rails based Web Applications. You may offer your suggestions in the comments below and I shall include them in a future article.