Class: Imdb::Movie

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

Overview

Represents a Movie on IMDB.com

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(imdb_id, title = nil) ⇒ Movie

Initialize a new IMDB movie object with it’s IMDB id (as a String)

movie = Imdb::Movie.new("0095016")

Imdb::Movie objects are lazy loading, meaning that no HTTP request will be performed when a new object is created. Only when you use an accessor that needs the remote data, a HTTP request is made (once).



15
16
17
18
19
# File 'lib/imdb/movie.rb', line 15

def initialize(imdb_id, title = nil)
  @id = imdb_id
  @url = "http://www.imdb.com/title/tt#{imdb_id}/"
  @title = title.gsub(/"/, "") if title
end

Instance Attribute Details

#idObject

Returns the value of attribute id.



5
6
7
# File 'lib/imdb/movie.rb', line 5

def id
  @id
end

#title(force_refresh = false) ⇒ Object

Returns a string containing the title



63
64
65
# File 'lib/imdb/movie.rb', line 63

def title
  @title
end

#urlObject

Returns the value of attribute url.



5
6
7
# File 'lib/imdb/movie.rb', line 5

def url
  @url
end

Instance Method Details

#cast_membersObject

Returns an array with cast members



22
23
24
# File 'lib/imdb/movie.rb', line 22

def cast_members
  document.search("table.cast td.nm a").map { |link| link.innerHTML.strip.imdb_unescape_html } rescue []
end

#directorObject

Returns the name of the director



27
28
29
30
# File 'lib/imdb/movie.rb', line 27

def director
  # document.at("h5[text()='Director:'] ~ a").innerHTML.strip.imdb_unescape_html rescue nil
  document.search("h5[text()^='Director'] ~ a").map { |link| link.innerHTML.strip.imdb_unescape_html } rescue []
end

#genresObject

Returns an array of genres (as strings)



33
34
35
# File 'lib/imdb/movie.rb', line 33

def genres
  document.search("h5[text()='Genre:'] ~ a[@href*=/Sections/Genres/']").map { |link| link.innerHTML.strip.imdb_unescape_html } rescue []
end

#lengthObject

Returns the duration of the movie in minutes as an integer.



38
39
40
# File 'lib/imdb/movie.rb', line 38

def length
  document.search("//h5[text()^='Runtime']/..").innerHTML[/\d+ min/].to_i rescue nil
end

#plotObject

Returns a string containing the plot.



43
44
45
# File 'lib/imdb/movie.rb', line 43

def plot
  document.search("//h5[text()^='Plot']/..").innerHTML.split("\n")[2].gsub(/<.+>.+<\/.+>/, '').strip.imdb_unescape_html rescue nil
end

#posterObject

Returns a string containing the URL to the movie poster.



48
49
50
# File 'lib/imdb/movie.rb', line 48

def poster
  document.at("a[@name='poster'] img")['src'][/http:.+@@/] + '.jpg' rescue nil
end

#ratingObject

Returns a float containing the average user rating



53
54
55
# File 'lib/imdb/movie.rb', line 53

def rating
  document.at(".general.rating b").innerHTML.strip.imdb_unescape_html.split('/').first.to_f rescue nil
end

#release_dateObject

Returns a date containing the release date fo the movie.



77
78
79
80
# File 'lib/imdb/movie.rb', line 77

def release_date
  @date = Date.strptime(document.search("//h5[text()^='Release Date']/..").innerHTML.split("\n")[2].gsub(/<.+>.+<\/.+>/, '').split('(').first.strip,
                        '%d %b %Y') rescue nil
end

#taglineObject

Returns a string containing the tagline



58
59
60
# File 'lib/imdb/movie.rb', line 58

def tagline
  document.search("//h5[text()^='Tagline']/..").innerHTML.split("\n")[2].gsub(/<.+>.+<\/.+>/, '').strip.imdb_unescape_html rescue nil
end

#yearObject

Returns an integer containing the year (CCYY) the movie was released in.



72
73
74
# File 'lib/imdb/movie.rb', line 72

def year
  document.search('a[@href^="/Sections/Years/"]').innerHTML.to_i
end