enceladus 0.0.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (53) hide show
  1. checksums.yaml +7 -0
  2. data/.gitignore +23 -0
  3. data/.rspec +1 -0
  4. data/.ruby-gemset +1 -0
  5. data/.ruby-version +1 -0
  6. data/.travis.yml +4 -0
  7. data/Gemfile +4 -0
  8. data/LICENSE.txt +22 -0
  9. data/README.md +36 -0
  10. data/Rakefile +2 -0
  11. data/enceladus.gemspec +29 -0
  12. data/lib/enceladus/configuration/api.rb +60 -0
  13. data/lib/enceladus/configuration/image.rb +83 -0
  14. data/lib/enceladus/exceptions.rb +5 -0
  15. data/lib/enceladus/models/account.rb +143 -0
  16. data/lib/enceladus/models/api_paginated_collection.rb +76 -0
  17. data/lib/enceladus/models/api_resource.rb +25 -0
  18. data/lib/enceladus/models/cast.rb +9 -0
  19. data/lib/enceladus/models/genre.rb +4 -0
  20. data/lib/enceladus/models/guest_account.rb +15 -0
  21. data/lib/enceladus/models/movie.rb +162 -0
  22. data/lib/enceladus/models/movie_collection.rb +3 -0
  23. data/lib/enceladus/models/production_company.rb +4 -0
  24. data/lib/enceladus/models/production_country.rb +4 -0
  25. data/lib/enceladus/models/release.rb +4 -0
  26. data/lib/enceladus/models/spoken_language.rb +4 -0
  27. data/lib/enceladus/models/you_tube_trailer.rb +9 -0
  28. data/lib/enceladus/requester.rb +68 -0
  29. data/lib/enceladus/version.rb +3 -0
  30. data/lib/enceladus.rb +43 -0
  31. data/spec/enceladus/configuration/api_spec.rb +59 -0
  32. data/spec/enceladus/configuration/image_spec.rb +200 -0
  33. data/spec/enceladus/enceladus_spec.rb +53 -0
  34. data/spec/enceladus/models/account_spec.rb +176 -0
  35. data/spec/enceladus/models/cast_spec.rb +18 -0
  36. data/spec/enceladus/models/guest_account_spec.rb +19 -0
  37. data/spec/enceladus/models/movie_collection_spec.rb +217 -0
  38. data/spec/enceladus/models/movie_spec.rb +469 -0
  39. data/spec/enceladus/models/you_tube_trailer_spec.rb +16 -0
  40. data/spec/spec_helper.rb +20 -0
  41. data/spec/support/responses/account_response.rb +27 -0
  42. data/spec/support/responses/authentication_response.rb +19 -0
  43. data/spec/support/responses/configuration_response.rb +32 -0
  44. data/spec/support/responses/credits_collection_response.rb +19 -0
  45. data/spec/support/responses/credits_response.rb +29 -0
  46. data/spec/support/responses/error_response.rb +12 -0
  47. data/spec/support/responses/guest_account_response.rb +21 -0
  48. data/spec/support/responses/movie_collection_resource_response.rb +36 -0
  49. data/spec/support/responses/movie_collection_response.rb +20 -0
  50. data/spec/support/responses/movie_response.rb +67 -0
  51. data/spec/support/responses/request_token_response.rb +21 -0
  52. data/spec/support/responses/session_response.rb +19 -0
  53. metadata +239 -0
