<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: Juan Diego Rosales</title>
    <description>The latest articles on DEV Community by Juan Diego Rosales (@jdrosales17).</description>
    <link>https://dev.to/jdrosales17</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F477410%2Faebbdcb0-34ba-46a6-9979-0a6808663e91.jpeg</url>
      <title>DEV Community: Juan Diego Rosales</title>
      <link>https://dev.to/jdrosales17</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/jdrosales17"/>
    <language>en</language>
    <item>
      <title>Most Useful Gems for Ruby on Rails API</title>
      <dc:creator>Juan Diego Rosales</dc:creator>
      <pubDate>Tue, 29 Sep 2020 15:29:57 +0000</pubDate>
      <link>https://dev.to/rootstrap/most-useful-gems-for-ruby-on-rails-api-2mnn</link>
      <guid>https://dev.to/rootstrap/most-useful-gems-for-ruby-on-rails-api-2mnn</guid>
      <description>&lt;p&gt;Are you new to Rails? Or maybe just looking for any cool gem to use in your new API? Here is a list of gems we have on our Rails API Base that we consider a must-have in every Rails API project.&lt;/p&gt;

&lt;h2&gt;
  
  
  The best authentication gem
&lt;/h2&gt;

&lt;p&gt;If you are building a Rails API, then you will probably need token-based authentication and that is when &lt;a href="https://github.com/lynndylanhurley/devise_token_auth"&gt;Devise Token Auth&lt;/a&gt; comes in. This gem is built on top of Devise, one of the most popular authentication gems for Rails which you may have already worked with. If that's the case, then you'll get familiar with Devise Token Auth very easily.&lt;/p&gt;

&lt;p&gt;With very little setup, this gem provides registration, sign in, sign out and reset password flows out of the box, as well as very useful helper functions. Also, it handles email confirmation if enabled and supports the use of multiple user models, giving you the possibility to manage the authentication of different types of users at the same time using groups.&lt;/p&gt;

&lt;p&gt;In comparison to other alternatives available, one of the best perks that comes with Devise Token Auth is the very little effort you have to invest to get things up and running. On top of that, you can also customize the different flows mentioned if you want to offer a different experience, and while this may be a little more challenging, it's not rocket science!&lt;/p&gt;

&lt;h2&gt;
  
  
  Handling authorization
&lt;/h2&gt;

&lt;p&gt;I guess authorizing every request on an API can be achieved in the controller, but that is not scalable at all and may ease the introduction of vulnerabilities in an application. Here at Rootstrap, we find &lt;a href="https://github.com/varvet/pundit"&gt;Pundit&lt;/a&gt; to be the perfect ally against those issues.&lt;/p&gt;

&lt;p&gt;Not only will you have one place in the codebase for the authorization logic, making the system more robust, but also it will help to clean up the controllers, improving the readability.&lt;/p&gt;

&lt;h2&gt;
  
  
  How to paginate your index actions
&lt;/h2&gt;

&lt;p&gt;When pagination comes into the discussion, I would definitely recommend &lt;a href="https://github.com/ddnexus/pagy"&gt;Pagy&lt;/a&gt;. Although it was recently added to our Rails API Base, we have been using it in several projects of the company over the last year or so, and the results have been pretty good! It has proven to be a light gem, which is very fast, customizable and easy to use.&lt;/p&gt;

&lt;h2&gt;
  
  
  The best admin for Rails
&lt;/h2&gt;

&lt;p&gt;Most APIs typically need some level of resource administration and for that I would definitely recommend &lt;a href="https://github.com/activeadmin/activeadmin"&gt;Active Admin&lt;/a&gt;. Despite the fact it can have a steep learning curve, specially at the beginning, due to it's &lt;a href="https://martinfowler.com/dsl.html"&gt;DSL&lt;/a&gt;, once you get used to it you can have admin pages with CRUD actions for any resource in no time.&lt;/p&gt;

&lt;p&gt;Something very important to mention is that Active Admin has a large community and good documentation compared to other administration gems, such as Rails Admin or Administrate. Also, another strong feature is that you can configure the index filters according to your needs and they can be customized to use more that one attribute since they support &lt;a href="https://github.com/activerecord-hackery/ransack"&gt;Ransack&lt;/a&gt;. &lt;/p&gt;

&lt;p&gt;If you need a custom page, you can have Active Admin render your own partial and you will probably need to make changes in the controller actions. This is not an easy task, but none of the other gems offer anything better.&lt;/p&gt;

&lt;p&gt;For some people, one of the downsides could be the design, since Active Admin does not come with a powerful CSS framework out of the box. Luckily, there are many &lt;a href="https://github.com/activeadmin/activeadmin/wiki/Themes"&gt;complementary gems&lt;/a&gt; that provide a better looking theme.&lt;/p&gt;

