lib/ruby-tmdb/tmdb.rb in ruby-tmdb-0.1.0 vs lib/ruby-tmdb/tmdb.rb in ruby-tmdb-0.1.1
- old
+ new
@@ -2,10 +2,11 @@
require 'net/http'
require 'uri'
require 'cgi'
require 'yaml'
+ require 'deepopenstruct'
@@api_key = ""
@@api_response = {}
def self.api_key
@@ -19,16 +20,12 @@
def self.base_api_url
"http://api.themoviedb.org/2.1/"
end
def self.api_call(method, data, language = "en")
- raise ArgumentError, "Tmdb.api_key must be set before using the API" if(Tmdb.api_key.nil? || Tmdb.api_key.empty?)
url = Tmdb.base_api_url + method + '/' + language + '/yaml/' + Tmdb.api_key + '/' + CGI::escape(data.to_s)
- # Memoize this API call
- response = @@api_response[url] ||= begin
- Tmdb.get_url(url)
- end
+ response = Tmdb.get_url(url)
if(response.code.to_i != 200)
return nil
end
body = YAML::load(response.body)
if( body.first.include?("Nothing found"))
@@ -51,8 +48,39 @@
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
+ ["posters", "backdrops", "profile"].each do |image_array_name|
+ if(object.respond_to?(image_array_name))
+ image_array = object.send(image_array_name)
+ image_array.each_index do |x|
+ image_array[x] = image_array[x].image
+ image_array[x].instance_eval <<-EOD
+ def self.data
+ return Tmdb.get_url(self.url).body
+ end
+ EOD
+ end
+ end
+ if(object.profile)
+ object.profiles = object.profile
+ end
+ end
+ unless(object.cast.nil?)
+ object.cast.each_index do |x|
+ object.cast[x].instance_eval <<-EOD
+ def self.bio
+ return TmdbCast.find(:id => self.id, :limit => 1)
+ end
+ EOD
+ end
+ end
+ return object
end
end
\ No newline at end of file