I am revisiting an earlier attempt at the CLI data gem project. I went through Avi's related video carefully and feel l understand what we are doing here and how useful creating our own gems can be. The idea of my gem is to create a handy list of upcoming movies that will be released in theaters for movie fans interested in what films are on the horizon.
In this project I've added pry and nokogiri to the gemspec so we can get website data in the terminal. Pry is an irb alternate that lets us browse source code and documents. And Nokogiri is a very helpful HTML parser so we can access pieces of an html page to use in our Ruby code.
My upcoming-movies rb file in my library folder requires Pry, Nokogiri and Open-Uri as well. Open-uri allows us to open a website as a file. I don't feel too familiar with this but hope to learn more about how it works in the future. We also require-relative the three ruby pages in my library>upcoming-movies folder. We use the a version.rb to keep track of our own versioning. Most of the code for the project is in the cli.rb and movies.rb files. It was great seeing Avi think through the logic for what we want the command line interface to do. I find it intimidating to write code from scratch still. I went through this carefully and reviewed a few procedural Ruby lessons but do need to think through a bit more how these two files work together. Some of the language feels abstract to me still.
In the movies.rb file we set up attr_accessor (read/write) variables for all the things about an upcoming movie release we would like to display. I switched around a few of the options because I started with looking at the Box Office Mojo site list and the IMDB upcoming list was easier to scrape and did not have a large messy table to contend with. Not sure why we need both a self.today and self.scrape_movies. I think it's because the today method stands for what we are doing and the scrape method is more the information itself that we push upwards to the today method. In the scrape_movies method we create an array of movies, shovel in information from the scrape_imdb and return this information. The last method is the self.scrape_imdb. We have a doc variable which connects us to an imdb page with help from Nokogiri. We create a new movie and use and list where and how we find movie names, dates, genre by scraping the imdb website.
One things was interesting to be was that what I scraped in Nokogiri didn't quite match up to what would display if I ran ./bin/upcoming-movies to see the final output. Not sure why this is. I had the best luck in terminal if I referred to each variable with an array number.
The cli.rb file has more connection to the actual operation of the CLI. We're adding the info from the upcoming-movies.rb file here via UpcomingMovies::Movies.today. The call method is currently a container for the other method. list_movies has an each.with_index which has two parameters the second is a number so we can have a numbered list of returns. I added an array to return 7 results but have managed to return the first movie seven times! Need a tiny bit of help to wire this to return not just the movie # + date, length, etc. but also multiple movies in order. The next method asks us if we want to hear about any of the movies and prints the related movie description if we press a movie number. If there's no typed input we put to the terminal 'enter the movie you would like to hear about' and if the input is more than zero we put the proper movie description. We have an eslif that that if you enter 'list' we can return the original list of movies again. We have a message for an input we don't recognize and a goodbye message if you type exit to leave. This is the project process currently:
Things I would be interested in working on:
1 - how to call multiple span:nth-child(1). Is there a better way than just a list here?
2 - putting a variable into the arrays so I can display multiple movies. An array for a group of arrays?
3 - strange . after the movie.date?
4 - fixing the menu method in cli.rb so you only enter a number between 1-7.
5 - hook up entering 'list' at the end of a movie description so someone can return to the beginning after reading a description.
6 - Add a note to tell people you need to type 'exit' to quit and detach it from the bad input message.
7 - The menu method feels messy in general. is there a way to simplify and clean up after changes have been made?
What else could be added:
A longer list of titles
The director, stars and rating from the page I'm scraping
Open a browser to the imdb page for the movie if someone wanted
List related upcoming titles by genre?
List previous movies by director?