class CLI

Public Class Methods

new() click to toggle source
# File lib/imdb_term/CLI.rb, line 3
def initialize
  @movies = nil
  @movie = nil
end

Public Instance Methods

details(id) click to toggle source
# 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.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
list(detailed = false) click to toggle source
# 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.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
now_playing() click to toggle source
# File lib/imdb_term/CLI.rb, line 34
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
parse(command) click to toggle source
# File lib/imdb_term/CLI.rb, line 8
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
save(id = nil) click to toggle source
# 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
this_week() click to toggle source
# 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