class MultimediaParadise::Video::MovieSearcher

Constants

API_KEY
NAMESPACE
#

NAMESPACE

#

Public Class Methods

[](i = '') click to toggle source
#

MultimediaParadise::Video::MovieSearcher[]

#
# File lib/multimedia_paradise/video/movie_searcher.rb, line 273
def self.[](i = '')
  new(i)
end
new( i = nil, run_already = true ) click to toggle source
#

initialize

#
# File lib/multimedia_paradise/video/movie_searcher.rb, line 53
def initialize(
    i           = nil,
    run_already = true
  )
  reset
  set_name_of_the_movie(i)
  run if run_already
end

Public Instance Methods

api_key?() click to toggle source
#

api_key?

#
# File lib/multimedia_paradise/video/movie_searcher.rb, line 88
def api_key?
  @api_key
end
cliner(optional_input = nil) click to toggle source
#

cliner

If input is provided, then we will use this embedded into the line.

#
# File lib/multimedia_paradise/video/movie_searcher.rb, line 152
def cliner(optional_input = nil)
  result = '=' * @width_limit
  if optional_input
    _ = " #{optional_input} "
    # ===================================================================== #
    # Also colourize the title there.
    # ===================================================================== #
    result[1,_.size] = palegoldenrod(_)
  end
  return result+N
end
determine_whether_rest_client_is_available_and_exit_if_it_is_unavailable() click to toggle source
#

determine_whether_rest_client_is_available_and_exit_if_it_is_unavailable

#
# File lib/multimedia_paradise/video/movie_searcher.rb, line 167
def determine_whether_rest_client_is_available_and_exit_if_it_is_unavailable

  begin
    require 'rest-client'
  rescue LoadError
    unless Object.const_defined? :RestClient
      opnn; e "The gem #{slateblue('rest-client')}"\
              " appears to be missing. Please install it"
      opnn; e 'first, as this class depends on the namespace '\
              'RestClient.'
      exit 1
    end
  end

  begin
    require 'json'
  rescue LoadError
    unless Object.const_defined? :Json
      opnn; e "The gem #{slateblue('json')}"\
              " appears to be missing. Please install it"
      opnn; e 'first, as this class depends on json indirectly.'
      exit 1
    end
  end

end
display_main_string() click to toggle source
#

display_main_string

#
# File lib/multimedia_paradise/video/movie_searcher.rb, line 132
def display_main_string
  e @main_string
end
movie_name?()
Alias for: name_of_the_movie?
name_of_the_movie?() click to toggle source
#

name_of_the_movie?

#
# File lib/multimedia_paradise/video/movie_searcher.rb, line 116
def name_of_the_movie?
  @name_of_the_movie
end
Also aliased as: movie_name?
report_that_no_movie_has_been_found_with_this_name( i = movie_name? ) click to toggle source
#

report_that_no_movie_has_been_found_with_this_name

#
# File lib/multimedia_paradise/video/movie_searcher.rb, line 139
def report_that_no_movie_has_been_found_with_this_name(
    i = movie_name?
  )
  e
  opnn; e "No movie has been found with this name (#{sfancy(i)})."
  e
end
reset() click to toggle source
#

reset (reset tag)

#
Calls superclass method MultimediaParadise::Base#reset
# File lib/multimedia_paradise/video/movie_searcher.rb, line 65
def reset
  super()
  # ======================================================================= #
  # === @namespace
  # ======================================================================= #
  @namespace = NAMESPACE
  # ======================================================================= #
  # === @width_limit
  # ======================================================================= #
  @width_limit = 78
  # ======================================================================= #
  # === @main_string
  # ======================================================================= #
  @main_string = ''.dup
  # ======================================================================= #
  # === @api_key
  # ======================================================================= #
  @api_key = API_KEY
end
run() click to toggle source
#

run (run tag)

