Class: Tmdb

Inherits:
Object
  • Object
show all
Defined in:
lib/ruby-tmdb/tmdb.rb

Constant Summary collapse

@@api_key =
""
@@default_language =
"en"
@@api_response =
{}

Class Method Summary collapse

Class Method Details

.api_call(method, data = nil, language = nil) ⇒ Object

Raises:

  • (ArgumentError)


34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
# File 'lib/ruby-tmdb/tmdb.rb', line 34

def self.api_call(method, data = nil, language = nil)
  raise ArgumentError, "Tmdb.api_key must be set before using the API" if(Tmdb.api_key.nil? || Tmdb.api_key.empty?)
  
  language = language || @@default_language
  if data.class == Hash
    # Addressable can only handle hashes whose values respond to to_str, so lets be nice and convert things.
    query_values = {}
    data.each do |key,value|
      if not value.respond_to?(:to_str) and value.respond_to?(:to_s)
        query_values[key] = value.to_s
      else
        query_values[key] = value
      end
    end
    uri = Addressable::URI.new
    uri.query_values = query_values
    params = '?' + uri.query
  elsif not data.nil?
    params = '/' + CGI::escape(data.to_s)
  else
    params = ''  
  end
  url = Tmdb.base_api_url + method + '/' + language + '/json/' + Tmdb.api_key + params 
  response = Tmdb.get_url(url)
  if(response.code.to_i != 200)
    raise RuntimeError, "Tmdb API returned status code '#{response.code}' for URL: '#{url}'"
  end
  body = JSON(response.body)
  if( body.first.include?("Nothing found"))
    return nil
  else
    return body
  end
end

.api_keyObject



14
15
16
# File 'lib/ruby-tmdb/tmdb.rb', line 14

def self.api_key
  @@api_key
end

.api_key=(key) ⇒ Object



18
19
20
# File 'lib/ruby-tmdb/tmdb.rb', line 18

def self.api_key=(key)
  @@api_key = key
end

.base_api_urlObject



30
31
32
# File 'lib/ruby-tmdb/tmdb.rb', line 30

def self.base_api_url
  "http://api.themoviedb.org/2.1/"
end

.data_to_object(data) ⇒ Object



86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
# File 'lib/ruby-tmdb/tmdb.rb', line 86

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

.default_languageObject



22
23
24
# File 'lib/ruby-tmdb/tmdb.rb', line 22

def self.default_language
  @@default_language
end

.default_language=(language) ⇒ Object



26
27
28
# File 'lib/ruby-tmdb/tmdb.rb', line 26

def self.default_language=(language)
  @@default_language = language
end

.get_url(uri_str, limit = 10) ⇒ Object

Get a URL and return a response object, follow upto ‘limit’ re-directs on the way



70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
# File 'lib/ruby-tmdb/tmdb.rb', line 70

def self.get_url(uri_str, limit = 10)
  return false if limit == 0
  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 
  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