Class: Imdb::CLI

Inherits:
Object
  • Object
show all
Defined in:
lib/imdb/cli.rb

Class Method Summary collapse

Class Method Details

.display_movie_details(movie) ⇒ Object



77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
# File 'lib/imdb/cli.rb', line 77

def self.display_movie_details(movie)
  title = "#{movie.title} (#{movie.year})"
  id    = "ID #{movie.id}"
  
  @stdout.puts
  @stdout.puts "#{title}#{" " * (75 - 1 - title.length - id.length)}#{id} "
  @stdout.puts "=" * 75
  @stdout.puts "Rating: #{movie.rating}"
  @stdout.puts "Duration: #{movie.length} minutes"
  @stdout.puts "Directed by: #{movie.director.join(", ")}"
  @stdout.puts "Cast: #{movie.cast_members[0..4].join(", ")}"
  @stdout.puts "Genre: #{movie.genres.join(", ")}"
  @stdout.puts "Plot: #{movie.plot}"
  @stdout.puts "IMDB URL: #{movie.url}"
  @stdout.puts "=" * 75
  @stdout.puts
end

.display_search_results(movies = []) ⇒ Object



95
96
97
98
99
100
101
# File 'lib/imdb/cli.rb', line 95

def self.display_search_results(movies = [])
  movies = movies[0..9] # limit to ten top hits
  
  movies.each do |movie|
    @stdout.puts " > #{movie.id} | #{movie.title}"
  end
end

.execute(stdout, arguments = []) ⇒ Object

Run the imdb command

Searching

imdb Star Trek

Get a movie, supply a 7 digit IMDB id

imdb 0095016


16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
# File 'lib/imdb/cli.rb', line 16

def self.execute(stdout, arguments=[])
  
  @stdout = stdout
  
  options = {
  }
  mandatory_options = %w(  )
  
  parser = OptionParser.new do |opts|
    opts.banner = <<-BANNER.gsub(/^          /,'')
      IMDB #{Imdb::VERSION}

      Usage: #{File.basename($0)} Search Query
             #{File.basename($0)} 0095016

    BANNER
    opts.separator ""
    opts.on("-v", "--version",
            "Show the current version.") { stdout.puts "IMDB #{Imdb::VERSION}"; exit }
    opts.on("-h", "--help",
            "Show this help message.") { stdout.puts opts; exit }
    opts.parse!(arguments)

    if mandatory_options && mandatory_options.find { |option| options[option.to_sym].nil? }
      stdout.puts opts; exit
    end
  end

  query = arguments.join(" ").strip
  exit if query.blank?
  
  movie, search = nil, nil
  
  # If ID, fetch movie
  if query.match(/\d\d\d\d\d\d\d/)
    fetch_movie(query)
  else
    search_movie(query)
  end
end

.fetch_movie(imdb_id) ⇒ Object



57
58
59
60
61
62
63
# File 'lib/imdb/cli.rb', line 57

def self.fetch_movie(imdb_id)
  @stdout.puts ">> Fetching movie #{imdb_id}"
  
  movie = Imdb::Movie.new(imdb_id)
  
  display_movie_details(movie)
end

.search_movie(query) ⇒ Object



65
66
67
68
69
70
71
72
73
74
75
# File 'lib/imdb/cli.rb', line 65

def self.search_movie(query)
  @stdout.puts ">> Searching for \"#{query}\""
  
  search = Imdb::Search.new(query)
  
  if search.movies.size == 1
    display_movie_details(search.movies.first)
  else
    display_search_results(search.movies)
  end
end