&lt;h2&gt;
  
  
  Background processing gems
&lt;/h2&gt;

&lt;p&gt;On the background processing front, I would definitely recommend starting with &lt;a href="https://github.com/collectiveidea/delayed_job"&gt;Delayed Job&lt;/a&gt;. This is because it easily integrates with Rails, has minimal dependencies, resulting in a fast setup, and it offers a very stable and reliable processing.&lt;/p&gt;

&lt;p&gt;If your application scales, you will need a framework which delivers performance and in that case I would recommend switching to &lt;a href="https://github.com/mperham/sidekiq"&gt;Sidekiq&lt;/a&gt;. Changing the framework should be an easy task thanks to Active Job and you will get awesome benefits from this. Sidekiq is known for its very fast speed, scalability and multithreading potential. By running in memory with Redis, fetching and saving data is much faster than completely relying on the database like Delayed Job, and what's more, you can have a dashboard to monitor the status of the processes.&lt;/p&gt;

&lt;h2&gt;
  
  
  Communicating with 3rd party services
&lt;/h2&gt;

&lt;p&gt;Even though you can usually find some good gem that will help you call a third party service, sometimes you need to do that on your own. &lt;a href="https://github.com/jnunemaker/httparty"&gt;HTTParty&lt;/a&gt; or &lt;a href="https://github.com/lostisland/faraday"&gt;Faraday&lt;/a&gt; are great HTTP clients you can use to make external HTTP requests. They may not be on our base repo, but everytime we need to do that in a project they are our go-to options.&lt;/p&gt;

&lt;h2&gt;
  
  
  Dealing with file uploads
&lt;/h2&gt;

&lt;p&gt;Active Storage is what we use to handle file uploads, but we noticed that it didn't support base64 attachments. That's why the Rootstrap team developed &lt;a href="https://github.com/rootstrap/active-storage-base64"&gt;ActiveStorageBase64&lt;/a&gt;, which offers an easy way to support uploading base64 encoded files. So, if you have this need, don't hesitate to check out that awesome gem of ours!&lt;/p&gt;

&lt;h2&gt;
  
  
  The best gems for testing
&lt;/h2&gt;

&lt;p&gt;Although it may take some time to get used to the DSL, here at Rootstrap we choose &lt;a href="https://github.com/rspec/rspec-rails"&gt;RSpec&lt;/a&gt; for testing. The main reason for this is that it encourages human readable tests, which ends up helping a lot in the development process.&lt;/p&gt;

&lt;p&gt;When writing tests you always need to set up database records to create the context, and for this we like to use factories with &lt;a href="https://github.com/thoughtbot/factory_bot"&gt;Factory Bot&lt;/a&gt;. This gem offers great flexibility, allowing the developer to easily create different scenarios, but also improves the readability as you can easily understand what is going on in the test. Another gem that we find very useful for factories is &lt;a href="https://github.com/faker-ruby/faker"&gt;Faker&lt;/a&gt;, as it provides an easy way to generate fake data.&lt;/p&gt;

&lt;p&gt;Last but not least, if your API relies on third party services you will need stubbing on the external HTTP requests and for this I recommend using &lt;a href="https://github.com/bblimke/webmock"&gt;Webmock&lt;/a&gt;. If you ask me, I found this gem much simpler and flexible than VCR, for example.&lt;/p&gt;

&lt;h2&gt;
  
  
  Keeping the code quality
&lt;/h2&gt;

&lt;p&gt;Finally, every developer wants a good quality codebase and in that sense linters are essential. Here's a list of gems that I strongly recommend:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;a href="https://github.com/rubocop-hq/rubocop-rails"&gt;RuboCop&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://github.com/flyerhzm/rails_best_practices"&gt;Rails Best Practices&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;
&lt;a href="https://github.com/troessner/reek"&gt;Reek&lt;/a&gt; for code smells&lt;/li&gt;
&lt;li&gt;
&lt;a href="https://github.com/flyerhzm/bullet"&gt;Bullet&lt;/a&gt; for query optimization&lt;/li&gt;
&lt;li&gt;
&lt;a href="https://github.com/glebm/i18n-tasks"&gt;i18n-tasks&lt;/a&gt; for translations linting&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;If you are having doubts while setting up your new API you can always check out &lt;a href="https://github.com/rootstrap/rails_api_base"&gt;Rootstrap's Rails API Base repository&lt;/a&gt;. I think it's a great starting point and you can always customize it to your needs.&lt;/p&gt;

</description>
      <category>rails</category>
      <category>ruby</category>
      <category>webdev</category>
    </item>
  </channel>
</rss>
