Class: TmdbMovie

Inherits:
Object
  • Object
show all
Defined in:
lib/ruby-tmdb3/tmdb_movie.rb

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.find(options) ⇒ Object

Raises:

  • (ArgumentError)


3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
# File 'lib/ruby-tmdb3/tmdb_movie.rb', line 3

def self.find(options)
  options = {
    :expand_results => true
  }.merge(options)
  
  raise ArgumentError, "At least one of: id, title, imdb should be supplied" if(options[:id].nil? && options[:title].nil? && options[:imdb].nil?)
  
  results = []
  unless(options[:id].nil? || options[:id].to_s.empty?)
    results << Tmdb.api_call("movie", {id: options[:id].to_s}, options[:language])
  end
  unless(options[:title].nil? || options[:title].to_s.empty?)
    api_return = Tmdb.api_call("search/movie", {query: options[:title].to_s}, options[:language])
    results << api_return["results"] if api_return
  end
  unless(options[:imdb].nil? || options[:imdb].to_s.empty?)
    results << Tmdb.api_call("movie", {id: options[:imdb].to_s}, options[:language])
    options[:expand_results] = true
  end
  
  results.flatten!(1)
  results.uniq!
  results.delete_if &:nil?
  
  unless(options[:limit].nil?)
    raise ArgumentError, ":limit must be an integer greater than 0" unless(options[:limit].is_a?(Fixnum) && options[:limit] > 0)
    results = results.slice(0, options[:limit])
  end
  
  results.map!{|m| TmdbMovie.new(m, options[:expand_results], options[:language])}
  
  if(results.length == 1)
    return results.first
  else
    return results
  end
end

.new(raw_data, expand_results = false, language = nil) ⇒ Object



41
42
43
44
45
46
47
48
49
50
51
52
53
# File 'lib/ruby-tmdb3/tmdb_movie.rb', line 41

def self.new(raw_data, expand_results = false, language = nil)
  # expand the result by calling movie unless :expand_results is false or the data is already complete
  # (as determined by checking for the runtime property in the raw data)
  if(expand_results && !raw_data.has_key?("runtime"))
    begin
      expanded_data = Tmdb.api_call("movie", {id: raw_data["id"].to_s}, language)
    rescue RuntimeError => e
      raise ArgumentError, "Unable to fetch expanded info for Movie ID: '#{raw_data["id"]}'" if expanded_data.nil?
    end
    raw_data = expanded_data
  end
  return Tmdb.data_to_object(raw_data)
end

Instance Method Details

#==(other) ⇒ Object



55
56
57
58
# File 'lib/ruby-tmdb3/tmdb_movie.rb', line 55

def ==(other)
  return false unless(other.is_a?(TmdbMovie))
  return @raw_data == other.raw_data
end