Travis Emmett's blog

A coder in training's thoughts

A Few of My Favorite Gems...

Along my journey of learning Rails, I thought it would be helpful for beginners if I listed a few of the indispensable gems I have come across. Before I started developing in Ruby, I would always hear people talking about 'gems' but not really have a firm grasp on what a gem actually was. A gem is basically a library or plug-in that provides some additional functionality that you can require your application to load every time it is run. They are incredibly helpful because they allow the developer to focus on building out the unique parts of an application, rather than reinventing the wheel each time. Rubygems.org is the main repository of Ruby gems (there are over 6,650 gems listed currently) but Ruby Toolbox is also a great resource because it breaks gems down by category. So, in no specific order, here are just a few of my favorites:

Rails ERD

I love Rails ERD. I think there's a tendency when coding to discount visual representations of data and logic for the text editor. While parsing code and relational data mentally is great, I think drawing it out is very important because it allows you to get a more complete understanding of how your data is really interacting with each other. Rails ERD utilizes Graphviz, an open source visualization library, to visualize your Active Record database schema and their relationships (which is probably an over fancy way of saying it let you visualize your database). It renders a nice pdf of your database schema and here is an example below:

Note that Rails ERD requires Graphviz to be installed on your system and detailed instructions can be found here. Once installed, you can type simply "erd" in your console and find the rendered file in your project's main directory under erd.pdf. I actually like to display foreign keys in my tables so to have ERD do this you would type "rake erd attributes=foreign_key,content" into your console instead.

Faker

Faker is a really fun gem to play with. Sooner or later, your applications will get to the size where your databases need a bunch of realistic data to test with. Faker will supply a huge variety of data with which to construct example seed data. So, for instance, in place of making up a street address yourself, you could specify Faker::Address.street_address and it would provide you an example address like “282 Kevin Brook”. It has tons of different kinds of sample data available. Some of the more interesting ones: Avatar images, company slogans, and of course sample bitcoin addresses for all you Internet currency junkies. Here’s a quick example of creating a new Person entry in your database with Faker:

1
2
3
4
5
person = Person.create(
  :first_name   => Faker::Name.first_name,
  :last_name    => Faker::Name.last_name,
  :email        => Faker::Internet.email
)

Figaro

Figaro is an extremely helpful gem when you begin utilizing API’s in your applications. If you are using a public GitHub account in the development of your project, once you supply your API key inside of your application, you want to make sure GitHub does not remember this file, lest it be found by a malicious user. This is where Figaro comes in handy and makes the process very easy. As the steps were a little confusing online when I began using it, assuming you are building a Rails application, here is an example implementation that worked for me:

1. Add “gem ‘figaro’” to your gemfile in your Rails application

2. Type “bundle install” in your console

3. Then type “figaro install” in your console. This will create a file in your config folder called application.yml where we will save the API key. This will also add an entry for application.yml in your .gitignore file so git will not push it to GitHub (be aware when working with any team members they will also have to perform these steps in order to use the API)

4. Go into your config/application.yml file created by Figaro in the previous step and enter your api key. There are different ways to do this but in the recent project I worked on, we chose to enter our key like this:
weather_api: “f0403B7e04110UI15c”

5. Then, find the place in your application (usually within your API’s model class) where you are making the request to the api for data, and save the api key to a variable:
api_key = ENV[“weather_api”]

Voila, you now have access to your api key from the variable api_key without it ever appearing on GitHub!