Class: CLI
- Inherits:
-
Object
- Object
- CLI
- Defined in:
- lib/imdb_term/CLI.rb
Instance Method Summary collapse
- #details(id) ⇒ Object
-
#initialize ⇒ CLI
constructor
A new instance of CLI.
- #list(detailed = false) ⇒ Object
- #now_playing ⇒ Object
- #parse(command) ⇒ Object
- #save(id = nil) ⇒ Object
- #search(title) ⇒ Object
- #this_week ⇒ Object
Constructor Details
#initialize ⇒ CLI
Returns a new instance of CLI.
3 4 5 6 |
# File 'lib/imdb_term/CLI.rb', line 3 def initialize @movies = nil @movie = nil end |
Instance Method Details
#details(id) ⇒ Object
67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 |
# File 'lib/imdb_term/CLI.rb', line 67 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.} | #{movie.runtime} | #{movie.genres.join(', ')}" print "Director(s): " puts "#{movie.director}" print "Stars: " puts "#{movie.stars.join(', ')}" print "Summary: " puts "#{movie.summary}" end |
#list(detailed = false) ⇒ Object
106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 |
# File 'lib/imdb_term/CLI.rb', line 106 def list(detailed = false) if detailed @movie = @movies = nil Movie.all.each do |movie| puts "#{movie.title} (#{movie.release_year})" puts "#{movie.} | #{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 |
#now_playing ⇒ Object
34 35 36 37 38 39 40 41 |
# File 'lib/imdb_term/CLI.rb', line 34 def @movie = nil movie_data = Scraper. @movies = Movie.new_from_array(movie_data) @movies.each do |movie| puts "#{movie.id} - #{movie.title}" end end |
#parse(command) ⇒ Object
8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 |
# File 'lib/imdb_term/CLI.rb', line 8 def parse(command) if command.eql? '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 |
#save(id = nil) ⇒ Object
83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 |
# File 'lib/imdb_term/CLI.rb', line 83 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 |
#search(title) ⇒ Object
52 53 54 55 56 57 58 59 60 61 62 63 64 65 |
# File 'lib/imdb_term/CLI.rb', line 52 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 |
#this_week ⇒ Object
43 44 45 46 47 48 49 50 |
# File 'lib/imdb_term/CLI.rb', line 43 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 |