DEV Community

Unpublished Post. This URL is public but secret, so share at your own discretion.

Creating My First CLI Gem Part 1: Planning and Set Up With Bundler

Excited to say I’m working on first Ruby gem! I’ve been learning all about designing object-oriented ruby programs and how to make scrapers for my Flatiron curriculum, so now it’s time to apply all my new knowledge into a full-fledged, mini-app.

The goal is to make a working command line interface that scrapes information from a website, providing some useful information to the user. For my app, the plan is to allow you to find a recipe to cook directly through the command line.

See also:

Planning

To start the whole process of creating the gem, before writing any code, I tried to imagine how the app would work and flow and documented it into planning notes:

CLI Planning Notes

User types "find-recipe"

Ask how the user wants to see the recipes:

How do you want to get your cooking inspiration?
1. See trending recipes
2. Search for a recipe

>> Trending recipes gets recipe titles from a trending recipes or popular recipes page.

>> Search gets recipes using a keyword input by the user; for example, an ingredient or dish name.

After choosing either option, the app will provide a list of recipe names from which the user can choose to see more details about.

1. Spaghetti with Meatballs
2. Creamy mushroom pasta
3. Lasagna
4. ....

Ask user to choose a recipe or start over.

If the user chooses a number, which will then list that recipe's details: description, ingredients, and steps.

Ask whether the user wants to go back to the list, start over, or exit.

Other: Typing "exit" at any time during the program will stop it.
Enter fullscreen mode Exit fullscreen mode

Initial setup using Bundler

The next step was to get the gem’s initial structure set up so I could start coding. One of the easiest ways to do this is to use Bundler’s built-in gem function which will create a scaffold directory for you, which at the same time initiates a Git repository so you don’t have to! To create my Find Recipe gem, I simply typed the following in my console:

bundle gem find_recipe
Enter fullscreen mode Exit fullscreen mode

That gave my bin/ folder that contains the executable files to run the program, my lib/ folder that contains my class files, and the important gemspec file that keeps track of your gem specifications and dependencies. You’ll also get some freebies like a README.md, and LICENSE.txt and CODE_OF_CONDUCT.md which are good to have if you plan on publishing the gem somewhere like Rubygems. Read more about this on Bundler’s “How to create a Ruby gem with Bundler” article.

Next, I needed to make sure that I could actually get the CLI to run typing in “find-recipe” in the console. To do so, I created an executable file in the bin/ folder called find-recipe and added the following code:

#!/usr/bin/env ruby

puts "Hello World!"
Enter fullscreen mode Exit fullscreen mode

However, when typing ./bin/find-recipe into the console, I got an error message from bash stating “Permission denied.” The reason for this is that the find-recipe file only had read and write permissions, no executable permissions. To check permissions, move to the bin folder and enter ls -lah in the terminal, which will give you a list of all files and their attributes in a readable format.

To change permissions for a file in a certain folder, use the chmod command, and in this case, I wanted to add executable permission to the file, so I typed in the following:

chmod +x find-recipe
Enter fullscreen mode Exit fullscreen mode

Now typing in ./bin/find-recipe executed the file with no problem.

That’s it for setup! Now I can get to coding the the application!

See also the “New Gem with Bundler” Railscast.

Top comments (0)