Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
33 commits
Select commit Hold shift + click to select a range
69a3968
wrote TmdbUpcoming class and methods with fixture and test
KellyMahan Dec 3, 2012
9e64727
need to set uri.query_values for live data
KellyMahan Dec 3, 2012
fa5d166
changed TmdbUpcoming to more generic TmdbList. Also added a subclass …
KellyMahan Dec 4, 2012
cdc371d
updated readme and made NilData class name consistent with others
KellyMahan Dec 7, 2012
ae3c0bd
readme formatting
KellyMahan Dec 7, 2012
75306b0
added a new method to the TmdbList class for retrieving which movies …
KellyMahan Dec 7, 2012
1892bb3
changes for lists to let movie_ids pull from the movies array every t…
KellyMahan Dec 7, 2012
6759d51
forgot to add file
KellyMahan Dec 7, 2012
48a3e82
added a rate limit time option so the user isn't required to handle r…
KellyMahan Dec 7, 2012
3d3c664
Didn't think that one through.
KellyMahan Dec 7, 2012
2804643
setting rate_limit to 0 for tests, otherwise tests take over 100 seco…
KellyMahan Dec 7, 2012
0b7b71e
Merge branch 'master' of git://github.com/Irio/ruby-tmdb
KellyMahan Dec 10, 2012
f40e71d
Provided options were being overridden. This fixes that.
KellyMahan Dec 10, 2012
f15db91
even though results were returned using the query param didn't work t…
KellyMahan Dec 10, 2012
5fe7d5a
added ability to rate movies. added tests, readme for rating movies
KellyMahan Dec 12, 2012
264b60c
Merge branch 'master' of git://github.com/Irio/ruby-tmdb
KellyMahan Dec 21, 2012
62e7e4c
added comment about end time.
KellyMahan Dec 21, 2012
fd30639
modified tests for new rating response
KellyMahan Dec 21, 2012
d3cd626
1.8.7 syntax for hash
KellyMahan Dec 21, 2012
a758a25
more 1.8.7 syntax compatible changes
KellyMahan Dec 21, 2012
0091037
missed another 1.8.7 syntax change
KellyMahan Dec 21, 2012
a45f05d
missed another 1.8.7
KellyMahan Dec 21, 2012
9f97f1e
Merge branch 'master' of git://github.com/Irio/ruby-tmdb
KellyMahan Jan 4, 2013
0d57003
changes for cast images
KellyMahan Mar 12, 2013
3eecda7
wrong order of url stub caused an issue
KellyMahan Mar 12, 2013
9e71a13
fix for popular list
KellyMahan May 8, 2013
a6690d8
better debugging
KellyMahan May 8, 2013
f75704c
added Kelly Mahan to authors
KellyMahan May 8, 2013
4ae44ad
added an error check when a list returns a null movie (I think this i…
KellyMahan Sep 10, 2013
106ff3a
updated version
KellyMahan Sep 10, 2013
545b7e1
updated base url
KellyMahan Dec 13, 2013
4488477
updated version
KellyMahan Dec 13, 2013
1d5e20c
missed version in gemspec
KellyMahan Dec 13, 2013
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
74 changes: 74 additions & 0 deletions README.rdoc
Original file line number Diff line number Diff line change
Expand Up @@ -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)
# => <OpenStruct>

Expand Down Expand Up @@ -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:
Expand All @@ -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

Expand Down
2 changes: 1 addition & 1 deletion VERSION
Original file line number Diff line number Diff line change
@@ -1 +1 @@
0.3.4
0.3.7
68 changes: 58 additions & 10 deletions lib/ruby-tmdb3/tmdb.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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"],
Expand All @@ -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
Expand All @@ -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)
Expand All @@ -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)
Expand All @@ -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))
Expand Down
11 changes: 8 additions & 3 deletions lib/ruby-tmdb3/tmdb_cast.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
77 changes: 77 additions & 0 deletions lib/ruby-tmdb3/tmdb_list.rb
Original file line number Diff line number Diff line change
@@ -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
18 changes: 18 additions & 0 deletions lib/ruby-tmdb3/tmdb_movie.rb
Original file line number Diff line number Diff line change
@@ -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,
Expand Down
4 changes: 2 additions & 2 deletions ruby-tmdb3.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
15 changes: 15 additions & 0 deletions test/fixtures/authentication_guest_session_new.txt
Original file line number Diff line number Diff line change
@@ -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" }
15 changes: 15 additions & 0 deletions test/fixtures/movie_changes.txt
Original file line number Diff line number Diff line change
@@ -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}
Loading