Class: Movies
- Inherits:
-
Object
- Object
- Movies
- Defined in:
- lib/movies.rb
Instance Attribute Summary collapse
-
#actors ⇒ Object
readonly
Returns the value of attribute actors.
-
#director ⇒ Object
readonly
Returns the value of attribute director.
-
#genres ⇒ Object
readonly
Returns the value of attribute genres.
-
#id ⇒ Object
readonly
Returns the value of attribute id.
-
#plot ⇒ Object
readonly
Returns the value of attribute plot.
-
#poster ⇒ Object
readonly
Returns the value of attribute poster.
-
#rated ⇒ Object
readonly
Returns the value of attribute rated.
-
#rating ⇒ Object
readonly
Returns the value of attribute rating.
-
#runtime ⇒ Object
readonly
Returns the value of attribute runtime.
-
#title ⇒ Object
readonly
Returns the value of attribute title.
-
#votes ⇒ Object
readonly
Returns the value of attribute votes.
-
#writers ⇒ Object
readonly
Returns the value of attribute writers.
-
#year ⇒ Object
readonly
Returns the value of attribute year.
Class Method Summary collapse
- .find_by_id(id, params = {}) ⇒ Object
- .find_by_release_name(title, params = {}) ⇒ Object
- .find_by_title(title, params = {}) ⇒ Object
Instance Method Summary collapse
- #found? ⇒ Boolean
- #href ⇒ Object
-
#initialize(url, params = {}) ⇒ Movies
constructor
A new instance of Movies.
- #prepare ⇒ Object
- #released ⇒ Object
- #tomato ⇒ Object
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
#actors ⇒ Object (readonly)
Returns the value of attribute actors.
8 9 10 |
# File 'lib/movies.rb', line 8 def actors @actors end |
#director ⇒ Object (readonly)
Returns the value of attribute director.
8 9 10 |
# File 'lib/movies.rb', line 8 def director @director end |
#genres ⇒ Object (readonly)
Returns the value of attribute genres.
8 9 10 |
# File 'lib/movies.rb', line 8 def genres @genres end |
#id ⇒ Object (readonly)
Returns the value of attribute id.
8 9 10 |
# File 'lib/movies.rb', line 8 def id @id end |
#plot ⇒ Object (readonly)
Returns the value of attribute plot.
8 9 10 |
# File 'lib/movies.rb', line 8 def plot @plot end |
#poster ⇒ Object (readonly)
Returns the value of attribute poster.
8 9 10 |
# File 'lib/movies.rb', line 8 def poster @poster end |
#rated ⇒ Object (readonly)
Returns the value of attribute rated.
8 9 10 |
# File 'lib/movies.rb', line 8 def rated @rated end |
#rating ⇒ Object (readonly)
Returns the value of attribute rating.
8 9 10 |
# File 'lib/movies.rb', line 8 def @rating end |
#runtime ⇒ Object (readonly)
Returns the value of attribute runtime.
8 9 10 |
# File 'lib/movies.rb', line 8 def runtime @runtime end |
#title ⇒ Object (readonly)
Returns the value of attribute title.
8 9 10 |
# File 'lib/movies.rb', line 8 def title @title end |
#votes ⇒ Object (readonly)
Returns the value of attribute votes.
8 9 10 |
# File 'lib/movies.rb', line 8 def votes @votes end |
#writers ⇒ Object (readonly)
Returns the value of attribute writers.
8 9 10 |
# File 'lib/movies.rb', line 8 def writers @writers end |
#year ⇒ Object (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
72 73 74 |
# File 'lib/movies.rb', line 72 def found? content["Response"] == "True" end |
#href ⇒ Object
104 105 106 |
# File 'lib/movies.rb', line 104 def href "http://www.imdb.com/title/#{@id}/" end |
#prepare ⇒ Object
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 |
#released ⇒ Object
76 77 78 79 80 |
# File 'lib/movies.rb', line 76 def released Date.parse(@released) rescue return nil end |
#tomato ⇒ Object
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 |