class CLI def initialize @movies = nil @movie = nil end def parse(command) if command.eql? 'now playing' now_playing elsif command.eql? 'this week' this_week elsif command.split(' ')[0].eql? 'search' search(command.split(' ')[(1..-1)]) elsif command.split(' ')[0].eql? 'details' details(command.split(' ')[1]) elsif command.split(' ')[0].eql? 'save' if command.split(' ').size == 2 save(command.split(' ')[1]) else save end elsif command.split(' ')[0].eql? 'list' if command.split(' ')[1].eql? 'detailed' list(true) else list end else puts 'Unknown Command' end end def now_playing @movie = nil movie_data = Scraper.scrape_now_playing @movies = Movie.new_from_array(movie_data) @movies.each do |movie| puts "#{movie.id} - #{movie.title}" end end def this_week @movie = nil movie_data = Scraper.scrape_opening_this_week @movies = Movie.new_from_array(movie_data) @movies.each do |movie| puts "#{movie.id} - #{movie.title}" end end def search(title) @movie = nil title = title.map{|e| e.capitalize}.join('+') movie_data = Scraper.scrape_movie_by_title(title) @movies = Movie.new_from_array(movie_data) @movies.each do |movie| print "#{movie.id} - #{movie.title} (#{movie.release_year}) " if movie.type print "(#{movie.type})\n" else print "\n" end end end def details(id) @movies = nil movie_data = Scraper.scrape_movie_by_id(id) @movie = Movie.new(movie_data) movie = @movie puts "#{movie.title} (#{movie.release_year})" puts "#{movie.content_rating} | #{movie.runtime} | #{movie.genres.join(', ')}" print "Director(s): " puts "#{movie.director}" print "Stars: " puts "#{movie.stars.join(', ')}" print "Summary: " puts "#{movie.summary}" end def save(id = nil) if id.nil? if @movies.nil? && @movie.nil? puts 'No movie to save' elsif !@movie.nil? movie_data = Scraper.scrape_movie_by_id(@movie.id) Movie.create_or_update(movie_data) @movie = nil puts 'Movie saved' elsif !@movies.nil? movies_data = @movies.map{|e| Scraper.scrape_movie_by_id(e.id)} Movie.create_from_array(movies_data) @movies = nil puts 'Movies saved' end else @movie = @movies = nil movie_data = Scraper.scrape_movie_by_id(id) Movie.create_or_update(movie_data) puts 'Movie Saved' end end def list(detailed = false) if detailed @movie = @movies = nil Movie.all.each do |movie| puts "#{movie.title} (#{movie.release_year})" puts "#{movie.content_rating} | #{movie.runtime} | #{movie.genres.join(', ')}" print "Director(s): " puts "#{movie.director}" print "Stars: " puts "#{movie.stars.join(', ')}" print "Summary: " puts "#{movie.summary}" puts "" end else @movie = @movies = nil Movie.all.each do |movie| print "#{movie.id} - #{movie.title} (#{movie.release_year}) " if movie.type print "(#{movie.type})\n" else print "\n" end end end end end