Permalink
Browse files

Tweak all of the databases

  • Loading branch information...
1 parent db9a127 commit aec7149eae5dc8300ea2643121b32f287484079a @Sutto committed Oct 13, 2012
Showing with 59 additions and 28 deletions.
  1. +19 −18 lib/ruby-tmdb3/tmdb.rb
  2. +40 −10 lib/ruby-tmdb3/tmdb_movie.rb
View
@@ -1,47 +1,47 @@
class Tmdb
-
+
require 'net/http'
require 'uri'
require 'cgi'
require 'json'
require 'deepopenstruct'
require "addressable/uri"
-
+
@@api_key = ""
@@default_language = "en"
@@api_response = {}
- # TODO: Should be refreshed and cached from API
+ # TODO: Should be refreshed and cached from API
CONFIGURATION = DeepOpenStruct.load({ "images" =>
- {
- "base_url" => "http://cf2.imgobject.com/t/p/",
+ {
+ "base_url" => "http://cf2.imgobject.com/t/p/",
"posters_sizes" => ["w92", "w154", "w185", "w342", "w500", "original"],
"backdrops_sizes" => ["w300", "w780", "w1280", "original"],
"profiles_sizes" => ["w45", "w185", "h632", "original"],
"logos_sizes" => ["w45", "w92", "w154", "w185", "w300", "w500", "original"]
}
})
-
+
def self.api_key
@@api_key
end
-
+
def self.api_key=(key)
@@api_key = key
end
-
+
def self.default_language
@@default_language
end
-
+
def self.default_language=(language)
@@default_language = language
end
-
+
def self.base_api_url
"http://api.themoviedb.org/3"
end
-
+
def self.api_call(method, data, language = nil)
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))
@@ -70,15 +70,16 @@ 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) && !data[:query].empty?
query_values = {
:query => CGI::escape(data[:query])
}.merge(query_values)
- uri.query_values = query_values
end
+ uri.query_values = query_values unless query_values.empty?
+
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}'"
@@ -95,20 +96,20 @@ def self.api_call(method, data, language = nil)
# Get a URL and return a response object, follow upto 'limit' re-directs on the way
def self.get_url(uri_str, limit = 10)
return false if limit == 0
- begin
+ begin
response = Net::HTTP.get_response(URI.parse(uri_str))
rescue SocketError, Errno::ENETDOWN
response = Net::HTTPBadRequest.new( '404', 404, "Not Found" )
return response
- end
+ end
case response
when Net::HTTPSuccess then response
when Net::HTTPRedirection then get_url(response['location'], limit - 1)
else
Net::HTTPBadRequest.new( '404', 404, "Not Found" )
end
end
-
+
def self.data_to_object(data)
object = DeepOpenStruct.load(data)
object.raw_data = data
@@ -137,5 +138,5 @@ def self.bio
end
return object
end
-
+
end
@@ -1,13 +1,43 @@
class TmdbMovie
-
+
+ def self.collection_of_movies(endpoint, options)
+ options = {
+ :language => Tmdb.default_language
+ }.merge(options)
+ results = []
+ api_return = Tmdb.api_call("movie/#{endpoint}", options.except(:language), options[:language])
+ results << api_return["results"] if api_return
+ results.flatten!(1)
+ results.uniq!
+ results.delete_if &:nil?
+ results.map!{|m| TmdbMovie.new(m, false, options[:language])}
+ results
+ end
+
+ def self.popular(options = {})
+ collection_of_movies "popular", options
+ end
+
+ def self.upcoming(options = {})
+ collection_of_movies "upcoming", options
+ end
+
+ def self.now_playing(options = {})
+ collection_of_movies "now_playing", options
+ end
+
+ def self.top_rated(options = {})
+ collection_of_movies "top_rated", options
+ end
+
def self.find(options)
options = {
:expand_results => true,
:language => Tmdb.default_language
}.merge(options)
-
+
raise ArgumentError, "At least one of: id, title, imdb should be supplied" if(options[:id].nil? && options[:title].nil? && options[:imdb].nil?)
-
+
results = []
unless(options[:id].nil? || options[:id].to_s.empty?)
results << Tmdb.api_call("movie", {:id => options[:id].to_s}, options[:language])
@@ -20,25 +50,25 @@ def self.find(options)
results << Tmdb.api_call("movie", {:id => options[:imdb].to_s}, options[:language])
options[:expand_results] = true
end
-
+
results.flatten!(1)
results.uniq!
results.delete_if &:nil?
-
+
unless(options[:limit].nil?)
raise ArgumentError, ":limit must be an integer greater than 0" unless(options[:limit].is_a?(Fixnum) && options[:limit] > 0)
results = results.slice(0, options[:limit])
end
-
+
results.map!{|m| TmdbMovie.new(m, options[:expand_results], options[:language])}
-
+
if(results.length == 1)
return results.first
else
return results
end
end
-
+
def self.new(raw_data, expand_results = false, language = nil)
# expand the result by calling movie unless :expand_results is false or the data is already complete
# (as determined by checking for the posters property in the raw data)
@@ -60,10 +90,10 @@ def self.new(raw_data, expand_results = false, language = nil)
end
return Tmdb.data_to_object(raw_data)
end
-
+
def ==(other)
return false unless(other.is_a?(TmdbMovie))
return @raw_data == other.raw_data
end
-
+
end

0 comments on commit aec7149

Please sign in to comment.