Writing a Wrapper for Wyoutube… I mean Youtube

I the first rails application my team create Anime Chase. One of the coolest features the ability we had in the application was the ability to view Youtube videos that correlates with the anime on the show page.

AnimeChase Show Screen

Unfortunately, the gem we used for to grab the Youtube videos, YoutubeIt, stopped working. I had a choice either just let this cool feature die in our application or try to get the Youtube feature working again. In this journey, I found another person in the class that wanted to work on using the Youtube in their application, so we decided to work together on the getting those videos working. We figured the best way to solve both of our problem was to create a Ruby wrapper.

Broken Anime Chase

This is the Ruby wrapper that my classmate and I wrote to get Youtube videos. I will go over each method set by set:

Screen Shot 2015-05-14 at 4.29.30 PM

First, we created a reader for youtube and client that we will set in the initialize method:

Screen Shot 2015-05-14 at 4.29.59 PM

In the initialize method we created set the instance of client to a new Google::API call in which we pass in our authorization key that we from the google then we set set authorization set to nil and finally the API call takes in application name pointing to the name of your app (the one that you named to get your api keys).

The instance of youtube is the discovered_api method on the new client instance with the parameters of ‘youtube’ and the version currently it is ‘v3’:

Screen Shot 2015-05-14 at 4.30.16 PM

We then created a search method that took in a query then creates a hash that puts the query in the q value. We then preset the other options in the hash run execute. The part value in the hash needs to be set to snippet to generate videos. MaxResults is set the amount of video results you would like:

Screen Shot 2015-05-14 at 4.30.40 PM

After we have the parsed json we can just send the video id and render the embedded youtube video on the show page.

Screen Shot 2015-05-14 at 4.32.28 PM

The next set is to create my very first ruby wrapper that just handles searching youtube because I found that is is really hard to get a gem that just simply searches youtube.




Using Send Like a Boss

I would like to talk about how I solved the Email guesser lab using the send method.

Here is a small synapsis of the assignment:


So for this lab we had to dynamically guess users emails based on the pattern of other users emails with their same domain.

so what I did first was with the JSON of the the possible patterns that a user can have I created a new hash with all possible

emails based on the person’s name after I split the domain and name pattern.

Screen Shot 2015-05-01 at 12.37.35 PM

Then I wrote a method for each possible change for the email:

Screen Shot 2015-05-01 at 12.18.20 PM

Afterwords I took the name and domain passed in and based on the matching number in the hash I saved it to a variable.

I then made created an empty array to store the possible name results.

Then I used the send method to pass in the matching number pattern based on the domain key of the hash and turn that into a method used the send method.

What the send method does is basically sends my message to an object or ancestor until a method matches. In my program, found the possible with number that matched than called that method.

after that I iterated through the transformed name and put the actual domain on the end of the transformed name. Then I just presented that.

Screen Shot 2015-05-01 at 12.18.31 PM

How to make take the $$$ out of Expensive calls in Rails

I have been working tireless on my rails project Bucketlistr and I would like to show one cool trick to make rails applications less expensive when you’re calling on objects that are related to other objects. so normally we would call something like “Todo.first.comments.users” to grab the users from the todos comments. This normally would be an expensive query because we are going through the ActiveRecord Database once for the first todo then usually in a second database query to find the first todo’s comments then users from those comments. What this call does is grabs everything at the same time to quit multiply redundant queries.

So how I like to think of it is like a lazy person shopping at the grocery store. They have a list but it is unordered the the person goes through the list grabbing one item at a time in the order of the list. even if a item is right next to one-another. What using something like “Todo.first.comments.users” does is making that lazy person in the shopping list grab all the items the person needs in the same isle. then going to another instead of going a shopping list that would require the person to go into the same isle to grab different items in the same isle.