enceladus 0.0.2
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/.gitignore +23 -0
- data/.rspec +1 -0
- data/.ruby-gemset +1 -0
- data/.ruby-version +1 -0
- data/.travis.yml +4 -0
- data/Gemfile +4 -0
- data/LICENSE.txt +22 -0
- data/README.md +36 -0
- data/Rakefile +2 -0
- data/enceladus.gemspec +29 -0
- data/lib/enceladus/configuration/api.rb +60 -0
- data/lib/enceladus/configuration/image.rb +83 -0
- data/lib/enceladus/exceptions.rb +5 -0
- data/lib/enceladus/models/account.rb +143 -0
- data/lib/enceladus/models/api_paginated_collection.rb +76 -0
- data/lib/enceladus/models/api_resource.rb +25 -0
- data/lib/enceladus/models/cast.rb +9 -0
- data/lib/enceladus/models/genre.rb +4 -0
- data/lib/enceladus/models/guest_account.rb +15 -0
- data/lib/enceladus/models/movie.rb +162 -0
- data/lib/enceladus/models/movie_collection.rb +3 -0
- data/lib/enceladus/models/production_company.rb +4 -0
- data/lib/enceladus/models/production_country.rb +4 -0
- data/lib/enceladus/models/release.rb +4 -0
- data/lib/enceladus/models/spoken_language.rb +4 -0
- data/lib/enceladus/models/you_tube_trailer.rb +9 -0
- data/lib/enceladus/requester.rb +68 -0
- data/lib/enceladus/version.rb +3 -0
- data/lib/enceladus.rb +43 -0
- data/spec/enceladus/configuration/api_spec.rb +59 -0
- data/spec/enceladus/configuration/image_spec.rb +200 -0
- data/spec/enceladus/enceladus_spec.rb +53 -0
- data/spec/enceladus/models/account_spec.rb +176 -0
- data/spec/enceladus/models/cast_spec.rb +18 -0
- data/spec/enceladus/models/guest_account_spec.rb +19 -0
- data/spec/enceladus/models/movie_collection_spec.rb +217 -0
- data/spec/enceladus/models/movie_spec.rb +469 -0
- data/spec/enceladus/models/you_tube_trailer_spec.rb +16 -0
- data/spec/spec_helper.rb +20 -0
- data/spec/support/responses/account_response.rb +27 -0
- data/spec/support/responses/authentication_response.rb +19 -0
- data/spec/support/responses/configuration_response.rb +32 -0
- data/spec/support/responses/credits_collection_response.rb +19 -0
- data/spec/support/responses/credits_response.rb +29 -0
- data/spec/support/responses/error_response.rb +12 -0
- data/spec/support/responses/guest_account_response.rb +21 -0
- data/spec/support/responses/movie_collection_resource_response.rb +36 -0
- data/spec/support/responses/movie_collection_response.rb +20 -0
- data/spec/support/responses/movie_response.rb +67 -0
- data/spec/support/responses/request_token_response.rb +21 -0
- data/spec/support/responses/session_response.rb +19 -0
- metadata +239 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: 5b09cdfa657d7ec3ce55c576659efe6f9333a840
|
4
|
+
data.tar.gz: ae3a1efb9ca54af43cacc201f5f5c5a0b65131ca
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 590d5b2c90c8d7d3461b8c1cf676497fc3e83d9fc102bbfe8624085522ad17739cd306ac6d327c58e0dc165f76375a3531bc8710723141509252dba32c5fd70f
|
7
|
+
data.tar.gz: c28b5c3bf39217ec32f3dd2dbf0d2f6884a3ce837890f2dcb025150b0355744f0a0973540a36f047cb76577a7b0227a7f0a8be3b1ec7742bc76e1567eaf48da4
|
data/.gitignore
ADDED
@@ -0,0 +1,23 @@
|
|
1
|
+
*.gem
|
2
|
+
*.rbc
|
3
|
+
.bundle
|
4
|
+
.config
|
5
|
+
.yardoc
|
6
|
+
Gemfile.lock
|
7
|
+
InstalledFiles
|
8
|
+
_yardoc
|
9
|
+
coverage
|
10
|
+
doc/
|
11
|
+
lib/bundler/man
|
12
|
+
pkg
|
13
|
+
rdoc
|
14
|
+
spec/reports
|
15
|
+
test/tmp
|
16
|
+
test/version_tmp
|
17
|
+
tmp
|
18
|
+
*.bundle
|
19
|
+
*.so
|
20
|
+
*.o
|
21
|
+
*.a
|
22
|
+
mkmf.log
|
23
|
+
run.rb
|
data/.rspec
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
--colour --format doc --order random
|
data/.ruby-gemset
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
enceladus
|
data/.ruby-version
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
ruby 2.1.2
|
data/.travis.yml
ADDED
data/Gemfile
ADDED
data/LICENSE.txt
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
Copyright (c) 2014 Vinicius Osiro
|
2
|
+
|
3
|
+
MIT License
|
4
|
+
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
6
|
+
a copy of this software and associated documentation files (the
|
7
|
+
"Software"), to deal in the Software without restriction, including
|
8
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
9
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
10
|
+
permit persons to whom the Software is furnished to do so, subject to
|
11
|
+
the following conditions:
|
12
|
+
|
13
|
+
The above copyright notice and this permission notice shall be
|
14
|
+
included in all copies or substantial portions of the Software.
|
15
|
+
|
16
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
17
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
18
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
19
|
+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
20
|
+
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
21
|
+
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
22
|
+
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,36 @@
|
|
1
|
+
# Enceladus
|
2
|
+
|
3
|
+
[![Code Climate](https://codeclimate.com/github/osiro/enceladus/badges/gpa.svg)](https://codeclimate.com/github/osiro/enceladus)
|
4
|
+
[![Build Status](https://travis-ci.org/osiro/enceladus.svg?branch=master)](https://travis-ci.org/osiro/enceladus)
|
5
|
+
[![Coverage Status](https://coveralls.io/repos/osiro/enceladus/badge.png?branch=master)](https://coveralls.io/r/osiro/enceladus?branch=master)
|
6
|
+
[![Dependency Status](https://gemnasium.com/osiro/enceladus.svg)](https://gemnasium.com/osiro/enceladus)
|
7
|
+
|
8
|
+
A ruby wrapper for [TMDb](www.themoviedb.org).
|
9
|
+
|
10
|
+
| [Wiki](https://github.com/osiro/enceladus/wiki) | [Documentation](http://www.rubydoc.info/github/osiro/enceladus/master/frames) |
|
11
|
+
|
12
|
+
## Installation
|
13
|
+
|
14
|
+
Add this line to your application's Gemfile:
|
15
|
+
|
16
|
+
gem 'enceladus'
|
17
|
+
|
18
|
+
And then execute:
|
19
|
+
|
20
|
+
$ bundle
|
21
|
+
|
22
|
+
Or install it yourself as:
|
23
|
+
|
24
|
+
$ gem install enceladus
|
25
|
+
|
26
|
+
## Usage
|
27
|
+
|
28
|
+
Have a look at some [usage examples](https://github.com/osiro/enceladus/wiki/Usage) and check out the [docs](http://www.rubydoc.info/github/osiro/enceladus/master/frames) too.
|
29
|
+
|
30
|
+
## Contributing
|
31
|
+
|
32
|
+
1. Fork it ( https://github.com/osiro/enceladus/fork )
|
33
|
+
2. Create your feature branch (`git checkout -b my-new-feature`)
|
34
|
+
3. Commit your changes (`git commit -am 'Add some feature'`)
|
35
|
+
4. Push to the branch (`git push origin my-new-feature`)
|
36
|
+
5. Create a new Pull Request
|
data/Rakefile
ADDED
data/enceladus.gemspec
ADDED
@@ -0,0 +1,29 @@
|
|
1
|
+
# coding: utf-8
|
2
|
+
lib = File.expand_path('../lib', __FILE__)
|
3
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
4
|
+
require 'enceladus/version'
|
5
|
+
|
6
|
+
Gem::Specification.new do |spec|
|
7
|
+
spec.name = "enceladus"
|
8
|
+
spec.version = Enceladus::VERSION
|
9
|
+
spec.authors = ["Vinicius Osiro"]
|
10
|
+
spec.email = ["vinicius.osiro@gmail.com"]
|
11
|
+
spec.summary = "Ruby wrapper for the The Movie Database API v3"
|
12
|
+
spec.description = "Ruby wrapper for the The Movie Database API for all endpoints of v3"
|
13
|
+
spec.homepage = "https://github.com/osiro/enceladus"
|
14
|
+
spec.license = "MIT"
|
15
|
+
|
16
|
+
spec.files = `git ls-files -z`.split("\x0")
|
17
|
+
spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
|
18
|
+
spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
|
19
|
+
spec.require_paths = ["lib"]
|
20
|
+
|
21
|
+
|
22
|
+
spec.add_dependency "rest-client", "~> 1.7.2", '>= 1.7.2'
|
23
|
+
spec.add_development_dependency "bundler", "~> 1.6"
|
24
|
+
spec.add_development_dependency "rake", "~> 10.1 "
|
25
|
+
spec.add_development_dependency "byebug", "~> 3.0"
|
26
|
+
spec.add_development_dependency 'rspec', '~> 3.0', '>= 3.0.0'
|
27
|
+
spec.add_development_dependency 'webmock', '~> 1.18', '>= 1.18.0'
|
28
|
+
spec.add_development_dependency 'coveralls', '~> 0.7.1', '>= 0.7.0'
|
29
|
+
end
|
@@ -0,0 +1,60 @@
|
|
1
|
+
require 'singleton'
|
2
|
+
require 'uri'
|
3
|
+
|
4
|
+
module Enceladus::Configuration
|
5
|
+
class Api
|
6
|
+
include Singleton
|
7
|
+
|
8
|
+
attr_reader :base_url, :version, :api_key
|
9
|
+
attr_accessor :include_adult
|
10
|
+
|
11
|
+
def initialize #:nodoc:#
|
12
|
+
self.base_url = "https://api.themoviedb.org".freeze
|
13
|
+
self.version = "3".freeze
|
14
|
+
self.include_adult = false
|
15
|
+
end
|
16
|
+
|
17
|
+
# Fetches the TMDb account configuration.
|
18
|
+
# This method hits the following TMDb endpoints:
|
19
|
+
# - https://api.themoviedb.org/3/configuration
|
20
|
+
#
|
21
|
+
# Once the request has succeeded, Enceladus will populate the following Enceladus::Configuration::Image attributes:
|
22
|
+
# base_url, secure_base_url, backdrop_sizes, logo_sizes, poster_sizes, profile_sizes and still_sizes.
|
23
|
+
#
|
24
|
+
# A failing request will reset/nullify all those mentioned attributes.
|
25
|
+
#
|
26
|
+
# Return a boolean indicating whether the request has succeeded or not.
|
27
|
+
#
|
28
|
+
# Examples:
|
29
|
+
#
|
30
|
+
# Enceladus::Configuration::Api.instance.connect("cceebf51cb1f8d707d10a132d9544b76")
|
31
|
+
#
|
32
|
+
def connect(api_key)
|
33
|
+
begin
|
34
|
+
self.api_key = api_key
|
35
|
+
Enceladus::Configuration::Image.instance.setup!
|
36
|
+
true
|
37
|
+
rescue Enceladus::Exception::Api
|
38
|
+
self.api_key = nil
|
39
|
+
Enceladus::Configuration::Image.instance.reset!
|
40
|
+
false
|
41
|
+
end
|
42
|
+
end
|
43
|
+
|
44
|
+
# Returns a string with a URL for one of TMDb API endpoints.
|
45
|
+
# Examples:
|
46
|
+
#
|
47
|
+
# Enceladus::Configuration::Api.instance.url_for("movies", { term: "Lola Benvenutti" })
|
48
|
+
# => https://api.themoviedb.org/3/movies?term=Lola+Benvenutti&api_key=token
|
49
|
+
#
|
50
|
+
def url_for(action, params={})
|
51
|
+
params[:api_key] = api_key
|
52
|
+
url = URI.join(base_url, "/#{version}/", action)
|
53
|
+
url.query = URI.encode_www_form(params)
|
54
|
+
url.to_s
|
55
|
+
end
|
56
|
+
|
57
|
+
private
|
58
|
+
attr_writer :base_url, :version, :api_key
|
59
|
+
end
|
60
|
+
end
|
@@ -0,0 +1,83 @@
|
|
1
|
+
require 'singleton'
|
2
|
+
|
3
|
+
module Enceladus::Configuration
|
4
|
+
class Image
|
5
|
+
include Singleton
|
6
|
+
|
7
|
+
attr_reader :base_url, :secure_base_url, :backdrop_sizes, :logo_sizes, :poster_sizes, :profile_sizes, :still_sizes
|
8
|
+
attr_accessor :include_image_language
|
9
|
+
|
10
|
+
def initialize #:nodoc:#
|
11
|
+
reset!
|
12
|
+
end
|
13
|
+
|
14
|
+
# Fetches the TMDb account's configuration data.
|
15
|
+
# This method hits the following TMDb endpoints:
|
16
|
+
# - https://api.themoviedb.org/3/configuration
|
17
|
+
#
|
18
|
+
def setup!
|
19
|
+
configuration = Enceladus::Requester.get("configuration").images
|
20
|
+
self.base_url = configuration.base_url.freeze
|
21
|
+
self.secure_base_url = configuration.secure_base_url.freeze
|
22
|
+
self.backdrop_sizes = configuration.backdrop_sizes.map(&:freeze).freeze
|
23
|
+
self.logo_sizes = configuration.logo_sizes.map(&:freeze).freeze
|
24
|
+
self.poster_sizes = configuration.poster_sizes.map(&:freeze).freeze
|
25
|
+
self.profile_sizes = configuration.profile_sizes.map(&:freeze).freeze
|
26
|
+
self.still_sizes = configuration.still_sizes.map(&:freeze).freeze
|
27
|
+
self
|
28
|
+
end
|
29
|
+
|
30
|
+
# Reset or nullify all image configurations.
|
31
|
+
#
|
32
|
+
def reset!
|
33
|
+
self.base_url = nil
|
34
|
+
self.secure_base_url = nil
|
35
|
+
self.backdrop_sizes = []
|
36
|
+
self.logo_sizes = []
|
37
|
+
self.logo_sizes = []
|
38
|
+
self.poster_sizes = []
|
39
|
+
self.profile_sizes = []
|
40
|
+
self.still_sizes = []
|
41
|
+
self
|
42
|
+
end
|
43
|
+
|
44
|
+
# Returns an array of strings containing URL's for movie/person/tv assets.
|
45
|
+
# Be aware to setup the configuration beforehand, by running:
|
46
|
+
#
|
47
|
+
# Enceladus::Configuration::Image.instance.setup!
|
48
|
+
#
|
49
|
+
# The following arguments are necessary:
|
50
|
+
# - type: the type of asset you're providing, must be one of "backdrop", "logo", "poster" or "profile".
|
51
|
+
# - image_path: /gWyevEz9NT5tON6kdhxsmfvdJ40.jpg
|
52
|
+
#
|
53
|
+
# Examples:
|
54
|
+
#
|
55
|
+
# Enceladus::Configuration::Image.instance.url_for("backdrop", )
|
56
|
+
# => ["http://image.tmdb.org/t/p/w300/gWyevEz9NT5tON6kdhxsmfvdJ40.jpg", "http://image.tmdb.org/t/p/w780/gWyevEz9NT5tON6kdhxsmfvdJ40.jpg", "http://image.tmdb.org/t/p/w1280/gWyevEz9NT5tON6kdhxsmfvdJ40.jpg", "http://image.tmdb.org/t/p/original/gWyevEz9NT5tON6kdhxsmfvdJ40.jpg"]
|
57
|
+
#
|
58
|
+
def url_for(type, image_path)
|
59
|
+
types = ["backdrop", "logo", "poster", "profile"]
|
60
|
+
raise ArgumentError.new("type must be one of #{types}") unless types.include?(type)
|
61
|
+
return [] if image_path.nil? || image_path.empty? && valid?
|
62
|
+
|
63
|
+
send("#{type}_sizes").map do |size|
|
64
|
+
"#{base_url}#{size}#{image_path}"
|
65
|
+
end
|
66
|
+
end
|
67
|
+
|
68
|
+
# Returns a boolean indicating whether the Image configurations are valid or not.
|
69
|
+
#
|
70
|
+
def valid?
|
71
|
+
!base_url.nil? &&
|
72
|
+
!secure_base_url.nil? &&
|
73
|
+
backdrop_sizes.any? &&
|
74
|
+
logo_sizes.any? &&
|
75
|
+
poster_sizes.any? &&
|
76
|
+
profile_sizes.any? &&
|
77
|
+
still_sizes.any?
|
78
|
+
end
|
79
|
+
|
80
|
+
private
|
81
|
+
attr_writer :base_url, :secure_base_url, :backdrop_sizes, :logo_sizes, :poster_sizes, :profile_sizes, :still_sizes
|
82
|
+
end
|
83
|
+
end
|
@@ -0,0 +1,143 @@
|
|
1
|
+
class Enceladus::Account < Enceladus::ApiResource
|
2
|
+
|
3
|
+
RESOURCE_ATTRIBUTES = [:id, :include_adult, :iso_3166_1, :iso_639_1, :name, :username, :session_id].map(&:freeze).freeze
|
4
|
+
attr_accessor *RESOURCE_ATTRIBUTES
|
5
|
+
|
6
|
+
# Responsible for authenticating TMDb users.
|
7
|
+
# Authentication of users follows the workflow: https://www.themoviedb.org/documentation/api/sessions
|
8
|
+
# This method hits the following api endpoints:
|
9
|
+
#
|
10
|
+
# - http://api.themoviedb.org/3/authentication/token/new
|
11
|
+
# - http://api.themoviedb.org/3/authentication/token/validate_with_login
|
12
|
+
# - http://api.themoviedb.org/3/authentication/session/new
|
13
|
+
# - http://api.themoviedb.org/3/account
|
14
|
+
#
|
15
|
+
# Example:
|
16
|
+
#
|
17
|
+
# Enceladus::Account.new("bruna_ferraz", "dajhhd")
|
18
|
+
#
|
19
|
+
def initialize(username, password)
|
20
|
+
self.username = username
|
21
|
+
self.password = password
|
22
|
+
start_authentication_workflow
|
23
|
+
end
|
24
|
+
|
25
|
+
# Adds movie to accounts favorite list.
|
26
|
+
# Example:
|
27
|
+
#
|
28
|
+
# account = Enceladus::Account.new("bruna_ferraz", "dajhhd")
|
29
|
+
# account.favorite_movie!(23444)
|
30
|
+
#
|
31
|
+
def favorite_movie!(movie_id)
|
32
|
+
toggle_favorite_movie(movie_id, true)
|
33
|
+
end
|
34
|
+
|
35
|
+
# Removes movie to accounts favorite list.
|
36
|
+
# Example:
|
37
|
+
#
|
38
|
+
# account = Enceladus::Account.new("bruna_ferraz", "dajhhd")
|
39
|
+
# account.unfavorite_movie!(23444)
|
40
|
+
#
|
41
|
+
def unfavorite_movie!(movie_id)
|
42
|
+
toggle_favorite_movie(movie_id, false)
|
43
|
+
end
|
44
|
+
|
45
|
+
# Adds movie to accounts watchlist.
|
46
|
+
# Example:
|
47
|
+
#
|
48
|
+
# account = Enceladus::Account.new("bruna_ferraz", "dajhhd")
|
49
|
+
# account.add_to_watchlist!(23444)
|
50
|
+
#
|
51
|
+
def add_to_watchlist!(movie_id)
|
52
|
+
toggle_movie_watchlist(movie_id, true)
|
53
|
+
end
|
54
|
+
|
55
|
+
# Removes movie to accounts watchlist.
|
56
|
+
# Example:
|
57
|
+
#
|
58
|
+
# account = Enceladus::Account.new("bruna_ferraz", "dajhhd")
|
59
|
+
# account.remove_from_watchlist!(23444)
|
60
|
+
#
|
61
|
+
def remove_from_watchlist!(movie_id)
|
62
|
+
toggle_movie_watchlist(movie_id, false)
|
63
|
+
end
|
64
|
+
|
65
|
+
# Return a list of account's favorite movies.
|
66
|
+
# The returned movies are wrapped into a Enceladus::MovieCollection.
|
67
|
+
# Example:
|
68
|
+
#
|
69
|
+
# account = Enceladus::Account.new("belinha", "dajhhd")
|
70
|
+
# account.favorite_movies
|
71
|
+
#
|
72
|
+
def favorite_movies
|
73
|
+
Enceladus::MovieCollection.new("account/#{id}/favorite/movies", { session_id: session_id }) if authenticated?
|
74
|
+
end
|
75
|
+
|
76
|
+
# Return a list of account's rated movies.
|
77
|
+
# The returned movies are wrapped into a Enceladus::MovieCollection.
|
78
|
+
# Example:
|
79
|
+
#
|
80
|
+
# account = Enceladus::Account.new("belinha", "dajhhd")
|
81
|
+
# account.rated_movies
|
82
|
+
#
|
83
|
+
def rated_movies
|
84
|
+
Enceladus::MovieCollection.new("account/#{id}/rated/movies", { session_id: session_id }) if authenticated?
|
85
|
+
end
|
86
|
+
|
87
|
+
# Return the account's watchlist
|
88
|
+
# The returned movies are wrapped into a Enceladus::MovieCollection.
|
89
|
+
# Example:
|
90
|
+
#
|
91
|
+
# account = Enceladus::Account.new("belinha", "hjgsss")
|
92
|
+
# account.watchlist
|
93
|
+
#
|
94
|
+
def watchlist
|
95
|
+
Enceladus::MovieCollection.new("account/#{id}/watchlist/movies", { session_id: session_id }) if authenticated?
|
96
|
+
end
|
97
|
+
|
98
|
+
private
|
99
|
+
attr_accessor :request_token, :password
|
100
|
+
|
101
|
+
def toggle_favorite_movie(movie_id, favorite)
|
102
|
+
if authenticated?
|
103
|
+
params = { session_id: session_id }
|
104
|
+
form_data = { media_type: "movie", media_id: movie_id, favorite: favorite }
|
105
|
+
Enceladus::Requester.post("account/#{id}/favorite", params, form_data)
|
106
|
+
end
|
107
|
+
end
|
108
|
+
|
109
|
+
def toggle_movie_watchlist(movie_id, watchlist)
|
110
|
+
if authenticated?
|
111
|
+
params = { session_id: session_id }
|
112
|
+
form_data = { media_type: "movie", media_id: movie_id, watchlist: watchlist }
|
113
|
+
Enceladus::Requester.post("account/#{id}/watchlist", params, form_data)
|
114
|
+
end
|
115
|
+
end
|
116
|
+
|
117
|
+
def authenticated?
|
118
|
+
id && session_id
|
119
|
+
end
|
120
|
+
|
121
|
+
def start_authentication_workflow
|
122
|
+
request_authentication_token
|
123
|
+
authenticate
|
124
|
+
open_session
|
125
|
+
rebuild_single_resource(Enceladus::Requester.get("account", { session_id: session_id }))
|
126
|
+
self.password = self.request_token = nil
|
127
|
+
end
|
128
|
+
|
129
|
+
def request_authentication_token
|
130
|
+
token_info = Enceladus::Requester.get("authentication/token/new")
|
131
|
+
self.request_token = token_info.request_token
|
132
|
+
end
|
133
|
+
|
134
|
+
def authenticate
|
135
|
+
params = { request_token: request_token, username: username, password: password }
|
136
|
+
Enceladus::Requester.get("authentication/token/validate_with_login", params).success
|
137
|
+
end
|
138
|
+
|
139
|
+
def open_session
|
140
|
+
params = { request_token: request_token }
|
141
|
+
self.session_id = Enceladus::Requester.get("authentication/session/new", params).session_id
|
142
|
+
end
|
143
|
+
end
|
@@ -0,0 +1,76 @@
|
|
1
|
+
class Enceladus::ApiPaginatedCollection
|
2
|
+
# Provides the implementation to handle collection of paginated resources.
|
3
|
+
# The collection of resources are cached locally, after going to next pages the previous pages do not make
|
4
|
+
# any request to obtain the resources again.
|
5
|
+
|
6
|
+
RESOURCE_CLASS = nil
|
7
|
+
|
8
|
+
attr_reader :total_pages, :total_results, :path, :params, :results_per_page
|
9
|
+
|
10
|
+
# The argument path refers to the TMDb api path that provides paginated resources.
|
11
|
+
# the argument params can be used when you need to send arguments to requests when fetching new pages.
|
12
|
+
# Example:
|
13
|
+
# Enceladus::MovieCollection.new("movie/upcoming", { blah: true })
|
14
|
+
def initialize(path, params={})
|
15
|
+
self.path = path
|
16
|
+
self.params = params
|
17
|
+
self.params[:page] = 1
|
18
|
+
self.results_per_page = []
|
19
|
+
get_results_per_page
|
20
|
+
end
|
21
|
+
|
22
|
+
# Returns a collection of resources for the current page
|
23
|
+
def all
|
24
|
+
get_results_per_page
|
25
|
+
end
|
26
|
+
|
27
|
+
# Request, fetch, cache and return the collection of resources for the next page.
|
28
|
+
def next_page
|
29
|
+
self.params[:page] += 1
|
30
|
+
get_results_per_page
|
31
|
+
end
|
32
|
+
|
33
|
+
# Request, fetch, cache and return the collection of resources for the previous page.
|
34
|
+
def previous_page
|
35
|
+
raise ArgumentError.new("current_page must be greater than 0") if self.params[:page] == 1
|
36
|
+
self.params[:page] -= 1
|
37
|
+
get_results_per_page
|
38
|
+
end
|
39
|
+
|
40
|
+
# Returns current page number.
|
41
|
+
def current_page
|
42
|
+
self.params[:page]
|
43
|
+
end
|
44
|
+
|
45
|
+
# Request, fetch, cache and return the collection of resources for a given page number.
|
46
|
+
def current_page=(page)
|
47
|
+
self.params[:page] = page
|
48
|
+
get_results_per_page
|
49
|
+
end
|
50
|
+
|
51
|
+
# Returns the first resource for of the current page.
|
52
|
+
def first
|
53
|
+
get_results_per_page.first
|
54
|
+
end
|
55
|
+
|
56
|
+
# Returns the last resource for of the current page.
|
57
|
+
def last
|
58
|
+
get_results_per_page.last
|
59
|
+
end
|
60
|
+
|
61
|
+
private
|
62
|
+
attr_writer :total_pages, :total_results, :path, :params, :results_per_page
|
63
|
+
|
64
|
+
def fetch_colletion
|
65
|
+
raise NotImplementedError.new("RESOURCE_CLASS must be defined") if self.class::RESOURCE_CLASS.nil?
|
66
|
+
|
67
|
+
response = Enceladus::Requester.get(path, params)
|
68
|
+
self.total_pages = response.total_pages
|
69
|
+
self.total_results = response.total_results
|
70
|
+
self.class::RESOURCE_CLASS.build_collection(response.results)
|
71
|
+
end
|
72
|
+
|
73
|
+
def get_results_per_page
|
74
|
+
self.results_per_page[current_page - 1] ||= fetch_colletion
|
75
|
+
end
|
76
|
+
end
|
@@ -0,0 +1,25 @@
|
|
1
|
+
class Enceladus::ApiResource
|
2
|
+
private
|
3
|
+
def rebuild_single_resource(resource_from_response)
|
4
|
+
self.class::RESOURCE_ATTRIBUTES.each do |resource_attr|
|
5
|
+
self.public_send("#{resource_attr}=", resource_from_response.public_send(resource_attr)) if resource_from_response.respond_to?(resource_attr)
|
6
|
+
end
|
7
|
+
self
|
8
|
+
end
|
9
|
+
|
10
|
+
def self.build_single_resource(resource_from_response)
|
11
|
+
resource = self.new
|
12
|
+
self::RESOURCE_ATTRIBUTES.each do |resource_attr|
|
13
|
+
resource.public_send("#{resource_attr}=", resource_from_response.public_send(resource_attr)) if resource_from_response.respond_to?(resource_attr)
|
14
|
+
end
|
15
|
+
resource
|
16
|
+
end
|
17
|
+
|
18
|
+
def self.build_collection(resources_from_response)
|
19
|
+
resources = []
|
20
|
+
resources_from_response.each do |resource_from_response|
|
21
|
+
resources << self.build_single_resource(resource_from_response)
|
22
|
+
end
|
23
|
+
resources
|
24
|
+
end
|
25
|
+
end
|
@@ -0,0 +1,9 @@
|
|
1
|
+
class Enceladus::Cast < Enceladus::ApiResource
|
2
|
+
RESOURCE_ATTRIBUTES = [:cast_id, :character, :credit_id, :id, :name, :order, :profile_path].map(&:freeze).freeze
|
3
|
+
attr_accessor *RESOURCE_ATTRIBUTES
|
4
|
+
|
5
|
+
# Returns an array containing URL's (as strings) for cast images.
|
6
|
+
def profile_urls
|
7
|
+
Enceladus::Configuration::Image.instance.url_for("profile", profile_path)
|
8
|
+
end
|
9
|
+
end
|
@@ -0,0 +1,15 @@
|
|
1
|
+
class Enceladus::GuestAccount
|
2
|
+
attr_reader :session_id
|
3
|
+
|
4
|
+
# Initiaises a guest account.
|
5
|
+
# This method hits the following api endpoint:
|
6
|
+
# - https://api.themoviedb.org/3/authentication/guest_session/new
|
7
|
+
#
|
8
|
+
# For more information about guest accounts, check out http://docs.themoviedb.apiary.io/ SECTION: Authentication
|
9
|
+
def initialize
|
10
|
+
self.session_id = Enceladus::Requester.get("authentication/guest_session/new").guest_session_id
|
11
|
+
end
|
12
|
+
|
13
|
+
private
|
14
|
+
attr_writer :session_id
|
15
|
+
end
|