Class: Enceladus::Movie
- Inherits:
-
ApiResource
- Object
- ApiResource
- Enceladus::Movie
- Defined in:
- lib/enceladus/models/movie.rb
Constant Summary collapse
- RESOURCE_ATTRIBUTES =
[ :adult, :backdrop_path, :id, :original_title, :release_date, :poster_path, :youtube_trailers, :popularity, :title, :vote_average, :vote_count, :belongs_to_collection, :budget, :homepage, :imdb_id, :releases, :overview, :revenue, :runtime, :status, :tagline, :genres, :production_companies, :production_countries, :spoken_languages, :rating, :cast ].map(&:freeze).freeze
Class Method Summary collapse
-
.find(id) ⇒ Object
Find a movie by TMDb ID or imdb_id.
-
.find_by_title(title) ⇒ Object
Find movies by title.
-
.now_playing ⇒ Object
Returns a paginated collection of movies playing in theatres.
-
.popular ⇒ Object
Returns a paginated collection of popular movies.
-
.top_rated ⇒ Object
Returns a paginated collection of top rated movies.
-
.upcoming ⇒ Object
Returns a paginated collection of upcoming movies.
Instance Method Summary collapse
-
#alternative_titles(country = nil) ⇒ Object
Returns a collection of alternative titles for a specific movie.
-
#backdrop_urls ⇒ Object
Returns an array containing URL’s (as string) for the movie background picture.
-
#cast ⇒ Object
Returns an array of Cast for the movie.
-
#genres=(genres_from_response) ⇒ Object
Method used by Enceladus::ApiResource to save genres fetched by TMDb API.
-
#poster_urls ⇒ Object
Returns an array containing URL’s (as string) for the movie poster picture.
-
#production_companies=(production_companies_from_response) ⇒ Object
Method used by Enceladus::ApiResource to save production companies fetched by TMDb API.
-
#production_countries=(production_countries_from_response) ⇒ Object
Method used by Enceladus::ApiResource to save production countries fetched by TMDb API.
-
#rate!(account, rating) ⇒ Object
Rate a movie.
-
#releases=(releases_from_response) ⇒ Object
Method used by Enceladus::ApiResource to save releases fetched by TMDb API.
-
#reload ⇒ Object
Fetchs details of movie information on TMDb API.
-
#similar ⇒ Object
Given a movie, this method returns a paginated collection of similar movies.
-
#spoken_languages=(spoken_languages_from_response) ⇒ Object
Method used by Enceladus::ApiResource to save spoken languages fetched by TMDb API.
-
#youtube_trailers=(trailers_from_response) ⇒ Object
Method used by Enceladus::ApiResource to save trailers fetched by TMDb API.
Class Method Details
.find(id) ⇒ Object
14 15 16 |
# File 'lib/enceladus/models/movie.rb', line 14 def self.find(id) build_single_resource(Enceladus::Requester.get("movie/#{id}", default_params)) end |
.find_by_title(title) ⇒ Object
22 23 24 |
# File 'lib/enceladus/models/movie.rb', line 22 def self.find_by_title(title) Enceladus::MovieCollection.new("search/movie", default_params.merge(query: title)) end |
.now_playing ⇒ Object
Returns a paginated collection of movies playing in theatres.
45 46 47 |
# File 'lib/enceladus/models/movie.rb', line 45 def self. Enceladus::MovieCollection.new("movie/now_playing", Enceladus::Movie.default_params(only: :language)) end |
.popular ⇒ Object
Returns a paginated collection of popular movies.
50 51 52 |
# File 'lib/enceladus/models/movie.rb', line 50 def self.popular Enceladus::MovieCollection.new("movie/popular", Enceladus::Movie.default_params(only: :language)) end |
.top_rated ⇒ Object
Returns a paginated collection of top rated movies.
55 56 57 |
# File 'lib/enceladus/models/movie.rb', line 55 def self.top_rated Enceladus::MovieCollection.new("movie/top_rated", Enceladus::Movie.default_params(only: :language)) end |
.upcoming ⇒ Object
Returns a paginated collection of upcoming movies. Resources are paginates and respond to methods such as: next_page, all, previous_page, etc… Examples:
collection = Enceladus::Movie.upcoming
collection.all
=> [Movie, Movie, ...]
collection.current_page
=> 1
collection.next_page
=> [Movie, Movie, ...]
collection.current_page
=> 2
40 41 42 |
# File 'lib/enceladus/models/movie.rb', line 40 def self.upcoming Enceladus::MovieCollection.new("movie/upcoming", Enceladus::Movie.default_params(only: :language)) end |
Instance Method Details
#alternative_titles(country = nil) ⇒ Object
Returns a collection of alternative titles for a specific movie. A country can be specified to fetch alternative titles in a certain place. The country refers to the ISO code 3166-1. Invalid country codes returns empty collections. Examples:
movie = Enceladus::Movie.find_by_title("Star Wars").first
movie.alternative_titles("BR")
=> #<Enceladus::AlternativeTitle @iso_3166_1="BR", @title="Star Wars: Episódio VII - O Despertar da Força">
movie.alternative_titles
=> [#<Enceladus::AlternativeTitle @iso_3166_1="US", @title="Star Wars 7">, #<Enceladus::AlternativeTitle @iso_3166_1="ES", @title="Star Wars El despertar de la Fuerza">, ..., ...]
80 81 82 83 84 |
# File 'lib/enceladus/models/movie.rb', line 80 def alternative_titles(country=nil) opts = Enceladus::Movie.default_params(only: :append_to_response) opts = opts.merge(country: country) unless country.nil? Enceladus::AlternativeTitle.build_collection(Enceladus::Requester.get("movie/#{id}/alternative_titles", opts).titles) end |
#backdrop_urls ⇒ Object
Returns an array containing URL’s (as string) for the movie background picture.
153 154 155 |
# File 'lib/enceladus/models/movie.rb', line 153 def backdrop_urls Enceladus::Configuration::Image.instance.url_for("backdrop", backdrop_path) end |
#cast ⇒ Object
Returns an array of Cast for the movie.
148 149 150 |
# File 'lib/enceladus/models/movie.rb', line 148 def cast @cast ||= Enceladus::Cast.build_collection(Enceladus::Requester.get("movie/#{id}/credits").cast) end |
#genres=(genres_from_response) ⇒ Object
Method used by Enceladus::ApiResource to save genres fetched by TMDb API.
114 115 116 |
# File 'lib/enceladus/models/movie.rb', line 114 def genres=(genres_from_response) @genres = Enceladus::Genre.build_collection(genres_from_response) end |
#poster_urls ⇒ Object
Returns an array containing URL’s (as string) for the movie poster picture.
158 159 160 |
# File 'lib/enceladus/models/movie.rb', line 158 def poster_urls Enceladus::Configuration::Image.instance.url_for("poster", poster_path) end |
#production_companies=(production_companies_from_response) ⇒ Object
Method used by Enceladus::ApiResource to save production companies fetched by TMDb API.
119 120 121 |
# File 'lib/enceladus/models/movie.rb', line 119 def production_companies=(production_companies_from_response) @production_companies = Enceladus::ProductionCompany.build_collection(production_companies_from_response) end |
#production_countries=(production_countries_from_response) ⇒ Object
Method used by Enceladus::ApiResource to save production countries fetched by TMDb API.
124 125 126 |
# File 'lib/enceladus/models/movie.rb', line 124 def production_countries=(production_countries_from_response) @production_countries = Enceladus::ProductionCountry.build_collection(production_countries_from_response) end |
#rate!(account, rating) ⇒ Object
Rate a movie. The argument account can be Enceladus::Account or Enceladus::GuestAccount. The argument rating must be an numeric value between 0 and 10. Examples:
movie = Enceladus::Movie.find(550)
guest_account = Enceladus::GuestAccount.new
movie.rate!(guest_account, 7.5)
account = Enceladus::Account.new("username", "password")
movie.rate!(account, 8.3)
TMDb expects the rating value to be divisors of 0.5, this method rounds the value properly before making the request.
99 100 101 102 103 104 105 106 107 108 109 110 111 |
# File 'lib/enceladus/models/movie.rb', line 99 def rate!(account, ) params = {} if account.kind_of?(Enceladus::Account) params[:session_id] = account.session_id elsif account.kind_of?(Enceladus::GuestAccount) params[:guest_session_id] = account.session_id else raise Enceladus::Exception::ArgumentError.new("account must be one of Enceladus::Account or Enceladus::GuestAccount") end form_data = { value: ( * 2).round / 2.0 } Enceladus::Requester.post("movie/#{id}/rating", params, form_data) end |
#releases=(releases_from_response) ⇒ Object
Method used by Enceladus::ApiResource to save releases fetched by TMDb API.
134 135 136 137 138 |
# File 'lib/enceladus/models/movie.rb', line 134 def releases=(releases_from_response) if !releases_from_response.nil? && !releases_from_response.countries? @releases = Enceladus::Release.build_collection(releases_from_response.countries) end end |
#reload ⇒ Object
Fetchs details of movie information on TMDb API.
65 66 67 |
# File 'lib/enceladus/models/movie.rb', line 65 def reload rebuild_single_resource(Enceladus::Requester.get("movie/#{id}", Enceladus::Movie.default_params)) end |
#similar ⇒ Object
Given a movie, this method returns a paginated collection of similar movies.
60 61 62 |
# File 'lib/enceladus/models/movie.rb', line 60 def similar Enceladus::MovieCollection.new("movie/#{id}/similar", Enceladus::Movie.default_params(only: [:language, :append_to_response])) end |
#spoken_languages=(spoken_languages_from_response) ⇒ Object
Method used by Enceladus::ApiResource to save spoken languages fetched by TMDb API.
129 130 131 |
# File 'lib/enceladus/models/movie.rb', line 129 def spoken_languages=(spoken_languages_from_response) @spoken_languages = Enceladus::SpokenLanguage.build_collection(spoken_languages_from_response) end |
#youtube_trailers=(trailers_from_response) ⇒ Object
Method used by Enceladus::ApiResource to save trailers fetched by TMDb API.
141 142 143 144 145 |
# File 'lib/enceladus/models/movie.rb', line 141 def youtube_trailers=(trailers_from_response) if !trailers_from_response.nil? && !trailers_from_response.youtube.nil? @youtube_trailers = Enceladus::YouTubeTrailer.build_collection(trailers_from_response.youtube) end end |