Class: ImdbVoteHistory

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

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(id) ⇒ ImdbVoteHistory

Ingoing argument is the id to fetch movies from.



9
10
11
12
# File 'lib/imdb_vote_history.rb', line 9

def initialize(id)
  @movies = []
  @url    = "http://www.imdb.com/mymovies/list?l=#{id}"
end

Instance Attribute Details

#urlObject (readonly)

Returns the value of attribute url.



6
7
8
# File 'lib/imdb_vote_history.rb', line 6

def url
  @url
end

Class Method Details

.find_by_id(id) ⇒ Object

Fetches movies for the given ID. Returns an ImdbVoteHistory object.

Raises:

  • (ArgumentError)


26
27
28
29
# File 'lib/imdb_vote_history.rb', line 26

def self.find_by_id(id)
  raise ArgumentError.new("The id #{id} is invalid") unless id.to_s.match(/^\d{2,}$/)
  ImdbVoteHistory.new(id)
end

.find_by_url(url) ⇒ Object

Fetches movies for the given URL. Returns an ImdbVoteHistory object. The URL must be valid, otherwise an argument error will be raised. Example of valid URL:

> www.imdb.com/mymovies/list?l=32558051

Raises:

  • (ArgumentError)


19
20
21
22
# File 'lib/imdb_vote_history.rb', line 19

def self.find_by_url(url)
  raise ArgumentError.new("The url #{url} is invalid") unless url.to_s.match(/^(http:\/\/)?(w{3}\.)?imdb\.com\/mymovies\/list\?l=\d{2,}$/)
  ImdbVoteHistory.new(url.match(/list\?l=(\d+)/).to_a[1])
end

Instance Method Details

#allObject

Should we fetch all movies, even though pagination exists?



47
48
49
# File 'lib/imdb_vote_history.rb', line 47

def all
  @all = self
end

#idObject

A unique id for this particular list. Return type is Fixnum.



42
43
44
# File 'lib/imdb_vote_history.rb', line 42

def id
  @url.match(/list\?l=(\d+)/).to_a[1].to_i
end

#moviesObject

Returns a list of movies of the Container::Movie type.



52
53
54
# File 'lib/imdb_vote_history.rb', line 52

def movies
  prepare! unless @movies.any?; @movies
end

#userObject

The owners username, nil if the page doesn’t exists.



32
33
34
35
36
37
38
# File 'lib/imdb_vote_history.rb', line 32

def user
  begin
    content.at_css(".blurb a:nth-child(1)").content
  rescue NoMethodError
    nil # The default value if no user i found
  end
end