#
# File lib/multimedia_paradise/video/movie_searcher.rb, line 197
def run
  determine_whether_rest_client_is_available_and_exit_if_it_is_unavailable
  # ======================================================================= #
  # First, denote the names to end the program early.
  # ======================================================================= #
  _ = movie_name? # Pointer to the movie name.
  case _ # case tag
  when 'quit','exit'
    exit 0
  else
    url = "http://www.omdbapi.com/?t=#{_}&apikey=#{api_key?}"
    if debug?
      opnn; e 'Now working on the URL `'+sfancy(url)+'`.'
    end
    # ===================================================================== #
    # The constructed URl would look like this:
    #   http://www.omdbapi.com/?t=Alien&apikey=946f500a
    # ===================================================================== #
    response = RestClient.get(url)
    # ===================================================================== #
    # Next, parse the response via JSON.
    # ===================================================================== #
    info = JSON.parse(response)
    # ===================================================================== #
    # Handle exceptions, in case that we obtain an invalid response.
    # ===================================================================== #
    case info['Response'].to_s
    when 'False'
      report_that_no_movie_has_been_found_with_this_name(_)
      exit 0
    else
      # =================================================================== #
      # We have to rescue in the event that no tomato score has been
      # supplied. Additionally, we will word wrap to the plot content
      # and the actors of said movie.
      #
      # Commandline examples:
      #
      #   commandline_imdb Toy Story
      #
      # =================================================================== #
      begin
        title = info['Title']
        year  = info['Year']
        score = info['Ratings'][1]['Value']
      rescue
        score = 'No Score Found'
      end
      indent_to_use = 65
      rated    = info['Rated']
      genre    = info['Genre']
      director = info['Director']
      actors   = wrap(info['Actors'], indent_to_use)
      plot_unformatted = info['Plot']
      plot     = wrap(plot_unformatted, indent_to_use)
      @main_string << "#{N}" << cliner('Movie')
      @main_string << "| #{mediumorchid('Title')}:    #{title}"+N
      @main_string << "| #{mediumpurple('Year')}:     #{year.rjust(5)}"+N
      @main_string << "| #{skyblue('Tomato')}:   #{score.rjust(5)}"+N
      @main_string << "| #{royalblue('Rated')}:    #{rated.center(5)}"+N
      @main_string << "| #{palegreen('Genre')}:    #{genre}"+N
      @main_string << "| #{indianred('Director')}: #{director}"+N
      @main_string << "| #{springgreen('Actors')}:   #{actors}"+N
      # Add the Plot subsection next.
      # =================================================================== #
      @main_string << "| #{seagreen('Plot')}:     #{plot}"+N
      # =================================================================== #
      @main_string << cliner+N
      display_main_string
    end
  end
end
set_name_of_the_movie(i = '') click to toggle source
#

set_name_of_the_movie

#
# File lib/multimedia_paradise/video/movie_searcher.rb, line 95
def set_name_of_the_movie(i = '')
  i = i.join(' ').strip if i.is_a? Array
  i = i.to_s.dup.chomp.strip
  # ======================================================================= #
  # We may also do some sanitizing. First, get rid of the file extension
  # if the input includes a '.' token.
  # ======================================================================= #
  if i.include? '.'
    i.sub!(/#{File.extname(i)}/,'')
  end
  if i.include?('_') and (i =~ /^\d+/)
    use_this_regex = /\d{1,3}_(.+)/ # See: http://rubular.com/r/9fsrGiTQi1
    i =~ use_this_regex
    i = $1.to_s.dup if $1
  end
  @name_of_the_movie = i
end
wrap(i, width_limit = @width_limit) click to toggle source
#

wrap (wrap tag)

A word-wrapping method.

#
# File lib/multimedia_paradise/video/movie_searcher.rb, line 125
def wrap(i, width_limit = @width_limit)
  i.gsub(/(.{1,#{width_limit}})(\s+|\Z)/, "\\1\n| ")
end