Ruby on Rails — Advanced Tools (part 3)
In this third episode we will talk about some advanced tools you’ll surely enjoy. They will make lives easier both for you as a developer and also for your users, who will be delighted with a fantastic app!
In this article, we’ll cover a few things you’ll need as your project grows up. From handling HTTP requests toward external services to PDF generation, background work to administration dashboards, we’ve got you covered! We’ll also name a few tools that can make your debugging life a lot easier.
HTTP clients 💻
httparty is a simple way of rolling out your own API client. You can include it in a class of your own and you’re instantly able to perform all kinds of HTTP requests, auto-parsing JSON responses, handle SSL certificates, perform basic authentication, handle the responses as streams and much more. An option to consider if you want to easily party hard. 🤓
REST Client is another good solution for keeping things tidy. It lets you perform file uploads in a very magical way, follows redirects while keeping a history of the redirection path and it can also stream the responses. You can configure request timeouts, handle SSL certificates among many other features. Check the project homepage, the documentation is very thorough.
On the other hand, Faraday focuses on flexibility. It aims to provide a common interface for your code to rely on, that hides the implementation details of the backend adapters you can use to actually perform the requests, like Ruby’s very own Net::HTTP. If you need that kind of control this is the right tool for you! It also allows you to tweak the configuration of the adapter of your choice, in case you need to set something very specific that you can’t do through Faraday itself. 👌
Background work ⛏
Resque is one of the most popular background processing gems out there. It’s based on Delayed::Job (a bit below) and uses Redis to save the background jobs information. It supports multiple queues for running your jobs and allows you to check the current state of those queues and jobs in a simple web interface.
Sidekiq is another great library for dealing with background work. It uses threads to run multiple jobs in parallel inside the same process and is also backed by Redis to store the jobs. If you need to focus on performance, Sidekiq is probably the best choice. You can check the benchmarks on the project’s GitHub, but it’s reportedly a lot faster than Resque or Delayed::Job. It has a great web console where you can check the progress of the jobs in quasi-realtime, and also the status for your queues. Additionally, there’s a pro version that contains several advanced features in case you need to take your background work to the next level. 🙌
Then, there’s Delayed::Job. It was extracted from Shopify, and unlike the other two solutions mentioned before, it’s backed by a real database to keep track of the jobs’ state (although there’s also an experimental version using Redis). This might be an issue if you use your application’s database to store that info, as you’ll be under extra-pressure when you have lot’s of jobs to run.
Note: regardless of which background library you pick for your background processing consider using ActiveJob. It provides a layer of abstraction on top of the gem of your choice, which may come in handy if you need to swap it in the future for some unforeseen reason. With a simple configuration in your
application.rb file, you’ll be ready to go! Or you can even use multiple job adapters and specify the adapter to use on a per-job basis.
PDF Generation 📄
Prawn is a Ruby library that provides you with a great DSL for creating PDFs. It has great support for drawing vectors, supports image embedding for PNG and JPEG files, has support for all kinds of TrueType fonts, and more. If you’re looking for a simple, declarative way of creating a PDF file from scratch, this is definitely a great option. It also ships with an extensive manual, with lots of examples.
Wicked PDF is a tool that also aims to generate PDF files but with a very different approach. This gem is a wrapper for the wkhtmltopdf tool, adding some extra Rails sauce on top of it! With Wicked PDF you write an HTML view and it will handle the conversion to a PDF file for you. It has an extensive configuration that lets you setup all the details you can think about.
With a similar approach to Wicked PDF, PDFKit also wraps wkhtmltopdf to bring HTML to PDF conversion to Ruby. Wicked PDF is a bit more extensible/configurable, though: if you don’t need to use custom layouts to build your HTML (that will be used to generate your PDF file) PDFKit is probably a lighter solution and you’ll get the same results, as they both use the same tool to actually perform the conversion.
Admin panels 🔐
Active Admin is a very customizable administration panel that allows you to display your database tables data with ease. By default, Active Admin will use Devise to control the accesses to the admin panel. You have built-in support for listing/filtering resources, edit and destroy them and to perform batch actions. You can create completely customized pages, download the data in different file formats and secure resources access with authorization adapters (for example with Pundit, or CanCanCan… did you read about them in the first article of this series??! 🤔).
RailsAdmin is also a very complete solution for implementing an administration back office. It mostly pairs with Active Admin in terms of features, allowing you to CRUD your resources, search/filter results and export that data in a variety of formats. It’s also very easy to integrate with Devise for user authentication, CanCanCan or Pundit for authorization and can display the versioning of a resource by using PaperTrail — see, I told you these tools were very handy when combined with each other. 😅
Although Administrate is less known, it focuses on providing the most flexibility to roll out your own admin panel. It also claims to provide a better UX over Active Admin and RailsAdmin, while making the developer’s job easier when it comes to customizing the defaults that ship with the gem. As you can manage the views and controllers, setting up Devise to authenticate your admins is just 1 line of code away! Definitely worth it if you need a simple yet very custom admin back office.
Console/Debugging tools 🐞
If you’re performing a lot of external HTTP requests, httplog will be a very handy tool for you. With a quick setup, it allows you to inspect most of the HTTP libraries out there, providing all the data you need to look at when making a request. You can also whitelist and blacklist URLs using patterns, benchmark the requests and configure a couple of display options. A must-have for your development environment!
Awesome Print is an awesome way to bring some life to your rails console! It will pretty print your ruby objects, with all their fields indented and full of color. Who said that looking at the console’s data needs to be boring!? 😎
Then, there’s Pry and friends. Pry is an alternative shell to the one that you’ll usually see in use for Ruby. It provides several enhancements over IRB, and with the help of a few extensions, you’ll get a perfect environment for debugging. For example, if you want your
rails console command to open the pry shell, you can use pry-rails. For debugging your code, you’ll want to use pry-byebug: it lets you to set breakpoints and to navigate your code while using pry. With pry-stack_explorer you can do even more, by navigating up and down your stack once inside a
binding.pry. With it, you can also print the whole call stack with the
I bet you don’t like errors in your app — that’s for sure — no dev does. 😱 But what if you could have a really nicer Rails error page in no time? Better Errors to the rescue! You will be able to inspect your local/instance variables and filter the stack trace to include only your app method calls.
Last but not least, ever heard of N+1 queries? I bet you did! As your app grows larger if you’re not careful enough you’ll most likely end up with a few of these. Bullet will be your best friend tackling N+1 queries! Use it (in your development environment, of course!) to get insights about places in your code where you should fix N+1 by eager loading some data. It will even tell you what you need to change in code, isn’t that cool? Bullet warns you about unused eager loads as well, and you can also whitelist specific classes to avoid them being reported. On top of that, it allows you to automatically report the issues found to an error reporting tool of your choice. 👏
That was quite a lot of information! But in time you’ll start using some of these tools (if you don’t already) and you’ll enjoy the benefits they introduce, having in mind the small overhead of setting them up in your projects.
On the other hand, if you know about other relevant tools such as the ones above, leave us a comment, we’re always interested in learning about new tools. 🚀
On the next post in the series, we’ll tell you all about the tools we use for testing and to ensure the code quality standards we’re used to. This has a lot of impact on our team, as we use both things exhaustively in our continuous integration process. Stay tuned! 👋