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



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

.display_search_results(movies = []) ⇒ Object



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

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

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 = "\nUsage: \#{File.basename($0)} Search Query\n   \#{File.basename($0)} 0095016\n\n    BANNER\n    opts.separator \"\"\n    opts.on(\"-v\", \"--version\",\n            \"Show the current version.\") { stdout.puts \"IMDB \#{Imdb::VERSION}\"; exit }\n    opts.on(\"-h\", \"--help\",\n            \"Show this help message.\") { stdout.puts opts; exit }\n    opts.parse!(arguments)\n\n    if mandatory_options && mandatory_options.find { |option| options[option.to_sym].nil? }\n      stdout.puts opts; exit\n    end\n  end\n\n  query = arguments.join(\" \").strip\n  exit if query.blank?\n  \n  movie, search = nil, nil\n  \n  # If ID, fetch movie\n  if query.match(/(\\d\\d\\d\\d\\d\\d\\d)/) || query.downcase.match(/^http:\\/\\/[www.]*imdb.com\\/title\\/tt(.+)\\/$/)\n    fetch_movie($1)\n  else\n    search_movie(query)\n  end\nend\n".gsub(/^          /,'')          

.fetch_movie(imdb_id) ⇒ Object



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

.search_movie(query) ⇒ Object



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