One thing cool about changing careers is being a beginner again.Of course, it’s a bit scary falling back down the totem pole, especially coming from law which puts a heavy emphasis on years of experience, subject matter expertise, etc. But I actually like being a beginner again. It’s easier to ask questions because you’re not expected to know everything, and it feels like you learn faster because there aren’t any barriers keeping you from being curious.

Another cool thing about switching careers is getting new teachers. At Flatiron, we are really lucky to have truly dedicated instructors who are teaching us how to code and inspire us to think like coders. One thing I wanted to know was, who do they look up to in this industry? Who inspires them, and who are they learning from? For the beginning coder, it’s not initially clear how to learn who is engaging in interesting conversations online about coding and new technology. I remember hearing a story in law school about a student who cut down on how much reading she had to do by comparing the footnotes that appeared most often in cases or treatises. Then she would track down those books and compare their footnotes until she had a short list of sources that the leading authorities all seemed to be citing to. So I thought it would be cool to use the Twitter API to see if I could find out who some of my Flatiron instructors all follow in common on Twitter. That might be a one way to find out who else I could be learning from as well.

OBJECT MODEL

The application I have in mind only needs to do a couple of things: 1) get at least two twitter usernames from the user to compare (for this post, I’m going to compare four Flatiron instructors: Avi, Joe, Steven, and Sophie); 2) request the lists of twitter users that are followed by all four; 3) compare those lists for matches, and 4) display those matches to the user. I’m going to keep things really simple, with two classes, 1) a CLI class to communicate with the user, and 2) an API class to grab the data and do the comparisons.

MAJOR 🔑 ALERT !!!

Lucky for me, there’s a really great Twitter gem (https://github.com/sferik/twitter) that simplifies a lot of the configuration and has a lot of helpful methods for parsing the data from the Twitter API. But here’s the thing: Twitter data, awesome. Twitter gem, awesome. Setting up my application to work with the Twitter API … sigh. The Twitter API requires that users register their applications with Twitter, which means handing over a phone number and some other details. Once that’s out of the way, you get four keys that you need to load into your application: 1) the consumer key, 2) the consumer secret), 3) the access token, 4) and the access token secret.

To keep this data from being hard-coded into your application, I put it in a separate file and and added the path to my .gitignore file. When you initialize an instance of the API client, it can read the keys from this file with this code:

TAKE IT TO THE LIMIT

When you make a request to the Twitter API, the data you get back is separated into ‘cursors’, which is a technique Twitter uses to paginate results. While the technique is useful for breaking up large API queries into smaller batches, it adds an extra step if you want to convert it into useful data for our application. In addition to cursoring, Twitter also limits the number of requests your application can make, making testing and navigating cursors a pain. Rate limits are divided into 15 minute intervals, and I was limited to 15 calls every interval.

So how to deal with this?

First, I relied on one of the Twitter gem’s built-in methods (#friend_ids) to only request a list of user_id #’s that each instructor follows, and nothing else. This kept the cursor size smaller to avoid having to make an additional request to the API. Second, the gem repo includes some useful code to rescue our application and put it to sleep for 15 minutes if the API returns a TooManyRequests error type. Take a look:

I decided to wrap up discrete sections of my application in this rescue code, storing data along the way and only restarting the portions that were not been completed.

CODING

After greeting the user, the CLI runs an instance method called #get_users which takes in the usernames we are comparing and stores them in an array called @users. The usernames are actually stored as hashes, which have two keys: the username we want to look up [:name], and an initially empty array called [:following]. It looks like this:

Next we need to populate the [:following] arrays by making requests to the API. We will iterate through the @users outer array and fill in the inner [:following] array with the responses we get back from the API. This is a perfect opportunity to wrap the method, which we are calling #populate_lists, in the rescue code. The method will check to see if [:following] is empty, and then fill it with the data we get back from the API. If it’s not empty, it’s because we hit the rate limit before iterating through the entire array. We do this check to make sure we don’t make unnecessary calls to the API when we already have that data. It looks like this:

We need to compare these lists now. There’s probably a better way to do the comparison, but I decided to use the ‘&’ operator, which returns the intersection between arrays. It looks like this:

Finally, now that the list culled down to just those accounts shared in common, the last bit is just printing out the results.

RESULTS

Here’s what I got back from running my application for Avi, Joe, Steven, and Sophie:

Very cool. The first entry, with the most twitter followers, looks like a promising account to follow.

Really impressive. In addition to Girl Develop It!, a national non-profit that has taught over 17,000 women how to build software, Sara is also the co-founder and CEO of Jewelbots, which makes “friendship bracelets for the iPhone era” thatintroduces young women to coding.

Here’s a few more:

Antoin, Bill Gates, and Elon Musk:

Steven, Drake, and Rihanna:

If you want to check out my CLI app, feel free to download the repo from my github page, just make sure to configure it with your own keys.

Special Easter Egg for my Flatiron classmates: if you enter bangarang! at any point, you’ll get a list of twitter handles for our cohort. Enjoy!

One clap, two clap, three clap, forty?

By clapping more or less, you can signal to us which stories really stand out.