diff --git a/README.rdoc b/README.rdoc index 671386c..4d8f76a 100644 --- a/README.rdoc +++ b/README.rdoc @@ -22,9 +22,16 @@ Ruby 1.8.x & 1.9.x (2.0.x experimental) # setup your API key Tmdb.api_key = "t478f8de5776c799de5a" + # (optional) setup your base url. This is provided for testing purposes by apiary.io when logged in. + # They provide a request and response log you can view from the web. + Tmdb.base_api_url = "http://private-xxxxx-themoviedb.apiary.io/3" + # setup your default language Tmdb.default_language = "en" + # change your rate limit. Default is 0.34 + Tmdb.rate_limit_time = 0.34 + @movie = TmdbMovie.find(:title => "Iron Man", :limit => 1) # => @@ -52,6 +59,29 @@ Parameters: You must supply at least one of :id, :title, or :imdb. All other parameters are optional. +==== TmdbMovie.set_rating(id, rating, [:session_id]) + +[:id] Specifies an individual movie via it's TMDb id +[:rating] What you want to set the rating to. +[:session_id] (optional) If you don't have a session id it will be provided after the first requet. One session_id should be used per user. + + status_code, session_id, message = TmdbMovie.set_rating(1, 7.9) + > status_code + => 1 + > session_id + => "1234" + > message + => "success" + + + status_code, session_id, message = TmdbMovie.set_rating(2, 7.9, session_id: "1234") + > status_code + => 1 + > session_id + => "1234" + > message + => "success" + ==== TmdbCast.find([:id, :name, :limit, :expand_results, :language]) Find information about an individual cast member, or a set of cast members sharing similar names, eg: @@ -66,6 +96,50 @@ Find information about an individual cast member, or a set of cast members shari You must supply at least one of :id or :name. All other parameters are optional. +==== TmdbList + +TmdbList is a class with 5 main methods + + list = TmdbList.upcoming + list = TmdbList.now_playing + list = TmdbList.top_rated + list = TmdbList.popular + list = TmdbList.changes + +Each method except changes has an optional page and language arguments. + + list = TmdbList.popular(2,"EN") + +TmdbList.changes only has an options hash and the TmdbList:Movie objects are only populated with an id. + + list = TmdbList.changes({page: 1, start_date: '2012-03-25', end_date: '2012-03-25', language: 'EN'}) + +[:page] Each page returns 100 results +[:start_date] Sets the start date for when a change occured +[:end_date] Sets the end date for when a change occured +[:language] See TmdbMovie + +All parameters are optional. The defaults are {page: 1, start_date: Time.now.strftime("%Y-%m-%d"), end_date: (Time.now+60*60*24).strftime("%Y-%m-%d"), language: "EN" } + + +A TmdbList instance has theses accessors + + attr_accessor :page, :total_pages, :total_results, :movies_data, :movies + +And these methods + + list.movie_ids #mapped array of movie id's + +an array of tmdb movie ids. + list.movie_ids +the original json data returned from tmdb. + list.movies_data +an array of TmdbList::Movie objects. + list.movies + +A TmdbList::Movie object has these accessors. + + attr_accessor :backdrop_path, :id, :original_title, :release_date, :poster_path, :title, :vote_average, :vote_count === Usage Examples diff --git a/VERSION b/VERSION index 448a0fa..ce4f5af 100644 --- a/VERSION +++ b/VERSION @@ -1 +1 @@ -0.3.4 \ No newline at end of file +0.3.7 \ No newline at end of file diff --git a/lib/ruby-tmdb3/tmdb.rb b/lib/ruby-tmdb3/tmdb.rb index 28d5e15..43b6097 100644 --- a/lib/ruby-tmdb3/tmdb.rb +++ b/lib/ruby-tmdb3/tmdb.rb @@ -10,11 +10,14 @@ class Tmdb @@api_key = "" @@default_language = "en" @@api_response = {} + @@last_request_at = Time.now-3600 + @@rate_limit_time = 0.34 + @@base_api_url = "http://api.themoviedb.org/3" # TODO: Should be refreshed and cached from API CONFIGURATION = DeepOpenStruct.load({ "images" => { - "base_url" => "http://cf2.imgobject.com/t/p/", + "base_url" => "http://image.tmdb.org/t/p/", "posters_sizes" => ["w92", "w154", "w185", "w342", "w500", "original"], "backdrops_sizes" => ["w300", "w780", "w1280", "original"], "profiles_sizes" => ["w45", "w185", "h632", "original"], @@ -30,6 +33,14 @@ def self.api_key=(key) @@api_key = key end + def self.rate_limit_time + @@rate_limit_time + end + + def self.rate_limit_time=(time) + @@rate_limit_time = time + end + def self.default_language @@default_language end @@ -39,15 +50,21 @@ def self.default_language=(language) end def self.base_api_url - "http://api.themoviedb.org/3" + @@base_api_url + end + + + def self.base_api_url=(url) + @@base_api_url=url end - def self.api_call(method, data, language = nil) + def self.api_call(method, data, language = nil, post = false) raise ArgumentError, "Tmdb.api_key must be set before using the API" if(Tmdb.api_key.nil? || Tmdb.api_key.empty?) raise ArgumentError, "Invalid data." if(data.nil? || (data.class != Hash)) - - method, action = method.split '/' - + + action = method.match(%r{.*/(.*)})[1] rescue nil + method = method.sub(%r{/[^/]*?$}, '') + data = { :api_key => Tmdb.api_key }.merge(data) @@ -70,18 +87,24 @@ def self.api_call(method, data, language = nil) if data.has_key?(:id) uri.query_values = query_values # Construct URL other queries - else + elsif data.has_key?(:query) query_values = { :query => CGI::escape(data[:query]) }.merge(query_values) uri.query_values = query_values + else + #do nothing? had to add this to allow for upcoming movie searches + uri.query_values = query_values end + url = [Tmdb.base_api_url, method, data[:id], action].compact.join '/' url_with_query = [url, uri.query].compact.join '?' - response = Tmdb.get_url(url_with_query) - if(response.code.to_i != 200) - raise RuntimeError, "Tmdb API returned status code '#{response.code}' for URL: '#{url}'" + response = Tmdb.get_url(url_with_query) unless post + response = Tmdb.post_url(url_with_query, query_values) if post + + if(response.code.to_i != 200 && response.code.to_i != 201) + raise RuntimeError, "Tmdb API returned status code '#{response.code}' for URL: '#{url_with_query}'" end body = JSON(response.body) @@ -93,7 +116,32 @@ def self.api_call(method, data, language = nil) end # Get a URL and return a response object, follow upto 'limit' re-directs on the way + def self.post_url(uri_str, query_values, limit = 10) + if Time.now < @@last_request_at+@@rate_limit_time #this will help avoid rate limit issues + sleep @@last_request_at+@@rate_limit_time-Time.now if @@rate_limit_time > 0 + end + @@last_request_at = Time.now + return false if limit == 0 + begin + uri = URI(uri_str) + request = Net::HTTP::Post.new(uri.request_uri) + request.set_content_type("application/json") + response = Net::HTTP.start(uri.hostname, uri.port) {|http| + http.request(request, query_values.to_json) + } + rescue SocketError, Errno::ENETDOWN + response = Net::HTTPBadRequest.new( '404', 404, "Not Found" ) + return response + end + response + end + def self.get_url(uri_str, limit = 10) + + if Time.now < @@last_request_at+@@rate_limit_time #this will help avoid rate limit issues + sleep @@last_request_at+@@rate_limit_time-Time.now if @@rate_limit_time > 0 + end + @@last_request_at = Time.now return false if limit == 0 begin response = Net::HTTP.get_response(URI.parse(uri_str)) diff --git a/lib/ruby-tmdb3/tmdb_cast.rb b/lib/ruby-tmdb3/tmdb_cast.rb index 9ebc887..3dbb92a 100644 --- a/lib/ruby-tmdb3/tmdb_cast.rb +++ b/lib/ruby-tmdb3/tmdb_cast.rb @@ -35,16 +35,21 @@ def self.find(options) end end + def self.new(raw_data, expand_results = false, language = nil) # expand the result by calling Person.getInfo unless :expand_results is set to false or the data is already complete # (as determined by checking for the 'birthday' property) - if(expand_results && !raw_data.has_key?("birthday")) + if(expand_results && (!raw_data.has_key?("posters") || !raw_data.has_key?("birthday"))) begin - expanded_data = Tmdb.api_call("person", {:id => raw_data["id"].to_s}, language) + cast_id = raw_data["id"].to_s + raw_data = Tmdb.api_call("person", {:id => cast_id}, language) + @images_data = Tmdb.api_call("person/#{cast_id}/images", {}, language) + if raw_data + raw_data['posters'] = @images_data ? @images_data['profiles'] : [] + end rescue RuntimeError => e raise ArgumentError, "Unable to fetch expanded info for Cast ID: '#{raw_data["id"]}'" if expanded_data.nil? end - raw_data = expanded_data end return Tmdb.data_to_object(raw_data) end diff --git a/lib/ruby-tmdb3/tmdb_list.rb b/lib/ruby-tmdb3/tmdb_list.rb new file mode 100644 index 0000000..19033ba --- /dev/null +++ b/lib/ruby-tmdb3/tmdb_list.rb @@ -0,0 +1,77 @@ +class TmdbList + + class NilData < StandardError; end; + class CorruptData < StandardError; end; + + attr_accessor :page, :total_pages, :total_results, :movies_data, :movies + + class Movie + + ATTRIBUTES =[:backdrop_path, :id, :original_title, :release_date, :poster_path, :title, :vote_average, :vote_count] + attr_accessor *ATTRIBUTES + + def initialize(data) + begin + ATTRIBUTES.each do |attr| + instance_variable_set("@#{attr}", data[attr.to_s]) + end + rescue + raise "no data for movie" + end + end + + end + + def initialize(data) + raise NilData if data.nil? + begin + self.movies_data = data + self.page = data["page"] + self.movies = data["results"].map{|r| TmdbList::Movie.new(r) rescue nil}.compact + self.total_pages = data["total_pages"] + self.total_results = data["total_results"] + rescue Exception => e + raise CorruptData, "Data wasn't in a structure we expected.\n #{e.message}\n #{e.backtrace}" + end + end + + def movie_ids + self.movies.map(&:id) + end + + + def self.upcoming(page = 1, language = nil) + data = Tmdb.api_call("movie/upcoming", {:page => page}, language) + return TmdbList.new(data) + end + + def self.now_playing(page = 1, language = nil) + data = Tmdb.api_call("movie/now_playing", {:page => page}, language) + return TmdbList.new(data) + end + + def self.top_rated(page = 1, language = nil) + data = Tmdb.api_call("movie/top_rated", {:page => page}, language) + return TmdbList.new(data) + end + + def self.popular(page = 1, language = nil) + data = Tmdb.api_call("movie/popular", {:page => page}, language) + return TmdbList.new(data) + end + + def self.changes(new_options = {}) + options = { + :page => 1, + :start_date => Time.now.strftime("%Y-%m-%d"), + :end_date => (Time.now+86400).strftime("%Y-%m-%d") #by default pull 1 days worth of changes + }.merge(new_options) + data = Tmdb.api_call('movie/changes', { + :page => options[:page], + :start_date=> options[:start_date], + :end_date=> options[:end_date] + }) + return TmdbList.new(data) + end + +end \ No newline at end of file diff --git a/lib/ruby-tmdb3/tmdb_movie.rb b/lib/ruby-tmdb3/tmdb_movie.rb index 42bcf42..5dac08b 100644 --- a/lib/ruby-tmdb3/tmdb_movie.rb +++ b/lib/ruby-tmdb3/tmdb_movie.rb @@ -1,5 +1,23 @@ class TmdbMovie + def self.set_rating(id, rating, options = {}) + options = { + :session_id => nil, + :language => Tmdb.default_language + }.merge(options) + session_id = options[:session_id] + unless session_id + response = Tmdb.api_call("authentication/guest_session/new", {}, options[:language]) + session_id = response["guest_session_id"] + end + response = Tmdb.api_call("movie/rating", {:id=> id, :guest_session_id=> session_id, :value=> rating}, options[:language], true) + return { + :status_code=> response["status_code"], + :session_id=> session_id, + :status_message=> response["status_message"] + } + end + def self.find(options) options = { :expand_results => true, diff --git a/ruby-tmdb3.gemspec b/ruby-tmdb3.gemspec index f1fbe5c..6be7577 100644 --- a/ruby-tmdb3.gemspec +++ b/ruby-tmdb3.gemspec @@ -5,10 +5,10 @@ Gem::Specification.new do |s| s.name = "ruby-tmdb3" - s.version = "0.3.3" + s.version = "0.3.7" s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version= - s.authors = ["Irio Irineu Musskopf Junior", "Aaron Gough"] + s.authors = ["Irio Irineu Musskopf Junior", "Aaron Gough", "Kelly Mahan"] s.date = "2012-09-15" s.description = "An ActiveRecord-style API wrapper for TheMovieDB.org" s.email = "iirineu@gmail.com" diff --git a/test/fixtures/authentication_guest_session_new.txt b/test/fixtures/authentication_guest_session_new.txt new file mode 100644 index 0000000..79d6a07 --- /dev/null +++ b/test/fixtures/authentication_guest_session_new.txt @@ -0,0 +1,15 @@ +HTTP/1.1 200 OK +Server: nginx +Date: Mon, 23 Aug 2010 16:45:21 GMT +Content-Type: text/json; charset=utf-8 +Transfer-Encoding: chunked +Connection: keep-alive +Keep-Alive: timeout=20 +Status: 200 OK +Cache-Control: public, max-age=21600 +X-Varnish: 2542127928 2542059531 +Age: 1000 +Via: 1.1 varnish +X-Cache: HIT + +{ "success": true, "guest_session_id": "0c550fd5da2fc3f321ab3bs9b60ca108", "expires_at": "2012-12-04 22:51:19 UTC" } \ No newline at end of file diff --git a/test/fixtures/movie_changes.txt b/test/fixtures/movie_changes.txt new file mode 100644 index 0000000..3729559 --- /dev/null +++ b/test/fixtures/movie_changes.txt @@ -0,0 +1,15 @@ +HTTP/1.1 200 OK +Server: nginx +Date: Mon, 23 Aug 2010 16:45:21 GMT +Content-Type: text/json; charset=utf-8 +Transfer-Encoding: chunked +Connection: keep-alive +Keep-Alive: timeout=20 +Status: 200 OK +Cache-Control: public, max-age=21600 +X-Varnish: 2542127928 2542059531 +Age: 1000 +Via: 1.1 varnish +X-Cache: HIT + +{ "results": [ { "id": 77946, "adult": false }, { "id": 125527, "adult": false }, { "id": 41023, "adult": false }, { "id": 41471, "adult": false }, { "id": 11058, "adult": false }, { "id": 136838, "adult": false }, { "id": 41024, "adult": false }, { "id": 136842, "adult": false }, { "id": 136844, "adult": false }, { "id": 136845, "adult": false }, { "id": 707, "adult": false }, { "id": 13572, "adult": false }, { "id": 96428, "adult": false }, { "id": 13377, "adult": false }, { "id": 117416, "adult": false }, { "id": 12405, "adult": false }, { "id": 77338, "adult": false }, { "id": 4643, "adult": false }, { "id": 136847, "adult": false }, { "id": 10264, "adult": false }, { "id": 84165, "adult": false }, { "id": 136849, "adult": false }, { "id": 52256, "adult": false }, { "id": 77697, "adult": false }, { "id": 31800, "adult": false }, { "id": 24808, "adult": false }, { "id": 34840, "adult": false }, { "id": 87428, "adult": false }, { "id": 22099, "adult": false }, { "id": 119372, "adult": false }, { "id": 20813, "adult": false }, { "id": 94671, "adult": false }, { "id": 22097, "adult": false }, { "id": 136510, "adult": false }, { "id": 10068, "adult": false }, { "id": 136850, "adult": false }, { "id": 136851, "adult": false }, { "id": 136852, "adult": false }, { "id": 13531, "adult": false }, { "id": 17971, "adult": false }, { "id": 136854, "adult": false }, { "id": 136855, "adult": false }, { "id": 2103, "adult": false }, { "id": 72933, "adult": false }, { "id": 10787, "adult": false }, { "id": 128508, "adult": false }, { "id": 119212, "adult": false }, { "id": 46929, "adult": false }, { "id": 17102, "adult": false }, { "id": 136811, "adult": false }, { "id": 136808, "adult": false }, { "id": 62974, "adult": false }, { "id": 92693, "adult": false }, { "id": 136803, "adult": false }, { "id": 74495, "adult": false }, { "id": 136856, "adult": false }, { "id": 136790, "adult": false }, { "id": 136857, "adult": false }, { "id": 136858, "adult": false }, { "id": 136859, "adult": false }, { "id": 113128, "adult": false }, { "id": 114150, "adult": false }, { "id": 76492, "adult": false }, { "id": 136861, "adult": false }, { "id": 136862, "adult": false }, { "id": 136863, "adult": false }, { "id": 136864, "adult": false }, { "id": 9833, "adult": false }, { "id": 667, "adult": false }, { "id": 136867, "adult": false }, { "id": 1450, "adult": false }, { "id": 136732, "adult": false }, { "id": 98205, "adult": false }, { "id": 136735, "adult": false }, { "id": 136190, "adult": false }, { "id": 94720, "adult": false }, { "id": 136741, "adult": false }, { "id": 136742, "adult": false }, { "id": 136743, "adult": false }, { "id": 136751, "adult": false }, { "id": 136752, "adult": false }, { "id": 136753, "adult": false }, { "id": 31393, "adult": false }, { "id": 29262, "adult": false }, { "id": 33067, "adult": false }, { "id": 136868, "adult": false }, { "id": 123025, "adult": false }, { "id": 136870, "adult": false }, { "id": 11370, "adult": false }, { "id": 9267, "adult": false }, { "id": 57387, "adult": false }, { "id": 136876, "adult": false }, { "id": 134739, "adult": false }, { "id": 88534, "adult": false }, { "id": 120591, "adult": false }, { "id": 136877, "adult": false }, { "id": 134708, "adult": false }, { "id": 18426, "adult": false }, { "id": 53146, "adult": false }, { "id": 9587, "adult": false } ], "page": 1, "total_pages": 7, "total_results": 664} \ No newline at end of file diff --git a/test/fixtures/movie_list.txt b/test/fixtures/movie_list.txt new file mode 100644 index 0000000..6d6d572 --- /dev/null +++ b/test/fixtures/movie_list.txt @@ -0,0 +1,15 @@ +HTTP/1.1 200 OK +Server: nginx +Date: Mon, 23 Aug 2010 16:45:21 GMT +Content-Type: text/json; charset=utf-8 +Transfer-Encoding: chunked +Connection: keep-alive +Keep-Alive: timeout=20 +Status: 200 OK +Cache-Control: public, max-age=21600 +X-Varnish: 2542127928 2542059531 +Age: 1000 +Via: 1.1 varnish +X-Cache: HIT + +{ "page": 1, "results": [ { "backdrop_path": "/mNvqYhD9wp1Jzoyj6fzKtV1zATk.jpg", "id": 49026, "original_title": "The Dark Knight Rises", "release_date": "2012-07-20", "poster_path": "/g6o0Df5IIe1yOPYLFtuj9FLadxy.jpg", "title": "The Dark Knight Rises", "vote_average": 9.5999999999999996, "vote_count": 8 }, { "backdrop_path": "/nH2agqnLaHTa76vYO63TfqTNyCZ.jpg", "id": 57387, "original_title": "Contre toi", "release_date": "2011-02-02", "poster_path": "/ddvPG3YwrlssCuc9un957wlSirH.jpg", "title": "In Your Hands", "vote_average": 0.0, "vote_count": 0 }, { "backdrop_path": "/mK3DKSB5slGOjZx9Atnvv3UGZV1.jpg", "id": 84327, "original_title": "The Queen of Versailles", "release_date": "2012-07-06", "poster_path": "/se9SuLfMmEjRWFyY3VlvbJkutnl.jpg", "title": "The Queen of Versailles", "vote_average": 0.0, "vote_count": 0 }, { "backdrop_path": "/lefGNO7sjlbdQqh98Gj9jMGvm4e.jpg", "id": 80041, "original_title": "The Runway", "release_date": "2011-12-02", "poster_path": "/51VGp7YGGwQa3pvMipIlF9xYMAN.jpg", "title": "The Runway", "vote_average": 0.0, "vote_count": 0 }, { "backdrop_path": "/5Z4UIO0o4Z7gw4ALdberVM77FaM.jpg", "id": 62580, "original_title": "La fille du puisatier", "release_date": "2011-04-20", "poster_path": "/h6Ox6v6DvxeXqNq1RdEIPEScyZl.jpg", "title": "The Well Digger's Daughter", "vote_average": 6.2999999999999998, "vote_count": 2 }, { "backdrop_path": "/dyXam0TpQvNzaSTVvY5wFa72Wne.jpg", "id": 103332, "original_title": "Ruby Sparks", "release_date": "2012-10-12", "poster_path": "/eXNKoDyYWjhPmNTNRNhl11wsBvU.jpg", "title": "Ruby Sparks", "vote_average": 0.0, "vote_count": 0 }, { "backdrop_path": null, "id": 84177, "original_title": "Big Boys Gone Bananas!*", "release_date": "2011-11-19", "poster_path": "/6gjN9JJmeTtA9cp0FncGxFJEb2z.jpg", "title": "Big Boys Gone Bananas!*", "vote_average": 0.0, "vote_count": 0 }, { "backdrop_path": "/yGYyb6pXLmabAxpy9TsCRskQafe.jpg", "id": 73567, "original_title": "Killer Joe", "release_date": "2011-09-11", "poster_path": "/6EBhU4BHWdx5UM0o1PrIZAYcH3q.jpg", "title": "Killer Joe", "vote_average": 0.0, "vote_count": 0 }, { "backdrop_path": "/2JITwlU9ZfbE7WM1sNMLuuB7dW.jpg", "id": 55741, "original_title": "Sacrifice", "release_date": "2012-07-27", "poster_path": "/fJqR7fhi3GqjF2rHA0Bs1vw8RQ5.jpg", "title": "Sacrifice", "vote_average": 7.5, "vote_count": 1 }, { "backdrop_path": "/yxqXznxXmrw2x8qqraXJf1RJG7B.jpg", "id": 84334, "original_title": "Searching for Sugar Man", "release_date": "2012-07-27", "poster_path": "/xkMfURabJuRAe3DESNYmhhYvDgh.jpg", "title": "Searching for Sugar Man", "vote_average": 0.0, "vote_count": 0 }, { "backdrop_path": "/cVF9tV2oDmibXaSild5tzJwYfsX.jpg", "id": 85446, "original_title": "Step Up Revolution", "release_date": "2012-07-27", "poster_path": "/nqRbVErs7ri5AsridaKyz2nV67Y.jpg", "title": "Step Up Revolution", "vote_average": 0.0, "vote_count": 0 }, { "backdrop_path": "/9yz83rotKPMCK0NX02OguZTlqhf.jpg", "id": 80035, "original_title": "The Watch", "release_date": "2012-07-27", "poster_path": "/iNqbF3tJXEZ56z0sWieLkXsHx0p.jpg", "title": "The Watch", "vote_average": 6.0, "vote_count": 1 }, { "backdrop_path": "/rEkMbYqeD3JgsCkniOiP1ZIlWCl.jpg", "id": 89455, "original_title": "360", "release_date": "2012-08-03", "poster_path": "/59sNZsa7FUxV3LQ1kMeUBqXSUqm.jpg", "title": "360", "vote_average": 0.0, "vote_count": 0 }, { "backdrop_path": "/wHkUB68RPxtsjqfv7o1qxhDugfG.jpg", "id": 98066, "original_title": "The Babymakers", "release_date": "2012-03-09", "poster_path": "/pJC7qNGruT6bBVMrslapx3lJKEv.jpg", "title": "The Babymakers", "vote_average": 0.0, "vote_count": 0 }, { "backdrop_path": "/vcpX1seqooSUa4toZqG3fpV2r5C.jpg", "id": 84184, "original_title": "Celeste and Jesse Forever", "release_date": "2012-08-03", "poster_path": "/A8Y74lJ3ucp52JGFOc13WD9JsJB.jpg", "title": "Celeste and Jesse Forever", "vote_average": 0.0, "vote_count": 0 }, null, { "backdrop_path": "/6zzRMEAFjnthrIgPXd0tmxlFwMp.jpg", "id": 82650, "original_title": "Diary of a Wimpy Kid: Dog Days", "release_date": "2012-08-03", "poster_path": "/o2YcOotynpXiEVoekvA9LdCRueu.jpg", "title": "Diary of a Wimpy Kid: Dog Days", "vote_average": 0.0, "vote_count": 0 }, { "backdrop_path": null, "id": 112304, "original_title": "Soldiers of Fortune", "release_date": "2012-08-03", "poster_path": "/fatdPFiq5aP8ezbeWpSXJcypUX5.jpg", "title": "Soldiers of Fortune", "vote_average": 0.0, "vote_count": 0 }, { "backdrop_path": "/yRaCI0pjb1FGgagYXjg7dV4lkJJ.jpg", "id": 64635, "original_title": "Total Recall", "release_date": "2012-08-03", "poster_path": "/pB4pxb2UTNdDiOLEXDSdeElhKo5.jpg", "title": "Total Recall", "vote_average": 0.0, "vote_count": 0 }, { "backdrop_path": null, "id": 84165, "original_title": "2 Days in New York", "release_date": "2012-08-10", "poster_path": "/7rRzvEjo8HsZQ71IYiSf60n9BoN.jpg", "title": "2 Days in New York", "vote_average": 0.0, "vote_count": 0 }, { "backdrop_path": "/hWKEuGLT3DBLhSWSF3ID2S5UzuW.jpg", "id": 84169, "original_title": "Ai Weiwei: Never Sorry", "release_date": "2012-08-10", "poster_path": "/cozaFTNAY9M8UtdOR9xNl9oaFHE.jpg", "title": "Ai Weiwei: Never Sorry", "vote_average": 0.0, "vote_count": 0 } ], "total_pages": 5, "total_results": 100} \ No newline at end of file diff --git a/test/fixtures/movie_set_rating.txt b/test/fixtures/movie_set_rating.txt new file mode 100644 index 0000000..dd5c94c --- /dev/null +++ b/test/fixtures/movie_set_rating.txt @@ -0,0 +1,15 @@ +HTTP/1.1 200 OK +Server: nginx +Date: Mon, 23 Aug 2010 16:45:21 GMT +Content-Type: text/json; charset=utf-8 +Transfer-Encoding: chunked +Connection: keep-alive +Keep-Alive: timeout=20 +Status: 200 OK +Cache-Control: public, max-age=21600 +X-Varnish: 2542127928 2542059531 +Age: 1000 +Via: 1.1 varnish +X-Cache: HIT + +{ "status_code": 1, "status_message": "Success" } \ No newline at end of file diff --git a/test/fixtures/person_images.txt b/test/fixtures/person_images.txt new file mode 100644 index 0000000..0967448 --- /dev/null +++ b/test/fixtures/person_images.txt @@ -0,0 +1,15 @@ +HTTP/1.1 200 OK +Server: nginx +Date: Mon, 23 Aug 2010 16:41:11 GMT +Content-Type: text/json; charset=utf-8 +Transfer-Encoding: chunked +Connection: keep-alive +Keep-Alive: timeout=20 +Status: 200 OK +Cache-Control: public, max-age=21600 +X-Varnish: 2542110576 2542059510 +Age: 751 +Via: 1.1 varnish +X-Cache: HIT + +{"id":287,"profiles":[{"file_path":"/w8zJQuN7tzlm6FY9mfGKihxp3Cb.jpg","width":1295,"height":1969,"iso_639_1":null,"aspect_ratio":0.66000000000000003},{"file_path":"/cLUacutO7dOMksQK8Zg0q2Gybsx.jpg","width":820,"height":1230,"iso_639_1":null,"aspect_ratio":0.67000000000000004},{"file_path":"/zEbgoayf0MfuSznehhXdaP2YkeH.jpg","width":700,"height":983,"iso_639_1":null,"aspect_ratio":0.70999999999999996}]} \ No newline at end of file diff --git a/test/setup/url_mocks.rb b/test/setup/url_mocks.rb index 2a9e7c9..a405682 100644 --- a/test/setup/url_mocks.rb +++ b/test/setup/url_mocks.rb @@ -1,5 +1,10 @@ def register_api_url_stubs unless(TEST_LIVE_API) + + + File.open(File.join(File.dirname(__FILE__), "..", "fixtures", "authentication_guest_session_new.txt")) do |file| + stub_request(:get, Regexp.new(Tmdb.base_api_url + "/authentication/guest_session/new" + ".*")).to_return(file) + end File.open(File.join(File.dirname(__FILE__), "..", "fixtures", "movie_search.txt")) do |file| stub_request(:get, Regexp.new(Tmdb.base_api_url + "/search/movie" + ".*")).to_return(file) @@ -12,7 +17,31 @@ def register_api_url_stubs File.open(File.join(File.dirname(__FILE__), "..", "fixtures", "movie_get_info.txt")) do |file| stub_request(:get, Regexp.new(Tmdb.base_api_url + "/movie/" + ".*")).to_return(file) end - + + File.open(File.join(File.dirname(__FILE__), "..", "fixtures", "movie_set_rating.txt")) do |file| + stub_request(:post, Regexp.new(Tmdb.base_api_url + '/movie/\d+/rating' + ".*")).to_return(file) + end + + File.open(File.join(File.dirname(__FILE__), "..", "fixtures", "movie_list.txt")) do |file| + stub_request(:get, Regexp.new(Tmdb.base_api_url + "/movie/upcoming" + ".*")).to_return(file) + end + + File.open(File.join(File.dirname(__FILE__), "..", "fixtures", "movie_list.txt")) do |file| + stub_request(:get, Regexp.new(Tmdb.base_api_url + "/movie/top_rated" + ".*")).to_return(file) + end + + File.open(File.join(File.dirname(__FILE__), "..", "fixtures", "movie_list.txt")) do |file| + stub_request(:get, Regexp.new(Tmdb.base_api_url + "/movie/popular" + ".*")).to_return(file) + end + + File.open(File.join(File.dirname(__FILE__), "..", "fixtures", "movie_list.txt")) do |file| + stub_request(:get, Regexp.new(Tmdb.base_api_url + "/movie/now_playing" + ".*")).to_return(file) + end + + File.open(File.join(File.dirname(__FILE__), "..", "fixtures", "movie_changes.txt")) do |file| + stub_request(:get, Regexp.new(Tmdb.base_api_url + "/movie/changes" + ".*")).to_return(file) + end + File.open(File.join(File.dirname(__FILE__), "..", "fixtures", "movie_posters.txt")) do |file| stub_request(:get, Regexp.new(Tmdb.base_api_url + '/movie/\d+/images')).to_return(file) end @@ -49,6 +78,10 @@ def register_api_url_stubs stub_request(:get, Regexp.new(Tmdb.base_api_url + "/person/" + ".*")).to_return(file) end + File.open(File.join(File.dirname(__FILE__), "..", "fixtures", "person_images.txt")) do |file| + stub_request(:get, Regexp.new(Tmdb.base_api_url + '/person/\d+/images')).to_return(file) + end + File.open(File.join(File.dirname(__FILE__), "..", "fixtures", "person_search.txt")) do |file| stub_request(:get, Regexp.new(Tmdb.base_api_url + "/search/person" + ".*")).to_return(file) end diff --git a/test/unit/fetch_trailers_with_expansion_enabled_test.rb b/test/unit/fetch_trailers_with_expansion_enabled_test.rb index b4cc22f..e0ba40d 100644 --- a/test/unit/fetch_trailers_with_expansion_enabled_test.rb +++ b/test/unit/fetch_trailers_with_expansion_enabled_test.rb @@ -3,6 +3,7 @@ class FetchTrailersWithExpansionEnabled < Test::Unit::TestCase def setup register_api_url_stubs + Tmdb.rate_limit_time = 0 end test "find with expansion enabled should return trailers" do diff --git a/test/unit/test_direct_require.rb b/test/unit/test_direct_require.rb index ebff4e3..a9459c8 100644 --- a/test/unit/test_direct_require.rb +++ b/test/unit/test_direct_require.rb @@ -10,7 +10,12 @@ end class DirectRequireTest < Test::Unit::TestCase + + def setup + Tmdb.rate_limit_time = 0 + end + test "TmdbMovie should not raise exception when directly required without using rubygems" do Tmdb.stubs(:api_call).returns([]) assert_nothing_raised do diff --git a/test/unit/tmdb_cast_test.rb b/test/unit/tmdb_cast_test.rb index cc46800..f8e0b29 100644 --- a/test/unit/tmdb_cast_test.rb +++ b/test/unit/tmdb_cast_test.rb @@ -4,6 +4,7 @@ class TmdbCastTest < Test::Unit::TestCase def setup register_api_url_stubs + Tmdb.rate_limit_time = 0 end test "search that returns no results should create empty array" do @@ -22,6 +23,11 @@ def setup cast = TmdbCast.find(:id => 287) assert_cast_methodized(cast, 287) end + + test "each cast member should include poster data" do + cast = TmdbCast.find(:id => 287) + assert_kind_of Array, cast.posters + end test "two cast objects with same data should be equal" do cast1 = TmdbCast.find(:id => 287, :limit => 1) @@ -101,6 +107,7 @@ def setup test "TmdbCast.new should raise error if supplied with raw data for cast member that doesn't exist" do Tmdb.expects(:api_call).with("person", {:id => "999999999999"}, nil).returns(nil) + Tmdb.expects(:api_call).with("person/999999999999/images", {}, nil).returns(nil) assert_raise ArgumentError do TmdbCast.new({"id" => 999999999999}, true) end diff --git a/test/unit/tmdb_list_test.rb b/test/unit/tmdb_list_test.rb new file mode 100644 index 0000000..8fa82bf --- /dev/null +++ b/test/unit/tmdb_list_test.rb @@ -0,0 +1,55 @@ +require File.expand_path(File.join(File.dirname(__FILE__), '..', 'test_helper.rb')) + +class TmdbListTest < Test::Unit::TestCase + + IDLIST = [49026,57387,84327,80041,62580,103332,84177,73567,55741,84334,85446,80035,89455,98066,84184,82650,112304,64635,84165,84169] + + def setup + register_api_url_stubs + Tmdb.rate_limit_time = 0 + end + + test "should return upcoming movies" do + list = TmdbList.upcoming + assert_kind_of Array, list.movie_ids + assert_equal IDLIST, list.movie_ids + end + + test "should return popular movies" do + list = TmdbList.popular + assert_kind_of Array, list.movie_ids + assert_equal IDLIST, list.movie_ids + end + + test "should return top_rate movies" do + list = TmdbList.top_rated + assert_kind_of Array, list.movie_ids + assert_equal IDLIST, list.movie_ids + end + + test "should return now_playing movies" do + list = TmdbList.now_playing + assert_kind_of Array, list.movie_ids + assert_equal IDLIST, list.movie_ids + end + + test "should return an array of Tmdb::Movie classes" do + list = TmdbList.now_playing + assert_kind_of Array, list.movies + assert_kind_of TmdbList::Movie, list.movies[0] + assert_equal "The Dark Knight Rises", list.movies[0].original_title + end + + test "should return a list of movies that have changed" do + list = TmdbList.changes({ + page: 1, + start_date: "2012-03-25", + end_date: "2012-03-26" + }) + assert_kind_of Array, list.movie_ids + assert_equal 77946, list.movie_ids.first + end + + + +end \ No newline at end of file diff --git a/test/unit/tmdb_movie_test.rb b/test/unit/tmdb_movie_test.rb index 2daaff1..06c30ae 100644 --- a/test/unit/tmdb_movie_test.rb +++ b/test/unit/tmdb_movie_test.rb @@ -4,6 +4,7 @@ class TmdbMovieTest < Test::Unit::TestCase def setup register_api_url_stubs + Tmdb.rate_limit_time = 0 end test "search that returns no results should create empty array" do @@ -120,6 +121,18 @@ def setup end end + test "TmdbMovie.set_rating without a session id should set a rating and return a session id" do + response = TmdbMovie.set_rating("1", 7.9) + assert_equal response[:status_code], 1 + assert_equal response[:session_id], "0c550fd5da2fc3f321ab3bs9b60ca108" + end + + test "TmdbMovie.set_rating with a session id should set a rating and return the same session id" do + response = TmdbMovie.set_rating("1", 7.9, session_id: "1234") + assert_equal response[:status_code], 1 + assert_equal response[:session_id], "1234" + end + private def assert_movie_methodized(movie, movie_id) diff --git a/test/unit/tmdb_test.rb b/test/unit/tmdb_test.rb index 69bfc43..f111e7b 100644 --- a/test/unit/tmdb_test.rb +++ b/test/unit/tmdb_test.rb @@ -5,6 +5,7 @@ class TmdbTest < Test::Unit::TestCase def setup register_api_url_stubs @@old_default_language = Tmdb.default_language + Tmdb.rate_limit_time = 0 end def teardown @@ -19,6 +20,14 @@ def teardown Tmdb.api_key = old_api_key end + test "should allow setting and getting of rate_limit_time" do + old_rate_limit_time = Tmdb.rate_limit_time + rate_limit_time = 2 + Tmdb.rate_limit_time = rate_limit_time + assert_equal Tmdb.rate_limit_time, rate_limit_time + Tmdb.rate_limit_time = old_rate_limit_time + end + test "language should default to 'en'" do assert_equal "en", Tmdb.default_language end