Whenever a synapse fires off in my brain, and an idea arises, I write it down and come back to it every so often to see if it still excites me enough to bring it to life. I think this is a very important first step to building something from scratch. Especially in programming, if frustration occurs, your passion for the project will get you through to the other side(hopefully).
Once the idea passes the first test, then I start thinking about how useful the tool will be to its users. Would I personally use this tool? Will it be easy to use? What will the user experience be like? If the idea of firing up the program and actually using it makes your skin crawl, then it’s time to drop the idea and move onto something else. Although, the pure joy of making something useless might be adequate. In this case however, my CLI Gem: ‘ra_events’ passed both of these tests. I wanted to make something that programmers can use to look up electronic music events(via resident advisor) in their state from within the command line. No need to go to the browser and navigate anywhere, just a couple keystrokes and you can list events in your state by month.
So, the research began. I needed to figure out how to actually build my Ruby Gem. I got to digging. The official docs are very useful in this regard.
Then I found this little ‘gem'(ha…)
$ bundle gem gem_name_here
Ok, so this gives you a skeleton or app structure to go off of. Since this isn’t a large application(Just a Scraper, Event, and CLI class), I needed to make some adjustments to the structure. Namely, the way in which bundler namespaced the classes by default.
Here is a side by side comparison of my current release(left) and a newly created gem(right).
As you can see, I added my classes in lib/ra_events. Inside lib/ra_events.rb, I required all of the necessary files for my program to run, and then I require_relative in my bin/ra-events. Your executable file should use caterpillar case(orKebab-case) This is convention for when you use your gem from terminal/bash(I would type in ra-events to start my program).
and here’s the executable:
Part 2, coming soon!
Other resources:
https://www.adrianprieto.com/building-a-ruby-cli-gem-from-scratch/
http://blog.thepete.net/blog/2010/11/20/creating-and-publishing-your-first-ruby/
https://gist.github.com/jendiamond/6128723
Leave a Reply