Using Regular Expressions to Convert Text to “Cat Mode”

This week, I added “cat mode” to my personal website, Essentially, I used jQuery to create a page that would dynamically load text. If a visitor clicks the “cat mode” link, the text reloads, replacing all words with “meow,” except for links. I also decided to maintain punctuation and capitalization.

While building out the feature, I created a few helper functions–isPunct, isLink, and isWords–that would return boolean responses and help determine whether to leave a text segment in its current state or convert it to meows using my meowify method. (You can check out the methods on my Github.)

This lead me to revisit Regular Expressions, a programming tool I’ve sometimes been wary to use because–let’s face it–compared to the readability of Ruby, Regular Expressions are hieroglyphic. In reality, though, they’re not that tricky to use, and sometimes extremely helpful.

Today, I checked out Britt Ballard’s excellent blog post about Regular Expressions, and was inspired to clean up the Regular Expressions I used in my functions, for practice and perhaps future reference. My project didn’t require particularly complex expressions, so be aware that there’s much more that can be done with Regular Expressions than I’m covering here. (Check out Britt’s post for some examples, keeping in mind that his use Ruby while mine were incorporated into a Javascript project.)

Four Easy and Useful Regular Expression

  1. Letters
JS letters
Return an array of all letters, case insensitive.

The [a-z] in this example could be replaced with any string or letters. Removing the “i” from the statement would make it case sensitive, so that, in this example, only [“y”, “a”] would be returned. Removing the “g” from the statement would mean only the first match would be returned.

2. Punctuation

JS match punct
Return the first punctuation found in a string.

This can be customized by adding or removing any punctuation or special characters.

3. All Punctuation

JS globally match punct
Return an array of all punctuation in a string.

I used this to save all punctuation from a string before splitting that string at punctuation.

4. Finding a link

JS match index
Return the starting index of a match in a string.

This could be used to find the start of a link in a string of text.

Using these regular expressions I was able to majorly simplify my code (though I’m finding that trying to convert my sentences while maintaining proper spacing and formatting is still quite a challenge!) I’ve still got some work ahead of me with the site, but I’m hoping that the work I’m putting in to automate my cat mode conversion will be helpful in later parts of my project as well.




Ruby’s Object Ancestors

Two weeks ago, I completed the Flatiron School web development immersive (woot!), and three weeks from now, I’ll be starting a position as an instructor at Flatiron, coaching students through Ruby  fundamentals (among many other topics) that I was learning four months ago.

51qtp4nzjrl-_sx258_bo1204203200_In the meantime, I’ve been taking a deeper dive into Ruby and reading The Well-Grounded Rubyist to make sure I’ve solidified the most foundational information in my mind.

That’s how I stumbled across Object ancestors again, and more specifically, became curious about Kernel.

Don’t know what Kernel is? Open up irb in your terminal and type Object.ancestors. This is what you should see (as long as you have ruby installed):

Object ancestors

So, what is Kernel (and, while we’re at it, what is BasicObject)?

David Black first mentions Kernel in chapter 4, Modules and Program Organization, of The Well-Grounded Rubyist. This is a hint to answering the question I posed, because, first of all, Kernel is a module. As Black says, Kernel is “the module where the majority of the methods common to all objects live.”

Just like any other module, Kernel is mixed in to a class to extend that class’s functionality. More specifically, Kernel contains the instance methods such as integer, loop,  rand, and puts, which every Ruby object has access to upon creation.

Meanwhile, BasicObject is a parent class. As the Ruby documentation plainly puts it, “BasicObject is the parent class of all classes in Ruby.” It only has a few methods, such as equals (“==”) and new.

So, essentially, when you create a new object in Ruby, what you’re doing is inheriting methods from BasicObject and then mixing in Kernel methods. Cool, right?


For a closer look at “puts” in the Kernel module, check out this cool blog post by Vaidehi Joshi.




Using the Spotify API

Last week, I completed my final project at Flatiron, a React-Redux app with a Rails API.*

My team decided to also make use of the Spotify API for our app, PlaylistPal, so that visitors could make playlists using the very cool metrics that Spotify stores about each of its songs, including danceability, valence, and amount of vocals.

Part 1: Requests without Authentication

Early into our project, we discovered that the Spotify API gives some information, such as an artist’s top ten songs, without any authentication. To get top ten tracks, we first took the user input of an artist’s name and made a get request for Spotify’s data on that artist.

From there, we parsed the unique artist id from our response, and made a second request to Spotify, this time returning the top ten tracks.

After that we could parse through the response from Spotify and grab any data we needed, including the track name, preview URL, and album title.

Part 2: Requests with Authentication

Making authenticated requests was quite a bit trickier, but after a few hours of fiddling we made our first successful request. Our first step was registering our app with Spotify in order to get our client secret and client id. These are simply strings of random letters and numbers that must be put in a secret file, so that they aren’t stored on Github and made publicly accessible.

In our app, we created sliders for users to select the danceability, emotional valence, etc, of songs that would be returned to them. The Spotify API requires requests to include an artist id or musical category along with the danceability and valence parameters, so we prompted users to enter an artist name before letting them manipulate the sliders and send a request.

In order to make our request for song recommendations to Spotify, we first had to get a token from Spotify (essentially another secret code!) using our client id and secret. The header of this post request contains a base 64 encoded string with the id and secret.

The returned token is then put in the header of the get request to Spotify, which has a url that includes the formatted query containing the artist id and other parameters.

Finally, the Spotify-recommended tracks are returned!

I should also mention that sites can use OAuth to authenticate individual user accounts with Spotify in order to get data such as albums saved to the user’s account or post tracks to a playlist. Check out the Spotify API docs for more info.

Overall, it took quite a bit of legwork to figure out how to properly format our requests to Spotify, but once we got it set up, we had tons of fun playing around with the data Spotify returned to us, and it added tons of value to our project.



**(For those who don’t know what React and Redux are, I’ll explain further: I built an app that uses the Javascript library React to build page components (such as a nav bar), along with the Redux framework, which manages the state of the application (such as the last item that was selected from a list). User information is stored in a Postgres database in my Rails API, so when I need to update my database, I make a post request from the React-Redux side to the Rails side. When I need to grab information from my database, I make a get request to Rails, which renders JSON, and allows me to parse the data I need.)