The Light at the End of the Tunnel — CLI Project: Date Night Trivia
I see a Light at the end of the tunnel. I just want to say this has been one of the most frustrating, most rewarding projects I’ve ever done. I am so thankful for an amazing class that I’m in, because with seeing how they think and comparing it to how I’m thinking something through and using that combined knowledge, I wouldn’t have grown in the last week.
I didn’t know if I was going to make it. I was at the point of tears and frustration was over 100%. I didn’t think anybody cared, but in fact everyone did and now I know that it takes a village and a ton of research. I am not someone who likes relying on others for help, so asking for help has humbled my approach at learning how to code, but more importantly LEARNING.
Let’s start off with my original idea. I started watching this YouTube video , it was very helpful and it got me started fast. The first idea I had was to do a real estate MLS CLI called RealEstInvestor. This idea was supposed to get a real estate API like Zillow, gather information about listings that you see publicly and give you back the information of the property you were interested in… after typing in a ZIP Code you were looking for. When watching how to set up your API, I had finished that and started working in my CLI class. I will just say this guy was probably close to being 3/4 of the way done, other than the fact that I needed an actual API. Unfortunately, that didn’t work in my favor and I was not going to pay for an API.
My next idea came that I could do a CLI with Disney movies outputting information on the best Disney movies of all time, up to date. Mind you this is the beginning of the week on Monday I had worked on the real estate CLI over the weekend put probably 20 hours into it just to toss it to the side. Then I realize that I wasn’t going to do the movies cause I couldn’t find a good Disney API. So then came my trivia game idea…
Here it is folks, the idea you wanted to hear about from the beginning of this blog and how I thought through it. So like I mentioned before, I definitely recommend watching the recorded lecture videos that your cohort leaders will make. Probably something along the lines of: making a CLI from scratch. I will ALWAYS recommend API over scraping, from my understanding, you just waste so much time scraping and sometimes, if you don’t deem the website readable, you lose so much time. I probably struggled the most with understanding how to grab my data from the website. Actually finding a good website is 1/2 the battle.
I set up my API class with watching lots of videos walkthroughs. A couple things to know when you’re setting up your API class:
Inside this API class, I made a method called self.get_data, from here I had a determine what my URL was going to do inside of this method. So I had a variable, a local one, set equal to a constant which then passes my url.
uri = URI("https://opentdb.com/api.php?amount=20")
After this I realized I needed to decide whether I was going to use HTTParty, Net::HTTP.get(uri), or any other kind of method body that’s similar to this layout… Honestly just google what you would need with the info you got researching for this, I used this doc => ruby docs getting the following info from it:
uri = URI('https://example.com/')
Net::HTTP.get(uri) # => String
You then set trivia array which is a local variable set equal to JSON.parse
trivia_array = JSON.parse(response)["results"]
From here on out, I recommend using binding.pry to see what you data you grabbed. That’s how I determined that I needed to put in the [“results”] in the code above. This project helped me determine when I should use binding.pry. I was never confident with using this feature, now I realize what I was missing!
To get the final piece of my self.get_data method, I had to iterate through the trivia_array.
trivia_array.each do |trivia|
Onto the next part, making the Trivia and CLI class. This part was fun yet frustrating — fun because you get to make it your own and frustrating since we aren’t experts on coding… yet! Lots of hours and errors later!
My biggest take from this section of the project was if/else statements and using self.send. In my head it’s easy and simple to make but when I have to connect everything together, it took a lot of trial and error. My mindset was always “What can I do to improve the user’s experience?”. While this is important, I had to realize that I shouldn’t overthink/ overcomplicate every scenario. You’ll be done with your Trivia class before your CLI class…. I personally worked simultaneously with them both, but did finish the Trivia before.
If you’re feeling defeated 3 days/2 days before this project is due, stay positive and keep moving forward. Your biggest opponent is yourself, you can do it! Use your resources wisely, ask for help! The worst thing someone can say is no. I am thankful for such an amazing cohort.