Class: Imdb::Movie
- Inherits:
-
Object
- Object
- Imdb::Movie
- Defined in:
- lib/imdb/movie.rb
Overview
Represents a Movie on IMDB.com
Instance Attribute Summary collapse
-
#id ⇒ Object
Returns the value of attribute id.
-
#title(force_refresh = false) ⇒ Object
Returns a string containing the title.
-
#url ⇒ Object
Returns the value of attribute url.
Instance Method Summary collapse
-
#cast_members ⇒ Object
Returns an array with cast members.
-
#director ⇒ Object
Returns the name of the director.
-
#genres ⇒ Object
Returns an array of genres (as strings).
-
#initialize(imdb_id, title = nil) ⇒ Movie
constructor
Initialize a new IMDB movie object with it’s IMDB id (as a String).
-
#length ⇒ Object
Returns the duration of the movie in minutes as an integer.
-
#plot ⇒ Object
Returns a string containing the plot.
-
#poster ⇒ Object
Returns a string containing the URL to the movie poster.
-
#rating ⇒ Object
Returns a float containing the average user rating.
-
#release_date ⇒ Object
Returns a date containing the release date fo the movie.
-
#tagline ⇒ Object
Returns a string containing the tagline.
-
#year ⇒ Object
Returns an integer containing the year (CCYY) the movie was released in.
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
#id ⇒ Object
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 |
#url ⇒ Object
Returns the value of attribute url.
5 6 7 |
# File 'lib/imdb/movie.rb', line 5 def url @url end |
Instance Method Details
#cast_members ⇒ Object
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 |
#director ⇒ Object
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 |
#genres ⇒ Object
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 |
#length ⇒ Object
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 |
#plot ⇒ Object
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 |
#poster ⇒ Object
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 |
#rating ⇒ Object
Returns a float containing the average user rating
53 54 55 |
# File 'lib/imdb/movie.rb', line 53 def document.at(".general.rating b").innerHTML.strip.imdb_unescape_html.split('/').first.to_f rescue nil end |
#release_date ⇒ Object
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 |
#tagline ⇒ Object
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 |
#year ⇒ Object
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 |