@@ -0,0 +1,162 @@
1
+ class Enceladus::Movie < Enceladus::ApiResource
2
+ RESOURCE_ATTRIBUTES = [ :adult, :backdrop_path, :id, :original_title, :release_date, :poster_path, :youtube_trailers,
3
+ :popularity, :title, :vote_average, :vote_count, :belongs_to_collection, :budget, :homepage, :imdb_id, :releases,
4
+ :overview, :revenue, :runtime, :status, :tagline, :genres, :production_companies, :production_countries, :spoken_languages,
5
+ :rating, :cast ].map(&:freeze).freeze
6
+
7
+ attr_accessor *RESOURCE_ATTRIBUTES
8
+
9
+ # Find a movie by TMDb ID or imdb_id.
10
+ # Examples:
11
+ # Enceladus::Movie.find(550)
12
+ # Enceladus::Movie.find("tt0137523")
13
+ #
14
+ def self.find(id)
15
+ build_single_resource(Enceladus::Requester.get("movie/#{id}", default_params))
16
+ end
17
+
18
+ # Find movies by title.
19
+ # Example:
20
+ # Enceladus::Movie.find_by_title("emmanuelle")
21
+ #
22
+ def self.find_by_title(title)
23
+ Enceladus::MovieCollection.new("search/movie", { query: title })
24
+ end
25
+
26
+ # Returns a paginated collection of upcoming movies.
27
+ # Resources are paginates and respond to methods such as: next_page, all, previous_page, etc...
28
+ # Examples:
29
+ #
30
+ # collection = Enceladus::Movie.upcoming
31
+ # collection.all
32
+ # => [Movie, Movie, ...]
33
+ # collection.current_page
34
+ # => 1
35
+ # collection.next_page
36
+ # => [Movie, Movie, ...]
37
+ # collection.current_page
38
+ # => 2
39
+ #
40
+ def self.upcoming
41
+ Enceladus::MovieCollection.new("movie/upcoming")
42
+ end
43
+
44
+ # Returns a paginated collection of movies playing in theatres.
45
+ def self.now_playing
46
+ Enceladus::MovieCollection.new("movie/now_playing")
47
+ end
48
+
49
+ # Returns a paginated collection of popular movies.
50
+ def self.popular
51
+ Enceladus::MovieCollection.new("movie/popular")
52
+ end
53
+
54
+ # Returns a paginated collection of top rated movies.
55
+ def self.top_rated
56
+ Enceladus::MovieCollection.new("movie/top_rated")
57
+ end
58
+
59
+ # Given a movie, this method returns a paginated collection of similar movies.
60
+ def similar
61
+ Enceladus::MovieCollection.new("movie/#{id}/similar", Enceladus::Movie.default_params)
62
+ end
63
+
64
+ # Fetchs details of movie information on TMDb API.
65
+ def reload
66
+ rebuild_single_resource(Enceladus::Requester.get("movie/#{id}"))
67
+ end
68
+
69
+ # Rate a movie.
70
+ # The argument account can be Enceladus::Account or Enceladus::GuestAccount.
71
+ # The argument rating must be an numeric value between 0 and 10.
72
+ # Examples:
73
+ # movie = Enceladus::Movie.find(550)
74
+ # guest_account = Enceladus::GuestAccount.new
75
+ # movie.rate!(guest_account, 7.5)
76
+ #
77
+ # account = Enceladus::Account.new("username", "password")
78
+ # movie.rate!(account, 8.3)
79
+ #
80
+ # TMDb expects the rating value to be divisors of 0.5, this method rounds the value properly before making the request.
81
+ #
82
+ def rate!(account, rating)
83
+ params = {}
84
+ if account.kind_of?(Enceladus::Account)
85
+ params[:session_id] = account.session_id
86
+ elsif account.kind_of?(Enceladus::GuestAccount)
87
+ params[:guest_session_id] = account.session_id
88
+ else
89
+ raise Enceladus::Exception::ArgumentError.new("account must be one of Enceladus::Account or Enceladus::GuestAccount")
90
+ end
91
+
92
+ form_data = { value: (rating * 2).round / 2.0 }
93
+ Enceladus::Requester.post("movie/#{id}/rating", params, form_data)
94
+ end
95
+
96
+ # Method used by Enceladus::ApiResource to save genres fetched by TMDb API.
97
+ def genres=(genres_from_response)
98
+ @genres = Enceladus::Genre.build_collection(genres_from_response)
99
+ end
100
+
101
+ # Method used by Enceladus::ApiResource to save production companies fetched by TMDb API.
102
+ def production_companies=(production_companies_from_response)
103
+ @production_companies = Enceladus::ProductionCompany.build_collection(production_companies_from_response)
104
+ end
105
+
106
+ # Method used by Enceladus::ApiResource to save production countries fetched by TMDb API.
107
+ def production_countries=(production_countries_from_response)
108
+ @production_countries = Enceladus::ProductionCountry.build_collection(production_countries_from_response)
109
+ end
110
+
111
+ # Method used by Enceladus::ApiResource to save spoken languages fetched by TMDb API.
112
+ def spoken_languages=(spoken_languages_from_response)
113
+ @spoken_languages = Enceladus::SpokenLanguage.build_collection(spoken_languages_from_response)
114
+ end
115
+
116
+ # Method used by Enceladus::ApiResource to save releases fetched by TMDb API.
117
+ def releases=(releases_from_response)
118
+ if !releases_from_response.nil? && !releases_from_response.countries?
119
+ @releases = Enceladus::Release.build_collection(releases_from_response.countries)
120
+ end
121
+ end
122
+
123
+ # Method used by Enceladus::ApiResource to save trailers fetched by TMDb API.
124
+ def youtube_trailers=(trailers_from_response)
125
+ if !trailers_from_response.nil? && !trailers_from_response.youtube.nil?
126
+ @youtube_trailers = Enceladus::YouTubeTrailer.build_collection(trailers_from_response.youtube)
127
+ end
128
+ end
129
+
130
+ # Returns an array of Cast for the movie.
131
+ def cast
132
+ @cast ||= Enceladus::Cast.build_collection(Enceladus::Requester.get("movie/#{id}/credits").cast)
133
+ end
134
+
135
+ # Returns an array containing URL's (as string) for the movie background picture.
136
+ def backdrop_urls
137
+ Enceladus::Configuration::Image.instance.url_for("backdrop", backdrop_path)
138
+ end
139
+
140
+ # Returns an array containing URL's (as string) for the movie poster picture.
141
+ def poster_urls
142
+ Enceladus::Configuration::Image.instance.url_for("poster", poster_path)
143
+ end
144
+
145
+ private
146
+ def self.build_single_resource(resource_from_response)
147
+ super(resource_from_response).tap do |resource|
148
+ resource.youtube_trailers=(resource_from_response.trailers)
149
+ end
150
+ end
151
+
152
+ def rebuild_single_resource(resource_from_response)
153
+ super(resource_from_response).tap do |resource|
154
+ self.youtube_trailers=(resource_from_response.trailers)
155
+ end
156
+ end
157
+
158
+ def self.default_params
159
+ include_image_language = Enceladus::Configuration::Image.instance.include_image_language
160
+ { append_to_response: "releases,trailers", include_image_language: include_image_language }
161
+ end
162
+ end
@@ -0,0 +1,3 @@
1
+ class Enceladus::MovieCollection < Enceladus::ApiPaginatedCollection
2
+ RESOURCE_CLASS = Enceladus::Movie
3
+ end
@@ -0,0 +1,4 @@
1
+ class Enceladus::ProductionCompany < Enceladus::ApiResource
2
+ RESOURCE_ATTRIBUTES = [:id, :name].map(&:freeze).freeze
3
+ attr_accessor *RESOURCE_ATTRIBUTES
4
+ end
@@ -0,0 +1,4 @@
1
+ class Enceladus::ProductionCountry < Enceladus::ApiResource
2
+ RESOURCE_ATTRIBUTES = [:iso_3166_1, :name].map(&:freeze).freeze
3
+ attr_accessor *RESOURCE_ATTRIBUTES
4
+ end
@@ -0,0 +1,4 @@
1
+ class Enceladus::Release < Enceladus::ApiResource
2
+ RESOURCE_ATTRIBUTES = [:iso_3166_1, :certification, :release_date].map(&:freeze).freeze
3
+ attr_accessor *RESOURCE_ATTRIBUTES
4
+ end
@@ -0,0 +1,4 @@
1
+ class Enceladus::SpokenLanguage < Enceladus::ApiResource
2
+ RESOURCE_ATTRIBUTES = [:iso_639_1, :name].map(&:freeze).freeze
3
+ attr_accessor *RESOURCE_ATTRIBUTES
4
+ end
@@ -0,0 +1,9 @@
1
+ class Enceladus::YouTubeTrailer < Enceladus::ApiResource
2
+ RESOURCE_ATTRIBUTES = [:name, :size, :source, :type].map(&:freeze).freeze
3
+ attr_accessor *RESOURCE_ATTRIBUTES
4
+
5
+ # Returns a YouTube link to the movie trailer.
6
+ def link
7
+ URI("https://www.youtube.com/watch?v=#{source}")
8
+ end
9
+ end
@@ -0,0 +1,68 @@
1
+ require 'rest_client'
2
+ require 'ostruct'
3
+
4
+ class Enceladus::Requester
5
+ class << self
6
+ # Makes a get request to one of the TMDb API endpoints.
7
+ # Example:
8
+ #
9
+ # Enceladus::Requester.get("account", { session_id: "43462867" })
10
+ #
11
+ # Performing this action might results in RestClient::Exception.
12
+ # Check out https://github.com/rest-client/rest-client#exceptions-see-wwww3orgprotocolsrfc2616rfc2616-sec10html for more details.
13
+ #
14
+ def get(action, params={})
15
+ url = api.url_for(action, params)
16
+ perform_request do
17
+ JSON.parse(RestClient.get(url, request_headers), object_class: OpenStruct)
18
+ end
19
+ end
20
+
21
+ # Makes a post request to TMDb API endpoints.
22
+ # Example:
23
+ #
24
+ # params = { session_id: "77678687" }
25
+ # form_data = { media_type: "movie", media_id: 31231, watchlist: true }
26
+ # Enceladus::Requester.post("account/777/watchlist", params, form_data)
27
+ #
28
+ # Performing this action might results in RestClient::Exception.
29
+ # Check out https://github.com/rest-client/rest-client#exceptions-see-wwww3orgprotocolsrfc2616rfc2616-sec10html for more details.
30
+ #
31
+ def post(action, params={}, form_data={})
32
+ url = api.url_for(action, params)
33
+ perform_request do
34
+ JSON.parse(RestClient.post(url, form_data.to_json, request_headers), object_class: OpenStruct)
35
+ end
36
+ end
37
+
38
+ private
39
+ def api
40
+ Enceladus::Configuration::Api.instance
41
+ end
42
+
43
+ # Handles request calls exceptions.
44
+ # Performing RestClient actions might results in RestClient::Exception.
45
+ # Check out https://github.com/rest-client/rest-client#exceptions-see-wwww3orgprotocolsrfc2616rfc2616-sec10html for more details.
46
+ #
47
+ # So, this method is responsible for handling RestClient::Exception, parsing the error message to finally raise Enceladus::Exception::Api.
48
+ #
49
+ def perform_request(&block)
50
+ begin
51
+ block.call
52
+ rescue RestClient::Exception => e
53
+ message = ["The Movie DB API Exception:"]
54
+ message << "@message=\"#{e.message}\""
55
+
56
+ JSON.parse(e.response).each do |k,v|
57
+ message << "@#{k}=\"#{v}\""
58
+ end
59
+
60
+ raise Enceladus::Exception::Api.new(message.join(" "))
61
+ end
62
+ end
63
+
64
+ def request_headers
65
+ { accept: 'application/json', content_type: 'application/json' }
66
+ end
67
+ end
68
+ end
@@ -0,0 +1,3 @@
1
+ module Enceladus
2
+ VERSION = "0.0.2"
3
+ end
data/lib/enceladus.rb ADDED
@@ -0,0 +1,43 @@
1
+ require File.dirname(__FILE__) + "/enceladus/version"
2
+ require File.dirname(__FILE__) + "/enceladus/exceptions"
3
+ require File.dirname(__FILE__) + "/enceladus/requester"
4
+ require File.dirname(__FILE__) + "/enceladus/configuration/api"
5
+ require File.dirname(__FILE__) + "/enceladus/configuration/image"
6
+ require File.dirname(__FILE__) + "/enceladus/models/api_resource"
7
+ require File.dirname(__FILE__) + "/enceladus/models/api_paginated_collection"
8
+ require File.dirname(__FILE__) + "/enceladus/models/account"
9
+ require File.dirname(__FILE__) + "/enceladus/models/guest_account"
10
+ require File.dirname(__FILE__) + "/enceladus/models/genre"
11
+ require File.dirname(__FILE__) + "/enceladus/models/movie"
12
+ require File.dirname(__FILE__) + "/enceladus/models/cast"
13
+ require File.dirname(__FILE__) + "/enceladus/models/movie_collection"
14
+ require File.dirname(__FILE__) + "/enceladus/models/production_company"
15
+ require File.dirname(__FILE__) + "/enceladus/models/production_country"
16
+ require File.dirname(__FILE__) + "/enceladus/models/release"
17
+ require File.dirname(__FILE__) + "/enceladus/models/spoken_language"
18
+ require File.dirname(__FILE__) + "/enceladus/models/you_tube_trailer"
19
+
20
+ module Enceladus
21
+
22
+ # Responsible for authenticating Enceladus and fetching the account configuration.
23
+ # This method hits the following TMDb endpoints:
24
+ # - https://api.themoviedb.org/3/configuration
25
+ #
26
+ # You can also provide the following optional arguments:
27
+ # - include_image_language: find backdrops and posters in a specific language (check out http://docs.themoviedb.apiary.io session: Image Languages)
28
+ # - include_adult: includes adult movies in searchers
29
+ #
30
+ # Examples:
31
+ #
32
+ # Enceladus.connect("0f76454c7b22300e457800cc20f24ae9")
33
+ # Enceladus.connect("0f76454c7b22300e457800cc20f24ae9", { include_image_language: "pt-BR" })
34
+ #
35
+ def self.connect(api_key, options={ include_image_language: "en", include_adult: false })
36
+ Enceladus::Configuration::Api.instance.tap do |api|
37
+ api.connect(api_key)
38
+ api.include_adult = options[:include_adult]
39
+ end
40
+
41
+ Enceladus::Configuration::Image.instance.include_image_language = options[:include_image_language]
42
+ end
43
+ end
@@ -0,0 +1,59 @@
1
+ require 'spec_helper'
2
+
3
+ describe Enceladus::Configuration::Api do
4
+ describe "#connect" do
5
+ subject { Enceladus::Configuration::Api.instance.connect(api_key) }
6
+ let(:api_key) { "token" }
7
+
8
+ context "when the provided api_key is not valid" do
9
+ before do
10
+ stub_request(:get, /api.themoviedb.org\/3\/configuration/).
11
+ to_return(status: 401, body: ErrorResponse.new(7, "Invalid API key: You must be granted a valid key.").to_json)
12
+ end
13
+
14
+ it "should set Enceladus::Configuration::Api api_key as nil" do
15
+ subject
16
+ expect(Enceladus::Configuration::Api.instance.api_key).to be_nil
17
+ end
18
+
19
+ it "should call Enceladus::Configuration::Image.instance.reset!" do
20
+ expect(Enceladus::Configuration::Image.instance).to receive(:reset!)
21
+ subject
22
+ end
23
+
24
+ it "should return false" do
25
+ should eq(false)
26
+ end
27
+ end
28
+
29
+ context "when the provided api_key is valid" do
30
+ before do
31
+ stub_request(:get, /api.themoviedb.org\/3\/configuration/).
32
+ to_return(status: 200, body: ConfigurationResponse.new.to_json)
33
+ end
34
+
35
+ it "should save Enceladus::Configuration::Api api_key" do
36
+ subject
37
+ expect(Enceladus::Configuration::Api.instance.api_key).to eq(api_key)
38
+ end
39
+
40
+ it "should return true" do
41
+ should eq(true)
42
+ end
43
+ end
44
+ end
45
+
46
+ describe "#url_for" do
47
+ subject { Enceladus::Configuration::Api.instance.url_for(action, params) }
48
+ let(:action) { "movies" }
49
+ let(:params) { { term: "Sasha Grey" } }
50
+
51
+ before do
52
+ expect(Enceladus::Configuration::Api.instance).to receive(:api_key).and_return("token")
53
+ end
54
+
55
+ it "should return the full endpoint URL based on the provided args" do
56
+ should eq "https://api.themoviedb.org/3/movies?term=Sasha+Grey&api_key=token"
57
+ end
58
+ end
59
+ end
@@ -0,0 +1,200 @@
1
+ require 'spec_helper'
2
+
3
+ describe Enceladus::Configuration::Image do
4
+ let(:configuration) { ConfigurationResponse.new }
5
+
6
+ describe "#setup!" do
7
+ subject(:image) { Enceladus::Configuration::Image.instance.setup! }
8
+
9
+ before do
10
+ stub_request(:get, /api.themoviedb.org\/3\/configuration/).
11
+ to_return(status: 200, body: configuration.to_json)
12
+ end
13
+
14
+ it "should save the base_url" do
15
+ expect(image.base_url).to eq(configuration.base_url)
16
+ end
17
+
18
+ it "should save the secure_base_url" do
19
+ expect(image.secure_base_url).to eq(configuration.secure_base_url)
20
+ end
21
+
22
+ it "should save the backdrop_sizes" do
23
+ expect(image.backdrop_sizes).to eq(configuration.backdrop_sizes)
24
+ end
25
+
26
+ it "should save the logo_sizes" do
27
+ expect(image.logo_sizes).to eq(configuration.logo_sizes)
28
+ end
29
+
30
+ it "should save the poster_sizes" do
31
+ expect(image.poster_sizes).to eq(configuration.poster_sizes)
32
+ end
33
+
34
+ it "should save the profile_sizes" do
35
+ expect(image.profile_sizes).to eq(configuration.profile_sizes)
36
+ end
37
+
38
+ it "should save the still_sizes" do
39
+ expect(image.still_sizes).to eq(configuration.still_sizes)
40
+ end
41
+
42
+ it "should return Enceladus::Configuration::Image.instance" do
43
+ is_expected.to eq(Enceladus::Configuration::Image.instance)
44
+ end
45
+ end
46
+
47
+ describe "#reset!" do
48
+ subject(:image) { Enceladus::Configuration::Image.instance.reset! }
49
+
50
+ it "should nullify the base_url" do
51
+ expect(image.base_url).to be_nil
52
+ end
53
+
54
+ it "should nullify the secure_base_url" do
55
+ expect(image.secure_base_url).to be_nil
56
+ end
57
+
58
+ it "should set the backdrop_sizes as an empty array" do
59
+ expect(image.backdrop_sizes).to eq([])
60
+ end
61
+
62
+ it "should set the logo_sizes as an empty array" do
63
+ expect(image.logo_sizes).to eq([])
64
+ end
65
+
66
+ it "should set the poster_sizes as an empty array" do
67
+ expect(image.poster_sizes).to eq([])
68
+ end
69
+
70
+ it "should set the profile_sizes as an empty array" do
71
+ expect(image.profile_sizes).to eq([])
72
+ end
73
+
74
+ it "should set the still_sizes as an empty array" do
75
+ expect(image.still_sizes).to eq([])
76
+ end
77
+
78
+ it "should return Enceladus::Configuration::Image.instance" do
79
+ is_expected.to eq(Enceladus::Configuration::Image.instance)
80
+ end
81
+ end
82
+
83
+ describe "#url_for" do
84
+ subject { Enceladus::Configuration::Image.instance.url_for(type, image_path) }
85
+ let(:image_path) { "/asa_akira.jpeg" }
86
+
87
+ before do
88
+ stub_request(:get, /api.themoviedb.org\/3\/configuration/).to_return(status: 200, body: configuration.to_json)
89
+ Enceladus::Configuration::Image.instance.setup!
90
+ end
91
+
92
+ context "when type is backdrop" do
93
+ let(:type) { "backdrop" }
94
+
95
+ it "should return an array containing all image URL's of backdrops" do
96
+ is_expected.to eq(["http://test.com/w300/asa_akira.jpeg", "http://test.com/w780/asa_akira.jpeg", "http://test.com/w1280/asa_akira.jpeg", "http://test.com/original/asa_akira.jpeg"])
97
+ end
98
+ end
99
+
100
+ context "when type is logo" do
101
+ let(:type) { "logo" }
102
+
103
+ it "should return an array containing all image URL's of logos" do
104
+ is_expected.to eq(["http://test.com/w45/asa_akira.jpeg", "http://test.com/w92/asa_akira.jpeg", "http://test.com/w154/asa_akira.jpeg", "http://test.com/w185/asa_akira.jpeg", "http://test.com/w300/asa_akira.jpeg", "http://test.com/w500/asa_akira.jpeg", "http://test.com/original/asa_akira.jpeg"])
105
+ end
106
+ end
107
+
108
+ context "when type is poster" do
109
+ let(:type) { "poster" }
110
+
111
+ it "should return an array containing all image URL's of logos" do
112
+ is_expected.to eq(["http://test.com/w92/asa_akira.jpeg", "http://test.com/w154/asa_akira.jpeg", "http://test.com/w185/asa_akira.jpeg", "http://test.com/w342/asa_akira.jpeg", "http://test.com/w500/asa_akira.jpeg", "http://test.com/w780/asa_akira.jpeg", "http://test.com/original/asa_akira.jpeg"])
113
+ end
114
+ end
115
+
116
+ context "when type is profile" do
117
+ let(:type) { "profile" }
118
+
119
+ it "should return an array containing all image URL's of profiles" do
120
+ is_expected.to eq(["http://test.com/w45/asa_akira.jpeg", "http://test.com/w185/asa_akira.jpeg", "http://test.com/h632/asa_akira.jpeg", "http://test.com/original/asa_akira.jpeg"])
121
+ end
122
+ end
123
+
124
+ context "when type is invalid" do
125
+ let(:type) { "jenna_jamenson" }
126
+
127
+ it "should raise ArgumentError" do
128
+ expect{ subject }.to raise_error(ArgumentError)
129
+ end
130
+ end
131
+
132
+ context "when image_path is nil" do
133
+ let(:type) { "profile" }
134
+ let(:image_path) { nil }
135
+
136
+ it "should return an empty array" do
137
+ is_expected.to eq([])
138
+ end
139
+ end
140
+
141
+ context "when Enceladus::Configuration::Image is invalid" do
142
+ let(:type) { "profile" }
143
+
144
+ before { Enceladus::Configuration::Image.instance.reset! }
145
+
146
+ it "should return an empty array" do
147
+ is_expected.to eq([])
148
+ end
149
+ end
150
+ end
151
+
152
+ describe "#valid?" do
153
+ subject { Enceladus::Configuration::Image.instance.valid? }
154
+ let(:image) { Enceladus::Configuration::Image.instance }
155
+
156
+ before do
157
+ stub_request(:get, /api.themoviedb.org\/3\/configuration/).to_return(status: 200, body: configuration.to_json)
158
+ image.setup!
159
+ end
160
+
161
+ context "when base_url is nil" do
162
+ before { image.send("base_url=", nil) }
163
+ it { is_expected.to eq(false) }
164
+ end
165
+
166
+ context "when secure_base_url is nil" do
167
+ before { image.send("secure_base_url=", nil) }
168
+ it { is_expected.to eq(false) }
169
+ end
170
+
171
+ context "when backdrop_sizes is empty" do
172
+ before { image.send("backdrop_sizes=", []) }
173
+ it { is_expected.to eq(false) }
174
+ end
175
+
176
+ context "when logo_sizes is empty" do
177
+ before { image.send("logo_sizes=", []) }
178
+ it { is_expected.to eq(false) }
179
+ end
180
+
181
+ context "when poster_sizes is empty" do
182
+ before { image.send("poster_sizes=", []) }
183
+ it { is_expected.to eq(false) }
184
+ end
185
+
186
+ context "when profile_sizes is empty" do
187
+ before { image.send("profile_sizes=", []) }
188
+ it { is_expected.to eq(false) }
189
+ end
190
+
191
+ context "when still_sizes is empty" do
192
+ before { image.send("still_sizes=", []) }
193
+ it { is_expected.to eq(false) }
194
+ end
195
+
196
+ context "when none of the configuration options are nil or empty" do
197
+ it { is_expected.to eq(true) }
198
+ end
199
+ end
200
+ end
@@ -0,0 +1,53 @@
1
+ require 'spec_helper'
2
+
3
+ describe Enceladus do
4
+
5
+ describe ".connect" do
6
+ subject { Enceladus.connect(api_key) }
7
+ let(:api_key) { "token" }
8
+ let(:configuration) { ConfigurationResponse.new }
9
+
10
+ before do
11
+ stub_request(:get, /api.themoviedb.org\/3\/configuration/).to_return(status: 200, body: configuration.to_json)
12
+ end
13
+
14
+ it "should start the configuration workflow" do
15
+ expect(Enceladus::Configuration::Api.instance).to receive(:connect).with(api_key)
16
+ subject
17
+ end
18
+
19
+ it "should set Enceladus::Configuration::Image#include_image_language as English" do
20
+ subject
21
+ expect(Enceladus::Configuration::Image.instance.include_image_language).to eq("en")
22
+ end
23
+
24
+ it "should set Enceladus::Configuration::Api#include_adult as false" do
25
+ subject
26
+ expect(Enceladus::Configuration::Api.instance.include_adult).to eq(false)
27
+ end
28
+
29
+ describe "include_image_language" do
30
+ context "when include_image_language is provided" do
31
+ subject { Enceladus.connect(api_key, { include_image_language: include_image_language } ) }
32
+ let(:include_image_language) { "pt-BR" }
33
+
34
+ it "should set Enceladus::Configuration::Image#include_image_language properly" do
35
+ subject
36
+ expect(Enceladus::Configuration::Image.instance.include_image_language).to eq(include_image_language)
37
+ end
38
+ end
39
+ end
40
+
41
+ describe "include_adult" do
42
+ context "when include_adult is provided" do
43
+ subject { Enceladus.connect(api_key, { include_adult: true }) }
44
+ let(:include_adult) { true }
45
+
46
+ it "should set Enceladus::Configuration::Api#include_adult properly" do
47
+ subject
48
+ expect(Enceladus::Configuration::Api.instance.include_adult).to eq(include_adult)
49
+ end
50
+ end
51
+ end
52
+ end
53
+ end