The CLI Mood App or: How I Learned To Stop Worrying And Love Spotify’s API
By: Tucker Umbehagen
Tags: Cli, Programming, Ruby, Spotify Api, Coding

When it came time for me to start working on my first full fledged Ruby project, I was slightly intimidated.

My background was rooted mostly in Javascript, and I had only been learning the Ruby language for about a month. I soon realized, however, that this project would be the learning experience that would cement many of these Ruby concepts into my frontal cortex for good.

Being the music nerd I am, I realized I would have a better experience with my learning process if I knew it would be returning some funky tunes. Since the assignment dictates that my project would be a CLI (Command Line Interface) app that interprets a website’s API (Application Programming Interface), the idea came to me pretty quickly; an app that recommends music to the user based on their current mood!

I know, I know, it’s a Steve Jobs-level concept. Unfortunately, I’m still waiting on one of the FAANG companies to slide into my inbox with an investment offer (Zuckerberg, I know you’re reading this).

Ah yes, the blinking cursor on an empty page. Every programmer’s mortal enemy.

Before I could start putting rubber to the road (or, uh, fingers to keys…) I first had to find a suitable API . After some Google-fu, I learned that Spotify had one of the largest musical databases on the internet; and it’s accessible via their public API! Even though it’s notoriously not a beginner-friendly API to work with (according to some anonymous internet commenters, at least) I decided to accept the challenge in hopes of coming out on the other side a stronger programmer.

After implementing communication between Spotify’s API and my app, it was time to work on the user experience. Pictured below is the first prompt the user receives; a friendly greeting followed by a selection of moods to choose from. Each mood is linked to a playlist in Spotify filled with songs that match the mood’s vibe. When the user makes a selection, a random song is returned from the specified mood’s playlist.

While functional, I knew the UX of my app could be slightly more intuitive.

Of course I was proud that my app was successful in it’s intended function, but I knew that I had take the next part a bit further in terms of visual presentation. After some more research, I discovered a Ruby gem built specifically for CLI apps. TTY-Prompt (a part of the TTY-Toolkit) seemed to offer exactly what I was searching for. After installing the gem and “requiring” it at the top of my CLI.rb file, it was finally time to implement it into my code.

Yes, I use the 80’s Synthwave theme in VScode. Don’t judge me.

As you can see, I included line 56 at the top of my method in order to enact the TTY-Prompt interface. Next I coded in all the potential user choices that would be available. Before the method was complete, however, I had to specify what each choice would return to the terminal. For this purpose, I used the trusty if-elsif statements to dictate what command each selection would enact. The second and third choices were easy enough; “Select a different mood” brings the user back to the beginning of the app to select a new mood to discover a new song for. “Exit”, you guessed it, exits the app altogether (after giving the user a warm exit message, of course. The first selection, however, was slightly more complicated.

In order to suggest another song that matches the previous selected mood, I had to call on my #return_random_song method in my rspotify.rb file using the user and playlist_id instance variables. Since they are instance variables, they are typed out with @ symbols in front of their names so they can be accessed from anywhere in my program (as long as the file where they are defined is required at the top of whatever file I am calling them in). Below is the prompt the user sees after the initial mood selection:

Seeing this in my terminal for the first time made me feel like the apes in 2001: A Space Odyssey after first seeing the monolith.

This interface was exactly what I was imagining in my head. Something a little more intuitive to interact with; a green highlight that moves with the user’s arrow key selections. It’s not exactly a game-changing addition, but I believe it helped my CLI app resemble an actual web page as opposed to a 1980’s Apple II prompt.

You can view and download my CLI Mood app here!

After completing this project, I feel much more confident not just with the Ruby language, but with programming in general. Every morning I wake up and am excited to sit in front of my screen and start writing code (well, maybe after a coffee or two first). Next I’ll be tackling the SQL language, which I will discuss in my next blog post. Until then, thanks for reading and happy coding!