Class: Imdb::CLI

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

Class Method Summary (collapse)

Class Method Details

+ (Object) display_movie_details(movie)



81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
# File 'lib/imdb/cli.rb', line 81

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 "Poster URL: #{movie.poster}"
  @stdout.puts "IMDB URL: #{movie.url}"
  @stdout.puts "=" * 75
  @stdout.puts
end

+ (Object) display_search_results(movies = [])



100
101
102
103
104
105
106
# File 'lib/imdb/cli.rb', line 100

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

+ (Object) execute(stdout, arguments = [])

Run the imdb command

Searching

imdb Star Trek

Get a movie, supply a 7 digit IMDB id or the IMDB URL

imdb 0095016
imdb http://akas.imdb.com/title/tt0796366/


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
56
57
# File 'lib/imdb/cli.rb', line 17

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

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)/) || query.downcase.match(/^http:\/\/[www.]*imdb.com\/title\/tt(.+)\/$/)
    fetch_movie($1)
  else
    search_movie(query)
  end
end

+ (Object) fetch_movie(imdb_id)



59
60
61
62
63
64
65
66
# File 'lib/imdb/cli.rb', line 59

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

+ (Object) search_movie(query)



68
69
70
71
72
73
74
75
76
77
78
79
# File 'lib/imdb/cli.rb', line 68

def self.search_movie(query)
  @stdout.puts
  @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