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.
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.
This is the Ruby wrapper that my classmate and I wrote to get Youtube videos. I will go over each method set by set:
First, we created a reader for youtube and client that we will set in the initialize method:
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’:
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:
After we have the parsed json we can just send the video id and render the embedded youtube video on the show page.
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.
Resources: