Class: Movies

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

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(url, params = {}) ⇒ Movies

Returns a new instance of Movies.



10
11
12
13
14
15
16
17
18
19
20
21
22
# File 'lib/movies.rb', line 10

def initialize(url, params = {})
  if params.keys.include?(:callback)
    raise ArgumentError.new("Passing the callback option makes not sense.")
  end
  
  @params = params
  
  if params.empty?
    @url = url
  else
    @url = "#{url}&#{(@params).map{|key, value| "#{key}=#{value}"}.join("&")}"
  end
end

Instance Attribute Details

#actorsObject (readonly)

Returns the value of attribute actors.



8
9
10
# File 'lib/movies.rb', line 8

def actors
  @actors
end

#directorObject (readonly)

Returns the value of attribute director.



8
9
10
# File 'lib/movies.rb', line 8

def director
  @director
end

#genresObject (readonly)

Returns the value of attribute genres.



8
9
10
# File 'lib/movies.rb', line 8

def genres
  @genres
end

#idObject (readonly)

Returns the value of attribute id.



8
9
10
# File 'lib/movies.rb', line 8

def id
  @id
end

#plotObject (readonly)

Returns the value of attribute plot.



8
9
10
# File 'lib/movies.rb', line 8

def plot
  @plot
end

#posterObject (readonly)

Returns the value of attribute poster.



8
9
10
# File 'lib/movies.rb', line 8

def poster
  @poster
end

#ratedObject (readonly)

Returns the value of attribute rated.



8
9
10
# File 'lib/movies.rb', line 8

def rated
  @rated
end

#ratingObject (readonly)

Returns the value of attribute rating.



8
9
10
# File 'lib/movies.rb', line 8

def rating
  @rating
end

#runtimeObject (readonly)

Returns the value of attribute runtime.



8
9
10
# File 'lib/movies.rb', line 8

def runtime
  @runtime
end

#titleObject (readonly)

Returns the value of attribute title.



8
9
10
# File 'lib/movies.rb', line 8

def title
  @title
end

#votesObject (readonly)

Returns the value of attribute votes.



8
9
10
# File 'lib/movies.rb', line 8

def votes
  @votes
end

#writersObject (readonly)

Returns the value of attribute writers.



8
9
10
# File 'lib/movies.rb', line 8

def writers
  @writers
end

#yearObject (readonly)

Returns the value of attribute year.



8
9
10
# File 'lib/movies.rb', line 8

def year
  @year
end

Class Method Details

.find_by_id(id, params = {}) ⇒ Object



24
25
26
27
28
29
# File 'lib/movies.rb', line 24

def self.find_by_id(id, params = {})
  unless id.to_s.match(/tt\d{4,}/)
    raise ArgumentError.new("The id is not valid.")
  end
  Movies.new("http://www.imdbapi.com/?i=#{id}", params).prepare
end

.find_by_release_name(title, params = {}) ⇒ Object



38
39
40
41
42
43
44
45
# File 'lib/movies.rb', line 38

def self.find_by_release_name(title, params = {})
  if title.nil? or title.empty?
    raise ArgumentError.new("Title can not be blank.")
  end
  
  mf = MovieFilter.new(title: title)
  Movies.new("http://www.imdbapi.com/?t=#{URI.encode(mf.title)}", mf.to_param).prepare
end

.find_by_title(title, params = {}) ⇒ Object



31
32
33
34
35
36
# File 'lib/movies.rb', line 31

def self.find_by_title(title, params = {})
  if title.nil? or title.empty?
    raise ArgumentError.new("Title can not be blank.")
  end
  Movies.new("http://www.imdbapi.com/?t=#{URI.encode(title)}", params).prepare
end

Instance Method Details

#found?Boolean

Returns:

  • (Boolean)


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

def found?
  content["Response"] == "True"
end

#hrefObject



104
105
106
# File 'lib/movies.rb', line 104

def href
  "http://www.imdb.com/title/#{@id}/"
end

#prepareObject



47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
# File 'lib/movies.rb', line 47

def prepare
  tap do
    return self unless found?
    
    content.keys.each do |name| 
      instance_variable_set "@" + name.to_s.downcase, (content[name] == "N/A" ? "" : content[name])
    end
    
    @year    = @year.to_i
    @genres  = @genre.split(", ")
    @writers = @writer.split(", ")
    @actors  = @actors.split(", ")
    @rating  = @rating.to_f
    @votes   = @votes.to_i
    
    if @runtime =~ /(\d+).+?(\d+)/
      @runtime = $1.to_i * 60 + $2.to_i
    elsif @runtime =~ /(\d+) hrs/
      @runtime = $1.to_i * 60
    else
      @runtime = 0
    end 
  end
end

#releasedObject



76
77
78
79
80
# File 'lib/movies.rb', line 76

def released
  Date.parse(@released)
rescue
  return nil
end

#tomatoObject



82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
# File 'lib/movies.rb', line 82

def tomato
  unless @params[:tomatoes]
    raise ArgumentError.new("You have to set 'tomatoes' to true to get this data.")
  end
  
  @_tomato ||= Struct.new(
    :meter, 
    :image,
    :rating,
    :reviews,
    :fresh,
    :rotten
  ).new(
    @tomatometer.to_i, 
    @tomatoimage,
    @tomatorating.to_f,
    @tomatoreviews.to_i,
    @tomatofresh.to_i,
    @tomatorotten.to_i
  )
end